由于原文翻译过来还是很难看懂,并且夹杂了很多不重要的信息。所以我在「」中进行了总结,「」中的内容可以不看,也可以只看「」中的内容。

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