我使用Apache HttpClient 4.1进行简单的1线程循环。它连接到localhost上的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);
}
}
答案 0 :(得分:2)
我的测试显示不然,请以stackoverflow.com GET请求为例超过10次重复:
正如您在图像中看到(或不看)时,使用带View Results in a Table
的jmeter时平均时间为712ms。请注意,此侦听器不会仅打印请求统计信息的响应正文。
这是我的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 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
时,使用View Results in a Table
时jmeter中的另一个案例,因为我们仍然需要时间。
我不打算附上截图,因为答案的可读性会降低,但这段时间的平均时间为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是非常棒的工具,是最好的工具之一(免费)。