Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解
Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解
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)測(cè)試類型 @OutputTimeUnit(TimeUnit.SECONDS)//基準(zhǔn)測(cè)試結(jié)果的時(shí)間類型 @Warmup(iterations = 3)//預(yù)熱的迭代次數(shù) @Threads(2)//測(cè)試線程數(shù)量 @State(Scope.Thread)//該狀態(tài)為每個(gè)線程獨(dú)享 //度量:iterations進(jìn)行測(cè)試的輪次,time每輪進(jìn)行的時(shí)長(zhǎng),timeUnit時(shí)長(zhǎng)單位,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 運(yùn)行之后項(xiàng)目路徑會(huì)出現(xiàn)按照測(cè)試順序輸出hotspot_pid<PID>.log文件,可以使用JITWatch進(jìn)行分析,可以根據(jù)最后運(yùn)行的結(jié)果的順序按文件時(shí)間找到對(duì)應(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,請(qǐng)看JMH介紹 //.output("InstructionsBenchmark.log")//輸出信息到文件 .build(); new Runner(opt).run(); } //空循環(huán) 對(duì)照項(xiàng) @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)識(shí)每秒鐘執(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)測(cè)試,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
SSH框架網(wǎng)上商城項(xiàng)目第26戰(zhàn)之訂單支付后發(fā)送短信提醒
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第26戰(zhàn)之訂單支付后發(fā)送短信提醒,感興趣的小伙伴們可以參考一下2016-06-06Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長(zhǎng)連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10Java中自動(dòng)裝箱、拆箱引起的耗時(shí)詳解
這篇文章主要給大家介紹了關(guān)于Java中自動(dòng)裝箱、拆箱引起的耗時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04淺談SpringMVC中Interceptor和Filter區(qū)別
這篇文章主要介紹了淺談SpringMVC中Interceptor和Filter區(qū)別,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04Java實(shí)現(xiàn)折半插入排序算法的示例代碼
折半插入排序(Binary Insertion Sort)是對(duì)插入排序算法的一種改進(jìn)。不斷的依次將元素插入前面已排好序的序列中。本文將利用Java語(yǔ)言實(shí)現(xiàn)這一排序算法,需要的可以參考一下2022-08-08Java 中的 BufferedReader 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
BufferedReader 是緩沖字符輸入流。它繼承于Reader。接下來(lái)通過(guò)本文給大家介紹BufferedReader的相關(guān)知識(shí),需要的朋友參考下吧2017-05-05