Java Benchmark 基準(zhǔn)測試的實例詳解
成都創(chuàng)新互聯(lián)公司主營平山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),平山h5重慶小程序開發(fā)公司搭建,平山網(wǎng)站營銷推廣歡迎平山等地區(qū)企業(yè)咨詢
import java.util.Arrays; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Threads; import org.openjdk.jmh.annotations.Warmup; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; @BenchmarkMode(Mode.Throughput)//基準(zhǔn)測試類型 @OutputTimeUnit(TimeUnit.SECONDS)//基準(zhǔn)測試結(jié)果的時間類型 @Warmup(iterations = 3)//預(yù)熱的迭代次數(shù) @Threads(2)//測試線程數(shù)量 @State(Scope.Thread)//該狀態(tài)為每個線程獨享 //度量:iterations進(jìn)行測試的輪次,time每輪進(jìn)行的時長,timeUnit時長單位,batchSize批次數(shù)量 @Measurement(iterations = 2, time = -1, timeUnit = TimeUnit.SECONDS, batchSize = -1) public class InstructionsBenchmark{ static int staticPos = 0; //String src = "SELECT a FROM ab , ee.ff AS f,(SELECT a FROM `schema_bb`.`tbl_bb`,(SELECT a FROM ccc AS c, `dddd`));"; final byte[] srcBytes = {83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 97, 98, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 44, 32, 101, 101, 46, 102, 102, 32, 65, 83, 32, 102, 44, 40, 83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 96, 115, 99, 104, 101, 109, 97, 95, 98, 98, 96, 46, 96, 116, 98, 108, 95, 98, 98, 96, 44, 40, 83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 99, 99, 99, 32, 65, 83, 32, 99, 44, 32, 96, 100, 100, 100, 100, 96, 41, 41, 59}; int len = srcBytes.length; byte[] array = new byte[8192]; int memberVariable = 0; //run public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(InstructionsBenchmark.class.getSimpleName()) .forks(1) // 使用之前要安裝hsdis //-XX:-TieredCompilation 關(guān)閉分層優(yōu)化 -server //-XX:+LogCompilation 運行之后項目路徑會出現(xiàn)按照測試順序輸出hotspot_pid<PID>.log文件,可以使用JITWatch進(jìn)行分析,可以根據(jù)最后運行的結(jié)果的順序按文件時間找到對應(yīng)的hotspot_pid<PID>.log文件 .jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+LogCompilation", "-XX:+TraceClassLoading", "-XX:+PrintAssembly") // .addProfiler(CompilerProfiler.class) // report JIT compiler profiling via standard MBeans // .addProfiler(GCProfiler.class) // report GC time // .addProfiler(StackProfiler.class) // report method stack execution profile // .addProfiler(PausesProfiler.class) /* WinPerfAsmProfiler You must install Windows Performance Toolkit. Once installed, locate directory with xperf.exe file and either add it to PATH environment variable, or set it to jmh.perfasm.xperf.dir system property. */ //.addProfiler(WinPerfAsmProfiler.class) //更多Profiler,請看JMH介紹 //.output("InstructionsBenchmark.log")//輸出信息到文件 .build(); new Runner(opt).run(); } //空循環(huán) 對照項 @Benchmark public int emptyLoop() { int pos = 0; while (pos < len) { ++pos; } return pos; } @Benchmark public int increment() { int pos = 0; int result = 0; while (pos < len) { ++result; ++pos; } return result; } @Benchmark public int decrement() { int pos = 0; int result = 0; while (pos < len) { --result; ++pos; } return result; } @Benchmark public int ifElse() { int pos = 0; int result = 0; while (pos < len) { if (pos == 10) { ++result; ++pos; } else { ++pos; } } return result; } @Benchmark public int ifElse2() { int pos = 0; int result = 0; while (pos < len) { if (pos == 10) { ++result; ++pos; } else if (pos == 20) { ++result; ++pos; } else { ++pos; } } return result; } @Benchmark public int ifnotElse() { int pos = 0; int result = 0; while (pos < len) { if (pos != 10) { ++pos; } else { ++result; ++pos; } } return result; } @Benchmark public int ifLessthanElse() { int pos = 0; int result = 0; while (pos < len) { if (pos < 10) { ++pos; } else { ++result; ++pos; } } return result; } @Benchmark public int ifGreaterthanElse() { int pos = 0; int result = 0; while (pos < len) { if (pos > 10) { ++pos; } else { ++result; ++pos; } } return result; } @Benchmark public int readMemberVariable_a_byteArray() { int pos = 0; int result = 0; while (pos < len) { result = srcBytes[pos]; pos++; } return result; } }
ops/time:標(biāo)識每秒鐘執(zhí)行的次數(shù)
依賴jar包:
<dependency> <groupId>org.openjdk.jmh</groupId> <artifactId>jmh-core</artifactId> <version>${jmh.version}</version> </dependency> <dependency> <groupId>org.openjdk.jmh</groupId> <artifactId>jmh-generator-annprocess</artifactId> <version>${jmh.version}</version> <scope>provided</scope> </dependency>
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <id>run-benchmarks</id> <phase>integration-test</phase> <goals> <goal>exec</goal> </goals> <configuration> <classpathScope>test</classpathScope> <executable>java</executable> <arguments> <argument>-classpath</argument> <classpath /> <argument>org.openjdk.jmh.Main</argument> <argument>.*</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
以上就是Java Benchmark 基準(zhǔn)測試,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
網(wǎng)站欄目:JavaBenchmark基準(zhǔn)測試的實例詳解
路徑分享:http://jinyejixie.com/article16/ppeidg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、、企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、移動網(wǎng)站建設(shè)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)