上篇我们主要介绍了Java基准测试工具JMH高级使用,在windows上,我们结合IDE很容易跑性能测试,但是如果我们在linux 上,如何方便的去跑JMH性能测试呢?
首先,如果我们要通过JMH进行基准测试的话,直接在我们的gradle文件中引入JMH的依赖即可:
dependencies { jmhCompile project jmhCompile 'org.openjdk.jmh:jmh-core:1.36' jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess::1.36' }
然后,我们需要引入 gradle jmh 插件: jmh-gradle-plugin, 这里记住我们的gradle 版本为6.8.3,对应插件版本为0.6.8,但jmh可以升级到最高版本1.36
apply plugin: "me.champeau.jmh"; buildscript { repositories { maven { url = uri("https://plugins.gradle.org/m2/") } } dependencies { classpath("me.champeau.jmh:jmh-gradle-plugin:0.6.8") } } jmh { jvmArgs = ['-Dfile.encoding=UTF-8'];//为了让输出报告不至于乱码 resultFormat="JSON"; humanOutputFile = project.file("${project.buildDir}/reports/jmh/human.txt") // human-readable output file resultsFile = project.file("${project.buildDir}/reports/jmh/result.txt") // results file }
然后,在命令行执行:
gradle gamioo-cache:jmh
等命令执行完成,就会生成两个报告,一个是生成的明细和结果human.txt,一个只包含结果result.txt。
Benchmark (type) Mode Cnt Score Error Units CacheBenchMark.cache guava thrpt 5 10068879.977 ± 6603108.991 ops/s CacheBenchMark.cache:get guava thrpt 5 8968121.305 ± 6148450.420 ops/s CacheBenchMark.cache:put guava thrpt 5 1100758.672 ± 1936783.318 ops/s CacheBenchMark.cache caffeine thrpt 5 11674724.368 ± 11083096.255 ops/s CacheBenchMark.cache:get caffeine thrpt 5 8464759.462 ± 6455055.722 ops/s CacheBenchMark.cache:put caffeine thrpt 5 3209964.905 ± 4733849.104 ops/s
这样,我们就能在linux 上通过gradle 命令跑性能测试啦。
附上一些配置:
jmh { includes = ['some regular expression'] // include pattern (regular expression) for benchmarks to be executed excludes = ['some regular expression'] // exclude pattern (regular expression) for benchmarks to be executed iterations = 10 // Number of measurement iterations to do. benchmarkMode = ['thrpt','ss'] // Benchmark mode. Available modes are: [Throughput/thrpt, AverageTime/avgt, SampleTime/sample, SingleShotTime/ss, All/all] batchSize = 1 // Batch size: number of benchmark method calls per operation. (some benchmark modes can ignore this setting) fork = 2 // How many times to forks a single benchmark. Use 0 to disable forking altogether failOnError = false // Should JMH fail immediately if any benchmark had experienced the unrecoverable error? forceGC = false // Should JMH force GC between iterations? jvm = 'myjvm' // Custom JVM to use when forking. jvmArgs = ['Custom JVM args to use when forking.'] jvmArgsAppend = ['Custom JVM args to use when forking (append these)'] jvmArgsPrepend =[ 'Custom JVM args to use when forking (prepend these)'] humanOutputFile = project.file("${project.buildDir}/reports/jmh/human.txt") // human-readable output file resultsFile = project.file("${project.buildDir}/reports/jmh/results.txt") // results file operationsPerInvocation = 10 // Operations per invocation. benchmarkParameters = [:] // Benchmark parameters. profilers = [] // Use profilers to collect additional data. Supported profilers: [cl, comp, gc, stack, perf, perfnorm, perfasm, xperf, xperfasm, hs_cl, hs_comp, hs_gc, hs_rt, hs_thr, async] timeOnIteration = '1s' // Time to spend at each measurement iteration. resultFormat = 'CSV' // Result format type (one of CSV, JSON, NONE, SCSV, TEXT) synchronizeIterations = false // Synchronize iterations? threads = 4 // Number of worker threads to run with. threadGroups = [2,3,4] //Override thread group distribution for asymmetric benchmarks. jmhTimeout = '1s' // Timeout for benchmark iteration. timeUnit = 'ms' // Output time unit. Available time units are: [m, s, ms, us, ns]. verbosity = 'NORMAL' // Verbosity mode. Available modes are: [SILENT, NORMAL, EXTRA] warmup = '1s' // Time to spend at each warmup iteration. warmupBatchSize = 10 // Warmup batch size: number of benchmark method calls per operation. warmupForks = 0 // How many warmup forks to make for a single benchmark. 0 to disable warmup forks. warmupIterations = 1 // Number of warmup iterations to do. warmupMode = 'INDI' // Warmup mode for warming up selected benchmarks. Warmup modes are: [INDI, BULK, BULK_INDI]. warmupBenchmarks = ['.*Warmup'] // Warmup benchmarks to include in the run in addition to already selected. JMH will not measure these benchmarks, but only use them for the warmup. zip64 = true // Use ZIP64 format for bigger archives jmhVersion = '1.36' // Specifies JMH version includeTests = true // Allows to include test sources into generate JMH jar, i.e. use it when benchmarks depend on the test classes. duplicateClassesStrategy = DuplicatesStrategy.FAIL // Strategy to apply when encountring duplicate classes during creation of the fat jar (i.e. while executing jmhJar task) }