【发布时间】:2012-11-08 10:04:00
【问题描述】:
我想测量 RestTemplate.getForObject 调用的 HTTP GET 请求的时间,而不需要解析响应所需的时间。所以只是远程 HTTP 调用需要的时间。我已经尝试设置ClientHttpRequestInterceptor,但我认为这不是正确的方法,因为时间似乎是错误的:
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 请求的时间?
【问题讨论】:
-
我遇到了同样的问题。您需要对响应对象进行方法调用以阻止执行,直到响应可用为止。
标签: java performance web-services spring http