JMH 超时不中断
JMH Timeout doesn't interrupt
在尝试使用 JMH 中的超时时,我发现 none 的超时实际上导致了任何中断。我可以将问题简化为以下几行:
package main.java;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.RunnerException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class TimeoutBenchmark {
public static void main(String... args) throws IOException, RunnerException {
Main.main(args);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 0)
@Timeout(time = 10, timeUnit = TimeUnit.SECONDS)
public long benchmark() throws InterruptedException {
Thread.sleep(20000);
return 0;
}
}
由于 Thread.sleep
处理中断,我希望每次迭代 运行 10 秒,而不是 20 秒。然而,事实并非如此:
# JMH version: 1.20
# VM version: JDK 1.8.0_144, VM 25.144-b01
# VM invoker: C:\Program Files\Java\jdk1.8.0_144\jre\bin\java.exe
# VM options: -Dfile.encoding=UTF-8
# Warmup: <none>
# Measurement: 20 iterations, 1 s each
# Timeout: 10 s per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: main.java.TimeoutBenchmark.benchmark
# Run progress: 0,00% complete, ETA 00:03:20
# Fork: 1 of 10
Iteration 1: 20,004 s/op
Iteration 2: 20,009 s/op
Iteration 3: 20,009 s/op
Iteration 4: 20,014 s/op
Iteration 5: 20,003 s/op
Iteration 6: 20,003 s/op
Iteration 7: 20,003 s/op
这是为什么?如何更改此代码,使每次迭代实际上在 10 秒后中断?
在BenchmarkHandler
中处理超时时在代码中有注释
// now we communicate all worker threads should stop
control.announceDone();
// wait for all workers to transit to teardown
control.awaitWarmdownReady();
// Wait for the result, handling timeouts
while (completed.size() < numThreads) {
所以基本上这个超时只适用于 teardown
阶段。
但是我认为应该使用此信息修改此注释的 javadoc。
拆解说明如下:http://java-performance.info/jmh/
这封邮件也很有用:http://mail.openjdk.java.net/pipermail/jmh-dev/2015-May/001912.html
在尝试使用 JMH 中的超时时,我发现 none 的超时实际上导致了任何中断。我可以将问题简化为以下几行:
package main.java;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.RunnerException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class TimeoutBenchmark {
public static void main(String... args) throws IOException, RunnerException {
Main.main(args);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 0)
@Timeout(time = 10, timeUnit = TimeUnit.SECONDS)
public long benchmark() throws InterruptedException {
Thread.sleep(20000);
return 0;
}
}
由于 Thread.sleep
处理中断,我希望每次迭代 运行 10 秒,而不是 20 秒。然而,事实并非如此:
# JMH version: 1.20
# VM version: JDK 1.8.0_144, VM 25.144-b01
# VM invoker: C:\Program Files\Java\jdk1.8.0_144\jre\bin\java.exe
# VM options: -Dfile.encoding=UTF-8
# Warmup: <none>
# Measurement: 20 iterations, 1 s each
# Timeout: 10 s per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: main.java.TimeoutBenchmark.benchmark
# Run progress: 0,00% complete, ETA 00:03:20
# Fork: 1 of 10
Iteration 1: 20,004 s/op
Iteration 2: 20,009 s/op
Iteration 3: 20,009 s/op
Iteration 4: 20,014 s/op
Iteration 5: 20,003 s/op
Iteration 6: 20,003 s/op
Iteration 7: 20,003 s/op
这是为什么?如何更改此代码,使每次迭代实际上在 10 秒后中断?
在BenchmarkHandler
中处理超时时在代码中有注释
// now we communicate all worker threads should stop control.announceDone(); // wait for all workers to transit to teardown control.awaitWarmdownReady(); // Wait for the result, handling timeouts while (completed.size() < numThreads) {
所以基本上这个超时只适用于 teardown
阶段。
但是我认为应该使用此信息修改此注释的 javadoc。
拆解说明如下:http://java-performance.info/jmh/
这封邮件也很有用:http://mail.openjdk.java.net/pipermail/jmh-dev/2015-May/001912.html