我在JMH基准测试XML处理器中有一个方法。我想通过将文件名传递给方法参数来使用多个文件对其进行多次基准测试,但它不起作用。有谁知道如何在没有 25 种不同方法的情况下运行 25 次 JMH 方法?谢谢。
public class MyBenchmark {
@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 100, time = 200, timeUnit = TimeUnit.MILLISECONDS)
public void dom(String file_name) {
try {
File fXmlFile = new File(file_name);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i=1; i<= 25; i++){
String file_name = Integer.toString(i) + ".xml";
dom(file_name);
}
}
错误:
first-benchmark: Compilation failure: Compilation failure:
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,9] illegal start of type
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,19] ')' expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,20] illegal start of type
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,21] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,22] ';' expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,24] illegal start of type
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,26] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,32] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,34] illegal start of type
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,35] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[71,36] ';' expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[72,33] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[73,17] invalid method declaration; return type required
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[73,30] <identifier> expected
[ERROR] /D:/Testing/first-benchmark/src/main/java/com/tri/MyBenchmark.java:[75,1] class, interface, or enum expected
下面是
使用@Param
使用不同参数多次对方法进行基准测试的示例。在您的情况下,也许您可以定义@Param({1,2,3,4,......,25})
.