Class Benchmark

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

public final class Benchmark extends Object
Benchmark 基准测试

Simple benchmarking utility.

简单基准测试工具。

Features | 主要功能:

  • Performance measurement utilities - 性能测量工具
  • Comparison benchmarking - 对比基准测试

Usage Examples | 使用示例:

Duration time = Benchmark.time(() -> heavyOperation());
Benchmark.BenchmarkResult result = Benchmark.run("test", () -> operation());

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • time

      public static Duration time(Runnable runnable)
      Measure execution time of runnable 测量可运行对象的执行时间
      Parameters:
      runnable - the runnable | 可运行对象
      Returns:
      the duration | 时长
    • time

      public static <T> Benchmark.TimedResult<T> time(Supplier<T> supplier)
      Measure execution time with result 测量带结果的执行时间
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      supplier - the supplier | 供应者
      Returns:
      the result with timing | 带计时的结果
    • run

      public static Benchmark.BenchmarkResult run(String name, Runnable runnable, int warmupIterations, int measureIterations)
      Run benchmark with warmup and iterations 运行带预热和迭代的基准测试
      Parameters:
      name - the benchmark name | 基准测试名称
      runnable - the runnable | 可运行对象
      warmupIterations - warmup iterations | 预热迭代次数
      measureIterations - measure iterations | 测量迭代次数
      Returns:
      the result | 结果
    • run

      public static Benchmark.BenchmarkResult run(String name, Runnable runnable)
      Run benchmark with defaults 使用默认值运行基准测试
      Parameters:
      name - the name | 名称
      runnable - the runnable | 可运行对象
      Returns:
      the result | 结果
    • compare

      public static Benchmark.ComparisonResult compare(String name1, Runnable runnable1, String name2, Runnable runnable2)
      Compare two implementations 比较两个实现
      Parameters:
      name1 - first name | 第一个名称
      runnable1 - first runnable | 第一个可运行对象
      name2 - second name | 第二个名称
      runnable2 - second runnable | 第二个可运行对象
      Returns:
      comparison result | 比较结果