JMH(Java Microbenchmark Harness)是一個(gè)專門用于編寫、運(yùn)行和分析Java微基準(zhǔn)測(cè)試的工具。它是由OpenJDK項(xiàng)目提供的一個(gè)開源項(xiàng)目,旨在幫助開發(fā)人員準(zhǔn)確地測(cè)量和評(píng)估Java代碼的性能。
JMH提供了一組注解和API,使得編寫微基準(zhǔn)測(cè)試變得簡(jiǎn)單和方便。使用JMH,您可以定義和運(yùn)行各種類型的基準(zhǔn)測(cè)試,包括方法級(jí)別的基準(zhǔn)測(cè)試、類級(jí)別的基準(zhǔn)測(cè)試和多線程基準(zhǔn)測(cè)試。JMH還提供了豐富的選項(xiàng)和配置,以控制基準(zhǔn)測(cè)試的執(zhí)行方式,如迭代次數(shù)、線程數(shù)、預(yù)熱時(shí)間等。
JMH的主要特點(diǎn)包括:
使用JMH編寫和運(yùn)行基準(zhǔn)測(cè)試的一般步驟包括:
jmh提供了大量的測(cè)試用例,參考資料【1】所示。
@BenchmarkMode(Mode.AverageTime)@OutputTimeUnit(TimeUnit.NANOSECONDS)public class JMHSample_21_ConsumeCPU { /* * At times you require the test to burn some of the cycles doing nothing. * In many cases, you *do* want to burn the cycles instead of waiting. * * For these occasions, we have the infrastructure support. Blackholes * can not only consume the values, but also the time! Run this test * to get familiar with this part of JMH. * * (Note we use static method because most of the use cases are deep * within the testing code, and propagating blackholes is tedious). */ @Benchmark public void consume_0000() { Blackhole.consumeCPU(0); } @Benchmark public void consume_0001() { Blackhole.consumeCPU(1); } @Benchmark public void consume_0002() { Blackhole.consumeCPU(2); } @Benchmark public void consume_0004() { Blackhole.consumeCPU(4); } @Benchmark public void consume_0008() { Blackhole.consumeCPU(8); } @Benchmark public void consume_0016() { Blackhole.consumeCPU(16); } @Benchmark public void consume_0032() { Blackhole.consumeCPU(32); } @Benchmark public void consume_0064() { Blackhole.consumeCPU(64); } @Benchmark public void consume_0128() { Blackhole.consumeCPU(128); } @Benchmark public void consume_0256() { Blackhole.consumeCPU(256); } @Benchmark public void consume_0512() { Blackhole.consumeCPU(512); } @Benchmark public void consume_1024() { Blackhole.consumeCPU(1024); } /* * ============================== HOW TO RUN THIS TEST: ==================================== * * Note the single token is just a few cycles, and the more tokens * you request, then more work is spent (almost linearly) * * You can run this test: * * a) Via the command line: * $ mvn clean install * $ java -jar target/benchmarks.jar JMHSample_21 -f 1 * (we requested single fork; there are also other options, see -h) * * b) Via the Java API: * (see the JMH homepage for possible caveats when running from IDE: * http://openjdk.java/projects/code-tools/jmh/) */ public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(JMHSample_21_ConsumeCPU.class.getSimpleName()) .forks(1) .build(); new Runner(opt).run(); }}
JMH是一個(gè)強(qiáng)大的工具,可以幫助開發(fā)人員進(jìn)行準(zhǔn)確和可靠的Java微基準(zhǔn)測(cè)試,以評(píng)估和改進(jìn)代碼的性能。它廣泛應(yīng)用于Java開發(fā)社區(qū),并被認(rèn)為是Java性能測(cè)試領(lǐng)域的事實(shí)標(biāo)準(zhǔn)。
【1】https://github.com/openjdk/jmh/tree/master/jmh-samples/src/main/java/org/openjdk/jmh/samples。
本文鏈接:http://www.tebozhan.com/showinfo-26-14626-0.htmlOpenJDK JMH——Java程序的基準(zhǔn)測(cè)試工具
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com