3

I am using Jetty Client 11 and would like to compute for each request the sent/received bytes.

I am using High Level HttpClient . I have read this documentation but I don't see any information on how to do that:

I see there is a way to have this information by doing:

        ConnectionStatistics connectionStatistics = new ConnectionStatistics();
        httpClient.addBean(connectionStatistics);

Then:

        Request request = httpClient.newRequest(uri)
                .version(HttpVersion.HTTP_2)
                .timeout(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        request.send(listener);
        ConnectionStatistics connectionStatistics = httpClient.getBean(ConnectionStatistics.class);
        System.out.println(connectionStatistics.getSentBytes()+"/"+ connectionStatistics.getReceivedBytes());

And I must be doing something wrong as it does not even work on a global basis, it always gives 0.

And anyway, I don't see how to make it work on a request basis.

EDIT:

I tried @sbordet answer:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);

and it works fine for response. BUT for request, I would like to measure the bytes sent, it always give 0 (Note I only have gets without Content) while I would expect to have the raw size (the Raw HTTP request bytes including header size ...)

pmpm
  • 705
  • 1
  • 5
  • 20

1 Answers1

5

You can count request bytes using Request.ContentListener and response bytes using Response.ContentListener:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);
sbordet
  • 16,856
  • 1
  • 50
  • 45
  • Thanks, if the response is gzipped, is it before or after decompression ? – pmpm Mar 12 '21 at 21:11
  • It works fine for response, but for request, I would like to measure the bytes sent, it always give 0 (Note I only have gets without Content) while I would expect to have the raw size – pmpm Mar 12 '21 at 21:30
  • Hello @sbordet, I updated my question with my comment, it would be nice if you could have a look. Thanks – pmpm Mar 15 '21 at 09:50