简体   繁体   English

HttpCore用于测量HTTP请求/响应经过的时间

[英]HttpCore for measuring http request/response elapsed time

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. 我有一个基于Java Apache HttpCore 4的小型客户端类,该客户端类对服务进行了调用,我想测量http请求/响应往返的响应时间。 I thought there would be a way to read it from the HttpResponse object's meta data. 我认为会有一种方法可以从HttpResponse对象的元数据中读取它。 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);

for eg, I get the following elapsed time: Elapsed time -->1561599815 例如,我得到以下经过时间:经过时间-> 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   |

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. 上面的解决方案在异步非阻塞代码中特别难看,在该代码中,我必须通过匿名内部函数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

So this code is a not elegant. 因此,这段代码并不完美。 I wanted to get the response object to give me the elapsed time of http traffic. 我想获取响应对象,以提供HTTP流量经过的时间。 How can I do that? 我怎样才能做到这一点?

I'm using httpClient 4.2, httpCore-nio 4.2, httpasyncclient 4.0 beta. 我正在使用httpClient 4.2,httpCore-nio 4.2,httpasyncclient 4.0 beta。

In the blocking i/o mode request execution (or processing) is terminated immediately after receiving a message head (request line + headers). 在阻塞I / O模式中,请求执行(或处理)在接收到消息头(请求行+标头)后立即终止。 Message body, when available, is associated with the content entity as an InputStream instance. 消息正文(如果可用)与内容实体相关联,作为InputStream实例。 This enables the consumer to stream the content directly from the underlying network socket. 这使使用者可以直接从底层网络套接字流式传输内容。 So, all you have to is to ensure that the response content entity is fully consumed. 因此,您要做的就是确保响应内容实体被完全消耗。

    long start = System.nanoTime();
    HttpResponse httpResponse = httpexecutor.execute(request, conn, context);
    EntityUtils.consume(httpResponse.getEntity());
    long elapsed = System.nanoTime() - start;

So this code is a not elegant. 因此,这段代码并不完美。 I wanted to get the response object to give me the elapsed time of http traffic. 我想获取响应对象,以提供HTTP流量经过的时间。 How can I do that? 我怎样才能做到这一点?

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. 但是您也对网络流量感兴趣,因此无论如何该方法都不适合您。

  • If you only need this time measurement once, leave the code as it is. 如果您只需要一次此时间测量,则保持原样。 It does not look very nice, but this is the usual way how callbacks look like in Java. 它看起来不太好,但这是Java中回调外观的通常方式。
  • If you need it more than once, you can create a helper method which sends the request and does the time measurement for you. 如果您不止一次需要它,则可以创建一个帮助程序方法,该方法发送请求并为您进行时间测量。 This way you can keep it at one place. 这样,您可以将其保存在一个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM