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

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

问题描述

我有一个使用 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 毫秒.(Apache 基准测试 ab 在 0.4 毫秒内完成,但由于这是本机代码,因此可能无法进行比较.)

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 :

正如您从图像中看到的(或没有看到的),当使用带有 View Results in a Table 的 jmeter 时,平均时间为 712 毫秒.请注意,此侦听器不会仅打印请求统计信息的响应正文.

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

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

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 毫秒,比以前多约 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).

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

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