Class BenchmarkRunner

java.lang.Object
cloud.opencode.base.test.benchmark.BenchmarkRunner

public final class BenchmarkRunner extends Object
Benchmark Runner - Configurable benchmark test runner 基准测试运行器 - 可配置的基准测试运行器

Provides a fluent API for running and comparing benchmarks.

提供用于运行和比较基准测试的流式API。

Features | 主要功能:

  • Configurable warmup and measurement iterations - 可配置的预热和测量迭代
  • Multiple benchmark comparison - 多基准测试比较
  • Timeout protection - 超时保护
  • Formatted results output with comparison - 格式化结果输出与比较

Usage Examples | 使用示例:

BenchmarkRunner runner = BenchmarkRunner.create()
    .warmup(100)
    .iterations(1000)
    .add("ArrayList", () -> new ArrayList<>().add("test"))
    .add("LinkedList", () -> new LinkedList<>().add("test"));

List<BenchmarkResult> results = runner.run();
runner.printResults();

Security | 安全性:

  • Thread-safe: No (not designed for concurrent use) - 线程安全: 否(非设计用于并发使用)
  • Null-safe: Yes (validates non-null inputs) - 空值安全: 是(验证非空输入)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static BenchmarkRunner create()
      Creates new benchmark runner. 创建新的基准测试运行器。
      Returns:
      the runner | 运行器
    • runSingle

      public static BenchmarkResult runSingle(String name, Runnable runnable)
      Creates and runs single benchmark. 创建并运行单个基准测试。
      Parameters:
      name - the benchmark name | 基准测试名称
      runnable - the code to benchmark | 要基准测试的代码
      Returns:
      the result | 结果
    • warmup

      public BenchmarkRunner warmup(int iterations)
      Sets warmup iterations. 设置预热迭代次数。
      Parameters:
      iterations - the iterations | 迭代次数
      Returns:
      this | 此对象
    • iterations

      public BenchmarkRunner iterations(int iterations)
      Sets measure iterations. 设置测量迭代次数。
      Parameters:
      iterations - the iterations | 迭代次数
      Returns:
      this | 此对象
    • timeout

      public BenchmarkRunner timeout(Duration timeout)
      Sets timeout for each benchmark. 设置每个基准测试的超时时间。
      Parameters:
      timeout - the timeout | 超时时间
      Returns:
      this | 此对象
    • output

      public BenchmarkRunner output(PrintStream output)
      Sets output stream. 设置输出流。
      Parameters:
      output - the output stream | 输出流
      Returns:
      this | 此对象
    • verbose

      public BenchmarkRunner verbose()
      Enables verbose output. 启用详细输出。
      Returns:
      this | 此对象
    • add

      public BenchmarkRunner add(String name, Runnable runnable)
      Adds benchmark. 添加基准测试。
      Parameters:
      name - the benchmark name | 基准测试名称
      runnable - the code to benchmark | 要基准测试的代码
      Returns:
      this | 此对象
    • add

      public <T> BenchmarkRunner add(String name, Callable<T> callable)
      Adds benchmark with return value (result ignored). 添加带返回值的基准测试(忽略结果)。
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      name - the benchmark name | 基准测试名称
      callable - the code to benchmark | 要基准测试的代码
      Returns:
      this | 此对象
    • run

      public List<BenchmarkResult> run()
      Runs all benchmarks. 运行所有基准测试。
      Returns:
      the results | 结果列表
    • getResults

      public List<BenchmarkResult> getResults()
      Gets the results (must call run() first). 获取结果(必须先调用run())。
      Returns:
      the results | 结果列表
    • printResults

      public BenchmarkRunner printResults()
      Prints results to output stream. 将结果打印到输出流。
      Returns:
      this | 此对象
    • printComparison

      public BenchmarkRunner printComparison()
      Prints comparison of results. 打印结果比较。
      Returns:
      this | 此对象
    • onComplete

      public BenchmarkRunner onComplete(Consumer<List<BenchmarkResult>> consumer)
      Executes callback with results. 使用结果执行回调。
      Parameters:
      consumer - the consumer | 消费者
      Returns:
      this | 此对象