You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java 12内置微基准测试套件?相关技术咨询

Answers to Java 12's Microbenchmark Suite Questions

Great questions about Java 12's built-in microbenchmark suite from JEP 230! Let's break down each one clearly:

1. Do I need to import a specific module in module-info.java?

Yes, you must declare a dependency on the jdk.benchmark module in your module-info.java to use the suite. This is because the microbenchmark suite is packaged as a dedicated module in Java 12+. Your module descriptor should include:

requires jdk.benchmark;

Keep in mind that jdk.benchmark is a restricted module, so if you ever need access to non-public APIs (uncommon for most basic benchmarking tasks), you might need to add --add-exports flags during compilation or runtime. But for standard usage with core JMH annotations, the requires clause is all you need.

2. Where are the suite's classes/annotations located?

The package structure is identical to the standalone JMH library—since this suite is essentially a bundled version of JMH integrated into the JDK. The key packages you'll interact with are:

  • org.openjdk.jmh.annotations: Contains all core annotations like @Benchmark, @Fork, @Warmup, @Measurement, and @State.
  • org.openjdk.jmh.runner: Includes classes for executing benchmarks, such as JmhRunner (the main entry point for running benchmarks programmatically).
  • org.openjdk.jmh.results: Houses classes for handling and analyzing benchmark results if you need to process outputs programmatically.

All these packages are exported by the jdk.benchmark module, so they're accessible once you've added the requires clause to your module descriptor.

3. What are the main differences between this suite and standalone JMH?

Here are the key distinctions you should know:

  • No external setup needed: You don't have to add JMH as a Maven/Gradle dependency—it's pre-included with Java 12+. This simplifies benchmarking setup for projects already using the corresponding JDK version.
  • Version locked to JDK: The JMH version is fixed to whatever was bundled with your JDK release. You can't independently upgrade or downgrade JMH without switching to a different JDK version, unlike standalone JMH where you can pick any version you want.
  • Modular rules apply: Since it's part of the JDK's module system, you have to follow modular constraints (like declaring requires jdk.benchmark) which isn't necessary with standalone JMH (a traditional JAR that works on the classpath).
  • Limited extensibility: Standalone JMH supports a wide range of third-party plugins and extensions (such as custom profilers or alternative result formatters). The built-in suite focuses on core JMH functionality and may not support all these extensions.
  • Distribution availability: The jdk.benchmark module is included in the full JDK distribution, but it might be omitted from stripped-down JRE builds (though this is rarely a concern for benchmarking scenarios).

内容的提问来源于stack exchange,提问作者Jacob G.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:26:32