我想测量 JVM 启动时间,那么有什么方法可以用 JMH 做到这一点?
目前我正在考虑以下代码,但不确定测量是否可靠:
@State(Scope.Thread)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class JvmStartUpBenchmark {
private final String classpath = "...";
private final String path = ".../jdk/bin/java";
private final ProcessBuilder processBuilder = new ProcessBuilder(
path,
"-cp",
classpath,
Main.class.getName()
);
@Benchmark
public int jvmStartup() throws Exception {
return processBuilder.start().waitFor();
}
}
class Main {
public static void main(String[] args) {}
}