JMH:如何避免共享抽象基础状态的状态?

JMH: How to avoid sharing state from abstract base state?

我正在对 Spring 启动应用程序启动时间进行基准测试。完整的项目是here,是WIP,但是相关的类如下。

抽象基础状态:

public abstract class BootAbstractState {
    private Process started;

    private boolean isStarted() {
        return Objects.nonNull(started) && started.isAlive();
    }

    protected void start() {
        if (isStarted()) {
            throw new IllegalStateException("Already started");
        } else {
            ProcessBuilder pb = new ProcessBuilder(getCommand());
            try {
                started = pb
                        .inheritIO()
                        .start();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    }

    protected void stop() {
        if (isStarted()) {
            try {
                started.destroyForcibly().waitFor();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                started = null;
            }
        }
    }

    protected abstract String[] getCommand();
}

具体状态:

public class JarLauncherBenchmark {

    @Benchmark
    public void benchmark(JarLauncherState state) {
        state.start();
    }

    @State(Scope.Benchmark)
    public static class JarLauncherState extends BootAbstractState {
        private static final String MAIN_CLASS = "org.springframework.boot.loader.JarLauncher";

        @TearDown(Level.Iteration)
        public void tearDown() {
            stop();
        }

        @Override
        protected String[] getCommand() {
            return new String[]{"java", "-cp", System.getProperty("java.class.path"), MAIN_CLASS};
        }
    }
}

我构建了一个影子 JAR,运行 如下所示:

java -jar minimal-benchmark/build/libs/minimal-benchmark-0.0.1-SNAPSHOT-all.jar \
  -bm avgt -f 1 -foe true -i 5 -wi 1 -tu ms

以上失败,出现以下异常:

# JMH version: 1.20
# VM version: JDK 1.8.0_66, VM 25.66-b17
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 1 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: mypackage.JarLauncherBenchmark.benchmark

# Run progress: 0.00% complete, ETA 00:00:06
# Fork: 1 of 1
# Warmup Iteration   1: <failure>
<failure>

java.lang.IllegalStateException: Already started
        at mypackage.BootAbstractState.start(BootAbstractState.java:22)
        at mypackage.JarLauncherBenchmark.benchmark(JarLauncherBenchmark.java:13)

显然,它并不像我想的那样工作,并且没有为每次迭代实例化新状态。我还尝试了 Thread 作用域和 运行 多线程(-t 命令行选项),但这没有帮助。

"Iteration" 不是“@Benchmark 调用”——它由整个 API 中的树级别(试用、迭代、调用)捕获。它将是 @Setup(Level.Iteration) --> @Benchmark(多次,直到迭代时间在 avgt 模式下到期)--> @TearDown(Level.Iteration)。因此 @Benchmark 的第二次调用会给你这样的异常,因为 started() 之前确实被调用过。

不平衡 @Setup/@TearDown 对通常是个坏主意。既然你在做 @TearDown(Level.Iteration),你真的应该做 @Setup(Level.Iteration),然后在那里做 start()