JMH注解参数意义及Benchmark配置疑问咨询
JMH基准测试疑问解答
代码示例
@Fork(value = 2, warmups = 1) @Warmup(iterations = 1, time = 15) @Measurement(iterations = 5, time = 15) @Threads(16) public class BenchmarkRunner { @Benchmark @BenchmarkMode(Mode.Throughput) public void measureThroughput(Blackhole bh, MyHttpClient client) { bh.consume(client.exec()); } }
运行输出
# Warmup: 1 iterations, 15 s each # Measurement: 5 iterations, 15 s each # Timeout: 10 min per iteration # Threads: 16 threads, will synchronize iterations # Benchmark mode: Throughput, ops/time # Benchmark: org.example.BenchmarkRunner.measureThroughput
问题1:输出中“5 iterations, 15 s each”的具体含义是什么?是否指每个迭代运行15秒,期间JMH会尽可能多地发起调用?
是的,你的理解完全正确。
这对应代码里的@Measurement(iterations = 5, time = 15)注解配置:
iterations = 5表示基准测试的测量阶段会执行5轮独立迭代time = 15表示每一轮迭代持续15秒
在每一轮的15秒内,JMH会让配置的16个线程无间断地重复执行measureThroughput方法(也就是你的HTTP调用逻辑),尽可能多地发起调用。每轮结束后JMH会统计该轮的总调用次数,最终结合5轮的数据计算出平均吞吐量(ops/time,即单位时间内的操作数)。
问题2:输出中“16 threads, will synchronize iterations”的具体含义是什么?
这对应代码里的@Threads(16)注解配置:
16 threads表示JMH会启动16个独立的工作线程,并行执行基准测试方法will synchronize iterations表示每一轮迭代(包括预热和测量阶段)的启动、停止都会做线程同步:所有16个线程会在迭代开始前等待统一的启动信号,确保同一时刻开始执行;迭代时间到后,所有线程也会同步停止执行。
这种同步机制是为了消除线程启动时间差带来的测试误差,保证每一轮迭代中所有线程的执行时间窗口完全一致,让最终的吞吐量统计数据更精准。
内容的提问来源于stack exchange,提问作者Abhijit Sarkar
相关产品推荐
相关产品推荐

