HttpClient和LocalServerTestBase非常慢

时间:2016-05-13 09:49:19

标签: performance testing apache-httpclient-4.x

我正在尝试在使用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秒。这很慢,但我无法弄清楚为什么这么慢。我在测试中是否犯过任何错误,或者忘记了什么?

3 个答案:

答案 0 :(得分:1)

如果查看LocalServerTestBase类的源代码,可以看到以下方法:

@After
public void shutDown() throws Exception {
    if(this.httpclient != null) {
        this.httpclient.close();
    }

    if(this.server != null) {
        this.server.shutdown(10L, TimeUnit.SECONDS);
    }

}

通过在自己的测试用例中覆盖它,您可以将超时设置为0秒。这应该摆脱你在执行中看到的延迟。

示例实施:

public class MyClassTest extends LocalServerTestBase{
    HttpHost httpHost;
    @Before
    public void setUp() throws Exception {
      super.setUp();
      this.serverBootstrap.registerHandler("/*", new HttpRequestHandler() {
        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) 
                          throws HttpException, IOException {
            System.out.println(request.getRequestLine().getMethod());
            response.setStatusCode(HttpStatus.SC_OK);                
        }

      });

      httpHost = start();

    }

    @Test
    public void myMethodTest() throws Exception {
        //Create an instance and give the server url it should call
        MyClass instance = new MyClass(httpHost.toURI());
        //Test method that calls the server
        instance.myMethod();
    }

    @After @Override
    public void shutDown() throws Exception {
     if(this.httpclient != null) {
        this.httpclient.close();
     }
    if(this.server != null) {
        this.server.shutdown(0L, TimeUnit.SECONDS);
    }
   }

}

答案 1 :(得分:0)

这可能是因为您对响应对象不执行任何操作。至少应该关闭它。

答案 2 :(得分:0)

由于我无法理解的原因,使用基类的httpclient工作正常,如下所示:

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

    HttpGet method = new HttpGet(baseURL + "/api");
    HttpResponse response = this.httpclient.execute(method);

}