写点什么

JMH 实践记录

用户头像
Clarke
关注
发布于: 刚刚

1 引入 jar 包

<dependency>

<groupId>org.openjdk.jmh</groupId>

<artifactId>jmh-core</artifactId>

<version>1.26</version>

</dependency>

<dependency>

<groupId>org.openjdk.jmh</groupId>

<artifactId>jmh-generator-annprocess</artifactId>

<version>1.26</version>

<scope>compile</scope>

</dependency>

  1. 加入 maven build 配置

 <build> 	    <plugins> 	        <plugin>				<groupId>org.apache.maven.plugins</groupId>				<artifactId>maven-compiler-plugin</artifactId>				<configuration>					<source>1.8</source>					<target>1.8</target>					<annotationProcessorPaths>						<path>							<groupId>org.openjdk.jmh</groupId>							<artifactId>jmh-generator-annprocess</artifactId>							<version>1.26</version>						</path>					</annotationProcessorPaths>				</configuration>			</plugin>   		    <plugin>		        <groupId>org.apache.maven.plugins</groupId>		        <artifactId>maven-shade-plugin</artifactId>		        <version>2.0</version>		        <executions>		            <execution>		                <phase>package</phase>		                <goals>		                    <goal>shade</goal>		                </goals>		                <configuration>		                    <finalName>microbenchmarks</finalName>		                    <transformers>		                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">		                            <mainClass>XSchemaBenchmark</mainClass>		                        </transformer>		                    </transformers>		                </configuration>		            </execution>		        </executions>		    </plugin>		    <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-assembly-plugin</artifactId>                <version>2.2.1</version>                <configuration>                    <descriptors>                        <descriptor>src/main/assembly/assembly.xml</descriptor>                    </descriptors>                </configuration>                <executions>                    <execution>                        <id>make-assembly</id>                        <phase>package</phase>                        <goals>                            <goal>single</goal>                        </goals>                    </execution>                </executions>            </plugin>	    </plugins>	</build>
复制代码

加入 asemble 打包配置项和启动文件

<assembly>	<id>bin</id> 	<formats>		<format>zip</format>	</formats>	<baseDirectory>/</baseDirectory>	<!-- 从目标目录拷贝文件去压缩 -->	<files>  	  <file>		<source>${project.basedir}/target/microbenchmarks.jar</source> 		<outputDirectory>/bin/</outputDirectory>	  </file> 	  <file>		<source>${project.basedir}/src/main/bin/start.sh</source> 		<fileMode>0755</fileMode>		<outputDirectory>/bin/</outputDirectory>	  </file>		</files></assembly>
start.shjava -XX:+UnlockCommercialFeatures -XX:+UseG1GC -Xss256k -Xms6120m -Xmx6120m -jar microbenchmarks.jar > /export/Logs/perf.log
复制代码


3.加入性能测试类


import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.LongAdder;
import org.openjdk.jmh.Main;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.BenchmarkMode;import org.openjdk.jmh.annotations.Mode;import org.openjdk.jmh.annotations.OutputTimeUnit;import org.openjdk.jmh.annotations.Threads;import org.openjdk.jmh.runner.Runner;import org.openjdk.jmh.runner.options.Options;import org.openjdk.jmh.runner.options.OptionsBuilder;
@OutputTimeUnit(TimeUnit.MICROSECONDS)@BenchmarkMode(Mode.Throughput)public class LongAdderBenchmark { private static AtomicInteger count = new AtomicInteger(); private static LongAdder longAdder = new LongAdder();
public static void main(String[] args) throws Exception { Options options = new OptionsBuilder().include(LongAdderBenchmark.class.getName()).forks(1).build(); new Runner(options).run(); }
@Benchmark @Threads(10) public void run0() { count.getAndIncrement(); }
@Benchmark @Threads(10) public void run1() { longAdder.increment(); }
}
复制代码


发布于: 刚刚阅读数: 4
用户头像

Clarke

关注

还未添加个人签名 2018.04.15 加入

还未添加个人简介

评论

发布
暂无评论
JMH实践记录