java测试netty_《Netty官方文档》基准测试
原文鏈接? 譯者:lijunshu
Netty有一個模塊叫’netty-microbench’,我們可以用他來執行一系列的微型基準測試。Netty-microbench是基于OpenJDK JMH構件的(HotSpot的推薦基準測試方案)。當你開始netty基準測試時,你不需要額外的依賴。
運行基準測試
你可以通過maven 命令行或者直接從IDE開始你的基準測試,如果以默認設置開始跑測試,你可以使用命令行mvn -DskipTests=false。我們設置skipTests=false?是因為我們不想以單元測試的方式去運行這些基準測試。
如果沒有問題,你將為看到JMH開始warmup然后基于fork數量來運行基準測試,并提供給你漂亮的報告。你會看到一般情況下基準測試的結果如下。
# Fork: 2 of 2
# Warmup: 10 iterations, 1 s each
# Measurement: 10 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Running: io.netty.microbench.buffer.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_1_0
# Warmup Iteration?? 1: 8454.103 ops/ms
# Warmup Iteration?? 2: 11551.524 ops/ms
# Warmup Iteration?? 3: 11677.575 ops/ms
# Warmup Iteration?? 4: 11404.954 ops/ms
# Warmup Iteration?? 5: 11553.299 ops/ms
# Warmup Iteration?? 6: 11514.766 ops/ms
# Warmup Iteration?? 7: 11661.768 ops/ms
# Warmup Iteration?? 8: 11667.577 ops/ms
# Warmup Iteration?? 9: 11551.240 ops/ms
# Warmup Iteration? 10: 11692.991 ops/ms
Iteration?? 1: 11633.877 ops/ms
Iteration?? 2: 11740.063 ops/ms
Iteration?? 3: 11751.798 ops/ms
Iteration?? 4: 11260.071 ops/ms
Iteration?? 5: 11461.010 ops/ms
Iteration?? 6: 11642.912 ops/ms
Iteration?? 7: 11808.595 ops/ms
Iteration?? 8: 11683.780 ops/ms
Iteration?? 9: 11750.292 ops/ms
Iteration? 10: 11769.986 ops/ms
Result : 11650.238 ±(99.9%) 229.698 ops/ms
Statistics: (min, avg, max) = (11260.071, 11650.238, 11808.595), stdev = 169.080
Confidence interval (99.9%): [11420.540, 11879.937]
最終,你的測試結果看上去和這個相似,這個更多的取決于你的系統配置。
Benchmark??????????????????????????????????????????????????????????????? Mode?? Samples???????? Mean? ?Mean error??? Units
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_1_0????????? thrpt??????? 20??? 11658.812????? 120.728?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_2_256??????? thrpt??????? 20??? 10308.626????? 147.528?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_3_1024?????? thrpt??????? 20???? 8855.815?????? 55.933?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_4_4096?????? thrpt??????? 20???? 5545.538???? 1279.721?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_5_16384????? thrpt??????? 20???? 6741.581?????? 75.975?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_6_65536????? thrpt??????? 20???? 7252.869?????? 70.609?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_1_0??????????? thrpt??????? 20???? 9750.225?????? 73.900?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_2_256????????? thrpt??????? 20???? 9936.639????? 657.818?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_3_1024???????? thrpt??????? 20???? 8903.130????? 197.533?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_4_4096???????? thrpt??????? 20???? 6664.157?????? 74.163?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_5_16384??????? thrpt??????? 20???? 6374.924????? 337.869?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_6_65536??????? thrpt??????? 20???? 6386.337?????? 44.960?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_1_0??????? thrpt??????? 20???? 2137.241?????? 30.792?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_2_256????? thrpt??????? 20???? 1873.727?????? 41.843?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_3_1024???? thrpt??????? 20???? 1902.025?????? 34.473?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_4_4096???? thrpt??????? 20???? 1534.347?????? 20.509?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_5_16384??? thrpt??????? 20????? 838.804?????? 12.575?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_6_65536??? thrpt??????? 20????? 276.976??????? 3.021?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_1_0?????? ???thrpt??????? 20??? 35820.568????? 259.187?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_2_256??????? thrpt??????? 20??? 19660.951????? 295.012?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_3_1024?????? thrpt? ??????20???? 6264.614?????? 77.704?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_4_4096?????? thrpt??????? 20???? 2921.598?????? 95.492?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_5_16384????? thrpt??????? 20? ????991.631?????? 49.220?? ops/ms
i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_6_65536????? thrpt??????? 20????? 261.718?????? 11.108?? ops/ms
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 993.382 sec - in io.netty.microbench.buffer.ByteBufAllocatorBenchmark
你也可以直接從IDE運行基準測試。如果你導入了netty的最頂層項目,打開子項目microbench到子目錄src/test/java/io/netty/microbench?。在buffer子目錄,你可以像其它JUnit測試一樣運行ByteBufAllocatorBenchmark?。主要的區別就是,你可以一下子運行全部的基準測試,而不是去一個個的運行每一個基準測試。如果maven一樣,你可以在控制臺看到同樣的輸出。
編寫基準測試
雖然編寫基準測試不是太麻煩,但是卻不一定編寫的正確。這不是因為microbench項目很難使用,而是因為你很難去避免一些常用的陷阱。因此,JMH提供了有用的annotation和特性來幫助你避免大多數的陷阱。為此,你需要使你的基準繼承自AbstractMicrobenchmark,AbstractMicrobenchmark能保證以JUnit默認參賽的方式運行。
public class MyBenchmark extends AbstractMicrobenchmark {
}
下一步是創建一個方法以@GenerateMicroBenchmark?標注,然后給他一個合適的方法名。
@GenerateMicroBenchmark
public void measureSomethingHere() {
}
看這些樣例是很好的主意能啟發你如何去寫JMH測試。你也可以看下JMH主要作者的一些座談。
定制運行參數
默認的AbstractMicrobenchmark配置是
Warmup次數 10
測試次數 10
Fork數量 2
這些配置可以通過系統配置在運行的時候做配置
mvn -DskipTests=false -DwarmupIterations=2 -DmeasureIterations=3 -Dforks=1 test
需要注意通常并不建議跑測試時,用較少的循環次數,但是較少的次數有助于確認基準測試時工作的,在確認結束后,再運行大量的基準測試。
@Warmup(iterations = 20)
@Fork(1)
public class MyBenchmark extends AbstractMicrobenchmark {
}
這可以以方法級別或者類級別來運行基準測試,命令行的參數會覆蓋annotation上的參數。
總結
以上是生活随笔為你收集整理的java测试netty_《Netty官方文档》基准测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英伟达在人工智能技术领域的领导地位和近乎
- 下一篇: 迪士尼公布第三财季财报 营业收入达223