JMH - 运行 多次的方法

JMH - run a method for multiple times

我在 XML 处理器的 JMH 基准测试中有一个方法。我想通过将文件名传递给方法参数来对多个文件进行多次基准测试,但它不起作用。有谁知道如何在没有 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

Here 是使用 @Param 使用不同参数多次对方法进行基准测试的示例。在你的情况下,也许你可以定义 @Param({1,2,3,4,......,25}).