gpt4 book ai didi

java - JMH:如何避免从抽象基状态共享状态?

转载 作者:行者123 更新时间:2023-12-02 11:25:50 25 4
gpt4 key购买 nike

我正在对 Spring Boot 应用程序启动时间进行基准测试。完整的项目是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 命令行选项),但这没有帮助。

最佳答案

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

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

关于java - JMH:如何避免从抽象基状态共享状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com