Oracle官方推荐的性能测试工具!简单、精准又直观!
发布日期:2025-04-30 02:18:54 浏览次数:19 分类:精选文章

本文共 3175 字,大约阅读时间需要 10 分钟。

JMH ?????? Java ??????????? Java ???????? Oracle ?????? JIT???????? JVM?Java ???????????????????????

?????????? JMH ?? StringBuilder.append() ???????????????????

1. ????

????????? JMH ???????? Maven ????????????? JMH ????????????????

org.openjdk.jmh
jmh-core
1.23
org.openjdk.jmh
jmh-generator-annprocess
1.23

2. ?????

???????????? Java ? StringConnectTest?????????????????????????

public class StringConnectTest {    @Param(value = {"10", "50", "100"})    private int length;    @Benchmark    public void testStringBuilderAdd(Blackhole blackhole) {        StringBuilder sb = new StringBuilder();        for (int i = 0; i < length; i++) {            sb.append(i);        }        blackhole.consume(sb.toString());    }    @Benchmark    public void testStringAdd(Blackhole blackhole) {        String a = "";        for (int i = 0; i < length; i++) {            a += i;        }        blackhole.consume(a);    }    public static void main(String[] args) throws RunnerException {        Options opt = new OptionsBuilder()                .include(StringConnectTest.class.getSimpleName())                .result("result.json")                .resultFormat(ResultFormatType.JSON)                .build();        new Runner(opt).run();    }}

3. ??????

? main ???????? OptionsBuilder ?????????????????????

public static void main(String[] args) throws RunnerException {    Options opt = new OptionsBuilder()            .include(StringConnectTest.class.getSimpleName())            .result("result.json")            .resultFormat(ResultFormatType.JSON)            .build();    new Runner(opt).run();}

4. ??????

?? Maven ???????????????????????

mvn clean install -Djmh_demo=true StringConnectTest

5. ??????

????????????? JSON ??????? result.json??????????????????????????????????????

6. ??????

??????????????? StringBuilder.append() ???? String ????????????????????

???? ?? ?? ?? ???? (ns/op) ???
testStringBuilderAdd 100 avgt 5 819.329 0.00%
testStringAdd 100 avgt 5 6490.062 0.00%

???????????????? StringBuilder.append() ????????

7. ?? JMH ???

JMH ??????????????????????????????????????????????

  • @Benchmark ??????????????
  • @Param ????????????
  • @State ????????????
  • @Warmup ? @Measurement ??????????????????
  • @Fork ???????????????

8. JMH ????

????????? JMH ????????? Maven ??????????????? Maven ???????

org.apache.maven.plugins
maven-shade-plugin
2.4.1
package
shade
jmh-demo
org.openjdk.jmh.Main

9. ?? JMH Visual Chart

??????????????????? JMH Visual Chart ???????????

  • ?? JMH Visual Chart ???
  • ????? JSON ???????
  • ????????????????
  • 10. ?????????

    ??? JMH ???????????????????????

    • ???????????
    • ???????????

    ????????????????

    • ???????????????
    • ???????????
    • ?????????????

    ???????????????? JMH ??????????????????????????

    上一篇:ORACLE客户端连接
    下一篇:Oracle安装与远程连接配置(附Oracle安装包)

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2026年05月28日 03时53分48秒