我正在尝试对使用 HttpClient
版本 4.5
的类编写测试。我写的测试真的很慢,所以我试图找出问题所在。我可以编写一个测试来证明我的问题:
public class RequeteurRESTTest extends LocalServerTestBase {
private String startServer(String urlSuffix, HttpRequestHandler handler) throws Exception{
this.serverBootstrap.registerHandler(urlSuffix, handler);
HttpHost target = start();
String serverUrl = "http://localhost:" + target.getPort();
return serverUrl;
}
@Test
public void testCase() throws Exception{
String baseURL = startServer("/api", new HttpRequestHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
response.setStatusCode(HttpStatus.SC_OK);
}
});
HttpClient httpClient;
httpClient = HttpClients.custom().build();
HttpGet method = new HttpGet(baseURL + "/api");
HttpResponse response = httpClient.execute(method);
}
}
我遇到的问题是指令:
httpClient.execute(method)
执行需要10 秒。这很慢,但我不明白为什么这么慢。我在测试中是否犯了任何错误,或者忘记了什么?
最佳答案
这很可能是因为您未对响应对象执行任何操作。至少应该关闭它。
关于performance - HttpClient 和 LocalServerTestBase 真的很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206739/