与JMeter相比,Apache httpclient 4.1的速度较慢 [英] Slow Apache httpclient 4.1 compared to JMeter

查看:247
本文介绍了与JMeter相比,Apache httpclient 4.1的速度较慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Apache HttpClient 4.1的简单1线程循环.它连接到我在本地主机上的Apache httpd Web服务器.

I have a simple 1 thread loop using Apache HttpClient 4.1. It connects to my Apache httpd web server on localhost.

我平均每个请求/响应2.5毫秒.另一方面,JMeter平均为1 ms. (Apache Benchmark,ab,在0.4ms内完成操作,但由于这是本机代码,因此可能无法进行比较.)

I'm averaging 2.5 ms per request/response. JMeter, on the other hand, averages 1 ms. (Apache Benchmark, ab, does it in 0.4ms but since that is native code perhaps there is no comparison.)

代码就是:

    final HttpGet httpGet = new HttpGet(testUrl);
    while (true) {
        try {
            final long startNanoTime = System.nanoTime();
            final HttpResponse httpResponse = httpClient.execute(httpGet); 
            final InputStream inputStream = httpResponse.getEntity().getContent();

            final byte[] buffer = new byte[8192];
            int size = inputStream.read(buffer);
            while (size > 0) {
                size = inputStream.read(buffer);
            }

                            // Elapsed time measured here
            final long elapsed = System.nanoTime() - startNanoTime;

            inputStream.close();

        } catch (MalformedURLException e) {
            // Should never happen
            throw new RuntimeException(e);
        } catch (IOException e) {
            // Count
            errors++;
            throw new RuntimeException(e);
        }
    }

推荐答案

我的测试显示否则,以stackoverflow.com GET请求为例,该请求超过10次重复:

My testing shows otherwise, take stackoverflow.com GET request for example over 10 repetitions :

从图像中可以看到(或没有看到),将jmeter与View Results in a Table一起使用时,平均时间为712ms.请注意,此侦听器不会仅输出请求统计信息,而仅输出响应主体.

As you may see (or not) from the image the average time is 712ms when using jmeter with View Results in a Table. Notice that this listener doesn't print out the response body just the request stats.

这是我的Java代码:

And here is my code from Java :

public static void main(String[] args) throws Exception{        
        long totalTimeMS = 0;

        for (int i = 0; i < 10; i++) {

        long startTime = System.currentTimeMillis();


        HttpGet get = new HttpGet("http://stackoverflow.com");
        HttpClient client = new DefaultHttpClient();
        client.execute(get);       

        long endTime = System.currentTimeMillis();
        long duration = (endTime-startTime);
        totalTimeMS +=duration;
        System.out.format("Duration %d ms\n", duration);
        }

        System.out.format("Average time is %d ms", (totalTimeMS/10));
    }

因此,我也不关心响应主体.但是这里是结果(更快):

So I do not care about the response body as well. But here are the results (lot faster) :

Duration 615 ms
Duration 263 ms
Duration 264 ms
Duration 262 ms
Duration 268 ms
Duration 266 ms
Duration 265 ms
Duration 266 ms
Duration 268 ms
Duration 263 ms
Average time is 300 ms

在使用View Results in a Tree的jmeter中,当您实际上可以看到响应主体加上View Results in a Table时,因为我们仍然需要时间,因此在jmeter中还有另一种情况.

Now another case in jmeter when using View Results in a Tree when you can actually see the response body plus the View Results in a Table because we still need the time.

我将不附加屏幕截图,因为答案将变得难以理解,但是这次的平均时间为812 ms,因此比以前多了100毫秒.

I'm not going to attach screenshot because the answer will become less readable but the average time this time is 812 ms so about 100ms more than previously.

现在,关心响应主体的Java代码(新方法):

Now the java code that cares about the response body (new method) :

public static String convertStreamToString(InputStream is) throws IOException {
        if (is != null) {
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
            } finally {
                is.close();
            }
            return sb.toString();
        } else {
            return "";
        }
    }

我已经稍微修改了前面的代码,因此它可以打印出响应,模拟jmeter行为,并发布相关部分:

And I've modified slightly the previous code so it prints out the response, simulating the jmeter behavior, posting relevant part :

HttpGet get = new HttpGet("http://stackoverflow.com");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);       

        long endTime = System.currentTimeMillis();
        long duration = (endTime-startTime);
        totalTimeMS +=duration;
        System.out.println(convertStreamToString(response.getEntity().getContent()));
        System.out.format("Duration %d ms\n", duration);

结果如下:

Duration 678 ms  + (including printing of response body)
Duration 264 ms + (including printing of response body)
Duration 269 ms + (including printing of response body)
Duration 262 ms + (including printing of response body)
Duration 263 ms + (including printing of response body)
Duration 265 ms + (including printing of response body)
Duration 262 ms + (including printing of response body)
Duration 267 ms + (including printing of response body)
Duration 264 ms + (including printing of response body)
Duration 264 ms + (including printing of response body)
Average time is 305 ms

响应时间增加了5 ms.所以我不知道jmeter如何比普通的Java代码更快.无论如何,jmeter确实是很棒的工具,是最好的工具之一(免费).

Reponse time went up by 5 ms. So I don't know how jmeter got to be faster than just plain java code. Anyhow jmeter is really great tool, one of the best one arround (for free).

这篇关于与JMeter相比,Apache httpclient 4.1的速度较慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆