JMH 的使用及示例

1.JMH的介绍

 JMH是新的microbenchmark(微基准测试)框架(2013年首次发布)。与其他众多框架相比它的特色优势在于,它是由Oracle实现JIT的相同人员开发的。特别是我想提一下Aleksey Shipilev和他优秀的博客文章。JMH可能与最新的Oracle JRE同步,其结果可信度很高。

  
2. 使用JMH

使用JMH仅需满足2个必要条件(其他所有都是建议选项):

  •     设置jmh-core的maven依赖
  •  使用注解测试方法

2.1 在pom.xml中添加如下内容:

<dependencies>
		<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
		<dependency>
			<groupId>org.openjdk.jmh</groupId>
			<artifactId>jmh-core</artifactId>
			<version>1.15</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
		<dependency>
			<groupId>org.openjdk.jmh</groupId>
			<artifactId>jmh-generator-annprocess</artifactId>
			<version>1.15</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
</dependencies>

2.2 安装m2e-apt

在eclipse market中搜索m2e 找到这个:

104819_CjVj_2561483.png

并安装

2.3 在IDE的preference中勾选自动启动 apt

在eclipse:  windows-> preference-> Maven->Annotation Processing中 勾选第一个选项 如图:

105037_ALJ6_2561483.png

 

3 demo

官方有个jmh的示例 helloworld

http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/

我再附一个例子,测试两个方法的性能:

package jmh;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(value = Scope.Benchmark)
public class Test {
	
	private int a[] = new int[5];

	@Benchmark
	@BenchmarkMode(Mode.SampleTime)
	public void badTry() {
		int result = 0;
		int i = 0;
		try {
			while (true) {
				result += this.a[i++];
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			// do nothing
		}
	}

	@Benchmark
	@BenchmarkMode(Mode.SampleTime)
	public void goodTry() {
		int result = 0;
		for (int i = 0; i < a.length; i++) {
			result += this.a[i];
		}
	}

	
	public static void main(String[] args) throws RunnerException {

		Options opt = new OptionsBuilder()

				.include(Test.class.getSimpleName())

				.forks(0)

				.build();

		new Runner(opt).run();

	}
	

}

需要注意的是,运行jmh main 中就是这样写的,然后在方法上加上注解就可以跑了

 

 

 

转载于:https://my.oschina.net/u/2561483/blog/755660

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 JMH 测试结果转换为折线图,可以使用以下步骤: 1. 将 JMH 测试结果保存为 CSV 文件。可以通过在运行测试时添加 `-rff filename.csv` 参数来实现。这将生成一个名为 filename.csv 的文件,其中包含测试结果。 2. 使用一个图表库,例如 JFreeChart 或 Chart.js,读取 CSV 文件并将数据绘制为折线图。 如果使用 JFreeChart,可以按照以下步骤进行操作: 1. 将 JFreeChart 添加到项目的依赖中。 2. 使用 CSV 文件中的数据创建一个 `TimeSeriesCollection` 对象。 3. 创建一个 `JFreeChart` 对象,并将 `TimeSeriesCollection` 对象添加到其中。 4. 将 `JFreeChart` 对象添加到一个 `ChartPanel` 对象中。 5. 将 `ChartPanel` 添加到一个 `JFrame` 中,并显示窗口。 以下是一个简单的示例代码: ```java import java.io.File; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.time.Millisecond; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; import com.opencsv.CSVReader; public class JMHChart extends ApplicationFrame { private static final long serialVersionUID = 1L; private TimeSeries series; public JMHChart(final String title) { super(title); this.series = new TimeSeries("JMH Benchmark Results", Millisecond.class); final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(800, 600)); setContentPane(chartPanel); } private JFreeChart createChart(final XYDataset dataset) { return ChartFactory.createTimeSeriesChart( "JMH Benchmark Results", "Time", "Score", dataset, true, true, false ); } public void addData(File csvFile) throws IOException { CSVReader reader = new CSVReader(new FileReader(csvFile)); String[] line; while ((line = reader.readNext()) != null) { try { long timestamp = Long.parseLong(line[0]); double score = Double.parseDouble(line[1]); series.add(new Millisecond(timestamp), score); } catch (NumberFormatException e) { // ignore invalid lines } } reader.close(); } public static void main(final String[] args) throws IOException { final JMHChart demo = new JMHChart("JMH Benchmark Results"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); demo.addData(new File("filename.csv")); } } ``` 这个示例代码将 CSV 文件中的数据添加到 `TimeSeries` 对象中,并使用 `createChart` 方法创建一个 `JFreeChart` 对象。然后将 `JFreeChart` 对象添加到 `ChartPanel` 中,并显示窗口。你需要根据你的 CSV 文件的格式和需要绘制的图表类型进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值