我想测量一个RestTemplate.getForObject
调用的 HTTP GET 请求的时间,而不需要解析响应所需的时间。所以只是远程HTTP调用需要的时间。我已经尝试设置 aClientHttpRequestInterceptor
但我认为这不是正确的方法,因为时间似乎是错误的:
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;
}
}
称呼:
RestTemplate rest = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new PerfRequestSyncInterceptor());
rest.setInterceptors(interceptors);
Response inob = rest.getForObject(xmlURL, Response.class);
如何测量 RestTemplate HTTP 请求的时间?