如何在linux上做Java基准测试工具JMH测试

简介: 如何在linux上做Java基准测试工具JMH测试

上篇我们主要介绍了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)
}
目录
相关文章
|
14天前
|
安全 Linux Shell
四、Linux核心工具:Vim, 文件链接与SSH
要想在Linux世界里游刃有余,光会“走路”还不够,还得配上几样“高级装备”。首先是Vim编辑器,它像一把瑞士军刀,让你能在命令行里高效地修改文件。然后要懂“软硬链接”,软链接像个快捷方式,硬链接则是给文件起了个别名。最后,SSH是你的“传送门”,不仅能让你安全地远程登录服务器,还能用scp轻松传输文件,设置好密钥更能实现免-密登录,极大提升效率。
187 3
|
13天前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
85 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
29天前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
105 16
|
2月前
|
Java Linux Apache
Apache NetBeans 27 (macOS, Linux, Windows) - Java 等多语言开源跨平台 IDE
Apache NetBeans 27 (macOS, Linux, Windows) - Java 等多语言开源跨平台 IDE
147 5
Apache NetBeans 27 (macOS, Linux, Windows) - Java 等多语言开源跨平台 IDE
|
3月前
|
缓存 监控 Linux
Linux系统性能调优技巧和相关工具
Linux 作为一种应用应展和系统服务的优选操作系统,在处理性能和端到端点评估上持有出色表现。但是,在处理进程或系统处于低效状态时,性能调优就显得十分重要。本文将探讨一些 Linux 系统性能调优的常用技巧,并介绍相关工具
95 1
Linux系统性能调优技巧和相关工具
|
3月前
|
Linux 数据安全/隐私保护 iOS开发
推荐Linux环境下效能优良的双向文件同步工具
综合上述条件,对于Linux环境下的双向文件同步需求,Unison 和 Syncthing 是两个非常出色的选择。它们都有良好的社区支持和文档资源,适用于不同规模的环境,从个人使用到商业部署。Unison 特别适合那些需要手动干预同步过程、需要处理文件冲突解决的场景。而 Syncthing 更加现代化,适合需要自动、实时的数据同步与备份的环境。对于选择哪一个,这将取决于个人的使用场景和具体需求。
337 16
|
2月前
|
数据采集 编解码 运维
一文讲完说懂 WowKey -- WowKey 是一款 Linux 类设备的命令行(CLT)运维工具
WowKey 是一款面向 Linux 类设备的命令行运维工具,支持自动登录、批量执行及标准化维护,适用于企业、团队或个人管理多台设备,显著提升运维效率与质量。
|
4月前
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
756 23
|
9月前
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。