I want to measure the time of the HTTP GET request of a RestTemplate.getForObject
call without the the time needed for parsing the response. So just the time the remote HTTP call needs. I already tried setting a ClientHttpRequestInterceptor
but I dont think this is the right way to do it as the time seems to be wrong:
public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor {
private Logger log = Logger.getLogger(this.getClass());
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
long start = System.nanoTime();
ClientHttpResponse resp = execution.execute(request, body);
log.debug("remote request time: "
+ ((System.nanoTime() - start) * Math.pow(10, -9)));
return resp;
}
}
Call:
RestTemplate rest = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new PerfRequestSyncInterceptor());
rest.setInterceptors(interceptors);
Response inob = rest.getForObject(xmlURL, Response.class);
How can I measure the time of a RestTemplate HTTP request?
You can use AOP and the built in PerformanceMonitorInterceptor of Spring. You need to correctly define which methods of which calss you want to intercept then you can measure. You can configure it something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="springMonitoredService"
class="com.myorg.service.springmon.MyServiceSpringImpl"/>
<bean id="springMonitoringAspectInterceptor"
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor">
<property name="loggerName"
value="com.myorg.SPRING_MONITOR"/>
</bean>
<aop:config>
<aop:pointcut id="springMonitoringPointcut"
expression="execution(* java.net.HttpURLConnection.connect(..))"/>
<aop:advisor pointcut-ref="springMonitoringPointcut"
advice-ref="springMonitoringAspectInterceptor"/>
</aop:config>
</beans>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With