由于原文翻译过来还是很难看懂,并且夹杂了很多不重要的信息。所以我在「」中进行了总结,「」中的内容可以不看,也可以只看「」中的内容。
package org.openjdk.jmh.samples;
import org.openjdk.jmh.annotations.Benchmark;
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;
public class JMHSample_01_HelloWorld {
/*
* This is our first benchmark method.
* 这是我们第一个基准测试方法。
*
* JMH works as follows: users annotate the methods with @Benchmark, and
* then JMH produces the generated code to run this particular benchmark as
* reliably as possible. In general one might think about @Benchmark methods
* as the benchmark "payload", the things we want to measure. The
* surrounding infrastructure is provided by the harness itself.
* JMH会做如下工作:用户使用@Benchmark注解方法,然后JMH会生成代码去尽可能可靠地运行
* 这个指定的基准测试。一般来说,可以将@Benchmark注解的方法看作基准测试的“负载”,
* 即我们想度量的东西。「我们想度量的东西叫做“负载”,在方法上加上@Benchmark注解
* 可以使其变成一个“负载”。」周边的基础设施将由用具自身提供。
*
* Read the Javadoc for @Benchmark annotation for complete semantics and
* restrictions. At this point we only note that the methods names are
* non-essential, and it only matters that the methods are marked with
* @Benchmark. You can have multiple benchmark methods within the same
* class.
* 请阅读@Benchmark注解的Javadoc获取完整的语义与限制。在这里我们注意到方法名没有
* 特殊要求,只需在方法上标记@Benchmark注解。在同一个类中可以有多个基准测试方法。
*
* Note: if the benchmark method never finishes, then JMH run never finishes
* as well. If you throw an exception from the method body the JMH run ends
* abruptly for this benchmark and JMH will run the next benchmark down the
* list.
* 如果基准测试方法永不结束,那么JMH运行也永远不会结束。如果你从方法体中抛出一个
* 异常,那么基准测试的运行将突然终止,并且JMH将运行列表中的下一个基准测试。
* 「如果被测方法抛出异常,则该项测试结束,继续进行下一项测试。」
*
* Although this benchmark measures "nothing" it is a good showcase for the
* overheads the infrastructure bear on the code you measure in the method.
* There are no magical infrastructures which incur no overhead, and it is
* important to know what are the infra overheads you are dealing with. You
* might find this thought unfolded in future examples by having the
* "baseline" measurements to compare against.
* 尽管这个基准测试度量什么也没做,但是它很好地展示了基础设施对方法中你度量的代码所承担的开销。
* 不存在不会产生开销的魔法般的基础设施,了解你要处理的基础开销是很重要的。
* 你可能会发现这个思想体现在之后的例子中,通过用于比较的“基准线”度量值。
* 「虽然这个被测方法是空的,但是从中可以得到基础设施的方法调用的开销。
* 这个开销可以用作其他测试的“基准线”。」
*/
@Benchmark
public void wellHelloThere() {
// this method was intentionally left blank.
}
/*
* ============================== HOW TO RUN THIS TEST: ====================================
*
* You are expected to see the run with large number of iterations, and
* very large throughput numbers. You can see that as the estimate of the
* harness overheads per method call. In most of our measurements, it is
* down to several cycles per call.
* 你将看到大量的迭代和非常大的吞吐量。
* 你可以将其视为每个方法调用的管理开销的估计值。
* 在我们大多数测量中,每次调用可以归结为几个周期。
*
* a) Via command-line:
* $ mvn clean install
* $ java -jar target/benchmarks.jar JMHSample_01
*
* JMH generates self-contained JARs, bundling JMH together with it.
* The runtime options for the JMH are available with "-h":
* $ java -jar target/benchmarks.jar -h
*
* b) Via the Java API:
* (see the JMH homepage for possible caveats when running from IDE:
* http://openjdk.java.net/projects/code-tools/jmh/)
*/
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(JMHSample_01_HelloWorld.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
}