前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JMH的简单应用

JMH的简单应用

原创
作者头像
李子健
发布2023-08-01 21:50:49
2330
发布2023-08-01 21:50:49
举报
文章被收录于专栏:每日一善每日一善

jmh是个java的压测工具,感觉工具看说明书就行,如果有过压测经验,很容易使用。

没有也不要紧。网上搜下BeanchMarkMode即可。如果多线程使用State即可。这两部分足以满足日常的需求。

前提是环境好使。大家可以看下我下面的环境配置,在idea2023上,使用java8是可以正常使用的哦

性能测试工具JMH

  • 测试模式
    • Throughput 吞吐量
    • AverageTIme 平均时间
    • SampleTime 采样统计时间
    • SingleSHotTime 单次执行时间
  • 常用的注解
    • BeanchMark 执行压测
    • BeanchMarkMode 压测模式,可以选上面的
    • OutputTimeUnit 输出的时间单位
    • State作用域:变量共享,线程共享,组间共享
    • JMH之代码消除 :需要考虑编译器会做编码优化,消除优化的代码

构建一个jmh的通用的构建流程

代码语言:java
复制
public class JMHXMain {

    public static void main(String[] args) throws RunnerException {
        final Options options = new OptionsBuilder()
            .include(JMHXThroughput.class.getSimpleName())
            .forks(1)
            .build();
        new Runner(options).run();

    }
}


@State(Scope.Thread)
public class JMHXThroughput {


    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.SECONDS)
    @Warmup(iterations = 5)
    @Measurement(iterations = 4)
    public void measureThroughput() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(100);
    }

}

增加依赖项

代码语言:html
复制
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.36</version>
</dependency>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.36</version>
</dependency>

maven增加构建的build

代码语言:html
复制
<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <!--pluginManagement-->
    <plugins>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <!-- http://maven.apache.org/plugins/maven-compiler-plugin/index.html -->
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.2.0</version>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <!-- Skipping Tests 2018-02-01 16:00:42 -->
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-source-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <!-- 源码打包并发布 2018-11-07 16:41 -->
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${uber-jar.name}</finalName>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.openjdk.jmh.Main</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <!--
                                    Shading signed JARs will fail without this.
                                    https://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                                    -->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                    <exclude>META-INF/MANIFEST.MF</exclude>
                                    <exclude>LICENSE</exclude>
                                    <exclude>THIRD-PARTY</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!--/pluginManagement-->
</build>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云压测
云压测(Performance Testing Service, PTS)是一款分布式性能测试服务,可模拟海量用户的真实业务场景,全方位验证系统可用性和稳定性。支持按需发起压测任务,提供百万并发多地域流量发起能力。提供流量录制、场景编排、流量定制、高级脚本定制等功能,可快速根据业务模型定义压测场景,真实还原应用大规模业务访问场景,帮助用户提前识别应用性能问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档