如何测量用Java 11标准HTTPClient发送的ASYNC HTTPRequest的经过时间?

问题描述 投票:0回答:1

在一个方法中,我创建并发送了一些异步发送的HTTPRequests,并将CF添加到一个列表中。

// HTTPClient is Java11 standard, package java.net.http;
CompletableFuture<HttpResponse<String>> httpResponseCF = this.httpClient.sendAsync(httpRequest, BodyHandlers.ofString());
list.add(httpResponseCF);

之后,在另一个方法中,我每隔1秒定期检查我的一些异步发送的HTTPRequests是否已经收到。

for(CompletableFuture<HttpResponse<String>> httpResponseCF : list){
  if (httpResponseCF.isDone()) {
    //print out: The time between sending HTTPRequest and receiving HTTPResponse is 25ms
  }
}

问题是,如何测量精确的请求时间?

java asynchronous time httprequest httpclient
1个回答
0
投票

类似下面的东西应该可以做到。

long start = System.nanoTime();
CompletableFuture<HttpResponse<String>> httpResponseCF = 
    this.httpClient.sendAsync(httpRequest, BodyHandlers.ofString()).
    whenComplete((r,t) -> System.out.println("elapsed ns: "
                           + (System.nanoTime() - start)));

声明:我没有实际编译过上面的内容......这也不是我的作品。确切时间 但却是你能得到的最好的近似值。就像所有的事情一样 确切时间 是无法测量的。

© www.soinside.com 2019 - 2024. All rights reserved.