使用Java创建快速/可靠的基准测试?
|
我正在尝试使用Java创建基准测试。目前,我有以下简单方法:
public static long runTest(int times){
long start = System.nanoTime();
String str = \"str\";
for(int i=0; i<times; i++){
str = \"str\"+i;
}
return System.nanoTime()-start;
}
我当前在另一个循环中多次发生此循环,该循环发生了多次,并获得了运行此方法所需的最小/最大/平均时间。然后,我在另一个线程上开始一些活动,然后再次进行测试。基本上,我只是想获得一致的结果...如果我有1000万次runTest循环,这似乎很一致:
Number of times ran: 5
The max time was: 1231419504 (102.85% of the average)
The min time was: 1177508466 (98.35% of the average)
The average time was: 1197291937
The difference between the max and min is: 4.58%
Activated thread activity.
Number of times ran: 5
The max time was: 3872724739 (100.82% of the average)
The min time was: 3804827995 (99.05% of the average)
The average time was: 3841216849
The difference between the max and min is: 1.78%
Running with thread activity took 320.83% as much time as running without.
但这似乎有点多,并且要花一些时间...如果我在runTest循环中尝试使用较小的数字(100000),则会变得非常不一致:
Number of times ran: 5
The max time was: 34726168 (143.01% of the average)
The min time was: 20889055 (86.02% of the average)
The average time was: 24283026
The difference between the max and min is: 66.24%
Activated thread activity.
Number of times ran: 5
The max time was: 143950627 (148.83% of the average)
The min time was: 64780554 (66.98% of the average)
The average time was: 96719589
The difference between the max and min is: 122.21%
Running with thread activity took 398.3% as much time as running without.
有没有办法我可以做一个这样的基准,它既一致又高效/快速?
顺便说一下,我没有测试开始时间和结束时间之间的代码。我正在以某种方式测试CPU负载(请参阅我如何开始一些线程活动并重新测试)。因此,我认为我正在寻找的东西可以替代“ runTest”中的代码,从而产生更快,更一致的结果。
谢谢
没有找到相关结果
已邀请:
3 个回复
际恃啸称桅
嘘崇蔡对
犀耽澄协吻