HttpCore for measuring http request/response elaps

2019-08-03 23:40发布

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);

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   |

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

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?

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

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-04 00:12

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?

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.
  • 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.
查看更多
Melony?
3楼-- · 2019-08-04 00:29

In the blocking i/o mode request execution (or processing) is terminated immediately after receiving a message head (request line + headers). Message body, when available, is associated with the content entity as an InputStream instance. 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;
查看更多
登录 后发表回答