原創文章,轉載請指明出處:http://aub.iteye.com/blog/2124974, 尊重他人即尊重自己
1.避免垃圾回收對結果造成的誤差
方案一:JVM啓動時使用-verbose:gc觀察垃圾回收動作,確認整個測試期間垃圾回收根本不會執行
方案二:運行足夠的次數和時間,這樣測試程序能夠充分的反應出運行期間分配與垃圾回收的開銷(推薦)。
2.避免動態編譯對結果造成的誤差
方案一:讓測試程序長時間運行,讓編譯過程和解釋執行僅僅佔總體運行時間的一小部分。
方案二:讓測試代碼“熱身”,充分的執行,這樣開始計時前,代碼已經被編譯了(JVM啓動時使用-xx:PrintCompilation觀察是否有編譯動作)。
下面是一個多線程性能測試的例子:
package self.study;
import java.util.concurrent.CountDownLatch;
public class TestHarness {
public static void main(String[] args) throws InterruptedException {
TestHarness testHarness = new TestHarness();
long timeTasks = testHarness.timeTasks(10, new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println(timeTasks);
}
public long timeTasks(int nThreads, final Runnable task) throws InterruptedException {
//預熱,編譯
for (int i = 0; i < 10000; i++) {
task.run();
}
// 真正的測試
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(nThreads);
for (int i = 0; i < nThreads; i++) {
Thread t = new Thread() {
@Override
public void run() {
try {
startGate.await();
try {
task.run();
} finally {
endGate.countDown();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
long start = System.currentTimeMillis();
startGate.countDown();
endGate.await();
long end = System.currentTimeMillis();
return end - start;
}
}