微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 测量Spring RestTemplate HTTP请求时间

我想测量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

如何衡量RestTemplate HTTP请求的时间?

最佳答案
您可以使用AOP和Spring内置的PerformanceMonitorInterceptor.您需要正确定义要拦截哪种calss的方法,然后才能进行测量.您可以配置如下:

spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    fig>
            pointcut id="springMonitoringpointcut"
                   expression="execution(* java.net.HttpURLConnection.connect(..))"/>




                pointcut-ref="springMonitoringpointcut" 
            advice-ref="springMonitoringAspectInterceptor"/>      
    fig>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐