HttpCore用于测量http请求/响应经过的时间 [英] HttpCore for measuring http request/response elapsed time

查看:96
本文介绍了HttpCore用于测量http请求/响应经过的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Java Apache HttpCore 4的小型客户端类,可以调用服务,我想测量http请求/响应往返的响应时间。我认为有一种方法可以从HttpResponse对象的元数据中读取它。但是我无法在响应对象中获得响应时间。所以,我的解决方案是在拨打电话之前存储时间,然后测量拨打电话后的时间,差异是经过的时间。

I've a small Java Apache HttpCore 4 based client class that makes calls to a service and I wanted to measure the response time of the http request/response round-trip. I thought there would be a way to read it from the HttpResponse object's meta data. But I was not able to get the response time in the response object. So, my solution was I stored the time before making a call then measured the time after making a call, and the difference is the elapsed time .

      BasicHttpRequest request = new BasicHttpRequest("GET", target);
      request.setParams(params);
      httpexecutor.preProcess(request, httpproc, context);

      long start = System.nanoTime();
      HttpResponse httpResponse = httpexecutor.execute(request, conn, context);
      long elapsed = System.nanoTime() - start;
      System.out.println("Elapsed nano seconds -->" + elapsed);

      httpResponse.setParams(params);
      httpexecutor.postProcess(httpResponse, httpproc, context);

例如,我得到以下经过的时间:
经过时间 - > 1561599815

for eg, I get the following elapsed time: Elapsed time -->1561599815

我可以使用响应对象中的以下值读取以下标题,但我找不到与响应时间相关的任何内容:

And I could read the following headers with the following values from the response object, but I couldn't find anything related to response time:

|   Server --> Apache-Coyote/1.1   |
|   Date --> Mon, 26 Aug 2013 19:22:00 GMT   |
|   Content-Type --> application/json   |
|   Transfer-Encoding --> chunked   |

以上解决方案在异步非阻塞代码中特别难看,我必须进行回调函数调用通过匿名内部函数FutureCallback。像这样:

The above solution is ugly specially in the asynchronous non-blocking code where I had to make callback function calls by anonymous inner function FutureCallback. like this:

requester.execute(new BasicAsyncRequestProducer(target, request),
          new BasicAsyncResponseConsumer(), pool,
          new BasicHttpContext(),
        // Handle HTTP response from a callback
                new FutureCallback<HttpResponse>() {
                                        private int startTime; ...//etc

所以这段代码不太优雅。我想得到一个响应对象来给我一个http流量的经过时间我怎么能这样做?

So this code is a not elegant. I wanted to get the response object to give me the elapsed time of http traffic. How can I do that?

我正在使用httpClient 4.2,httpCore-nio 4.2,httpasyncclient 4.0 beta。

I'm using httpClient 4.2, httpCore-nio 4.2, httpasyncclient 4.0 beta.

推荐答案


所以这段代码不太优雅。我想得到响应对象,告诉我http流量的经过时间。我怎么能这样做?

So this code is a not elegant. I wanted to get the response object to give me the elapsed time of http traffic. How can I do that?

响应是从收到您请求的服务器发送的。因此,服务器唯一可以森d您是处理请求所需的时间。但是你也对网络流量感兴趣,所以这种方法无论如何都不适合你。

The response is sent from the server which received your request. So, the only time the server could send you is the time it needed for processing the request. But you are interested in the network traffic as well, so this approach will not be suitable for you anyway.


  • 如果你只需要这个时间测量一次,保持代码不变。它看起来并不是很好,但这是回调在Java中的常见方式。

  • 如果您不止一次需要它,您可以创建一个发送请求的辅助方法,为你做时间测量。这样你可以把它放在一个地方。

这篇关于HttpCore用于测量http请求/响应经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆