与JMeter相比,Apache httpclient 4.1慢
我有一个使用Apache HttpClient 4.1的简单1线程循环。它连接到我在本地主机上的Apache httpd Web服务器。
我平均每个请求/响应2.5毫秒。另一方面,JMeter平均为1 ms。(Apache
Benchmark,ab,在0.4ms内完成,但由于这是本机代码,因此可能无法进行比较。)
代码只是:
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);
}
}
回答:
如您所见(或未从图像中看到),将jmeter与一起使用时,平均时间为712ms View Results in a
Table。请注意,此侦听器不会仅输出请求统计信息,而仅显示响应主体。
这是我的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));
}
因此,我也不关心响应主体。但是这里是结果(更快):
Duration 615 msDuration 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因为我们仍然需要时间。
我不会附上屏幕截图,因为答案将变得难以理解,但是这次的平均时间812 ms
比以前多了100毫秒。
现在,关心响应主体的Java代码(新方法):
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行为,并发布相关部分:
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确实是很棒的工具,是最好的工具之一(免费)。
以上是 与JMeter相比,Apache httpclient 4.1慢 的全部内容, 来源链接: utcz.com/qa/406907.html