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

如何终止JMH基准测试?中途异常场景下的处理方案咨询

Handling JMH Benchmark Termination on Unexpected Failures

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 Runner API, you have full control:
    • Keep a reference to your Runner instance, and call runner.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 Runner with Thread.interrupt(). JMH handles interrupts properly, so it’ll shut down cleanly without leaving resources hanging.
  • 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 using runner.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 Blackhole for Safe Exception Throwing: While not mandatory, you can use Blackhole.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 BenchmarkParams to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:24:39