如何终止JMH基准测试?中途异常场景下的处理方案咨询
Great question—dealing with unexpected issues mid-benchmark doesn’t have to mean nuking the whole process. Let’s break this down into two key scenarios: stopping all benchmarks entirely, and aborting just the current one while letting others run.
Terminating All Benchmarks Properly
System.exit(...) is not your only option, and it’s often the least graceful choice—especially if you’re running JMH programmatically or as part of a larger pipeline. Here are cleaner approaches:
- Programmatic Runner Control: If you’re launching benchmarks via JMH’s
RunnerAPI, you have full control:- Keep a reference to your
Runnerinstance, and callrunner.stop()when you need to halt everything. This triggers a graceful shutdown, letting JMH wrap up in-flight iterations and output partial results if possible. - Alternatively, interrupt the thread running the
RunnerwithThread.interrupt(). JMH handles interrupts properly, so it’ll shut down cleanly without leaving resources hanging.
- Keep a reference to your
- Command-Line Interrupt: If you’re running benchmarks from the terminal, pressing Ctrl+C (or sending a SIGINT signal) is the standard way. JMH catches this and performs a graceful shutdown, printing any results collected up to that point.
- Fatal Exception: If you’re inside a benchmark method and need an immediate full stop, throw an unchecked
RuntimeException(or custom exception). JMH will treat this as a fatal error for the entire run and terminate all benchmarks. Note: This is more abrupt than usingrunner.stop()or interrupts, so use it only for critical failures.
Aborting the Current Benchmark Only (Keep Others Running)
If you want to fail just the problematic benchmark and let the rest of your suite proceed, here’s what to do:
- Throw an Exception in the Benchmark Method: When you detect a failure (like missing resources), throw an unchecked exception (e.g.,
IllegalStateException) directly from your benchmark method. JMH will mark that specific benchmark as failed (you’ll see an error in the final results) but will continue executing all other benchmark methods/classes. - Use
Blackholefor Safe Exception Throwing: While not mandatory, you can useBlackhole.throwException(Throwable t)to wrap your exception. This ensures JMH doesn’t optimize away the exception-throwing path (though in most cases, a regular exception works just fine). - Conditional Early Exit (For Non-Critical Issues): If you don’t want to mark the benchmark as failed but just skip bad iterations, use
BenchmarkParamsto check the current iteration and return early. But for critical failures that warrant aborting the entire benchmark, throwing an exception is clearer and more actionable.
Pro Tip: If you’re using Maven/Gradle JMH plugins, a single failed benchmark won’t stop the entire build by default. The plugin will report the failure but keep running other benchmarks—perfect if you have a large suite.
内容的提问来源于stack exchange,提问作者digital_infinity

