Spring Dimension HTTP RestTemplate Request Time

I want to measure the time of an HTTP GET request to call RestTemplate.getForObject without the time required to parse the response. So just the time it takes for a remote HTTP call. I have already tried setting the ClientHttpRequestInterceptor , but I don't think this is the right way to do this, since 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 RestTemplate HTTP request time?

+6
source share
3 answers

You can use AOP and the built-in PerformanceMonitorInterceptor Spring. You need to correctly determine which methods you want to intercept you can measure. You can configure it as follows:

 <?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> 
+4
source

You can use StopWatch for this.

 public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor { private final static Logger LOG = LoggerFactory.getLogger(PerfRequestSyncInterceptor.class); @Override public ClientHttpResponse intercept(HttpRequest hr, byte[] bytes, ClientHttpRequestExecution chre) throws IOException { Stopwatch stopwatch = Stopwatch.createStarted(); ClientHttpResponse response = chre.execute(hr, bytes); stopwatch.stop(); LOG.info("method=" + hr.getMethod() + ", uri="+hr.getURI() + ", response_time=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", response_code=" + response.getStatusCode().value()); return response; } } 

And in the class where restTemplate is instanced

 private final List<ClientHttpRequestInterceptor> requestInterceptors = new ArrayList<>(); requestInterceptors.add(new PerfRequestSyncInterceptor()); this.restTemplate.setInterceptors(requestInterceptors); 
+1
source

I want to measure HTTP GET request time Call RestTemplate.getForObject without the time required for parsing the response

I had the same requirements. I want to know the server response time to determine how long it will take for the server to respond without processing the RestTemplate response. I added two interceptors to the HttpClientBuilder with the map so that I can measure the time between the request and the low-level response.

 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // Attach interceptors ResponseTimeInterceptor interceptor = new ResponseTimeInterceptor(); httpClientBuilder.addInterceptorFirst( (HttpRequestInterceptor) interceptor ); httpClientBuilder.addInterceptorFirst( (HttpResponseInterceptor) interceptor ); // Use client with RestTemplate or on its own HttpClient client = httpClientBuilder.build(); 

Here is a minimal double-action interceptor:

 public class ResponseTimeInterceptor implements HttpRequestInterceptor, HttpResponseInterceptor { private final Map<HttpContext, Long> requestMap = new MaxSizeHashMap<>( 50 ); @Override public void process( HttpRequest httpRequest, HttpContext httpContext ) throws HttpException, IOException { requestMap.put( httpContext, System.currentTimeMillis() ); } @Override public void process( HttpResponse httpResponse, HttpContext httpContext ) throws HttpException, IOException { long startTime = requestMap.getOrDefault( httpContext, 0L ); long diff = System.currentTimeMillis() - startTime; System.out.println( "Response time: " + diff + "ms" ); } } 

After the interceptor responder returns, the response data continues into the RestTemplate response handler.

Note. MaxSizeHashMap taken from fooobar.com/questions/223912 / ....

0
source

All Articles