JMH - 多次运行方法

时间:2018-03-19 23:49:13

标签: java xml benchmarking microbenchmark jmh

我在JMH基准测试中有一个XML处理器的方法。我想通过将文件名传递给方法参数来对几个文件进行多次基准测试,但它不起作用。有没有人知道如何运行JMH方法25次而没有25种不同的方法?感谢。

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

1 个答案:

答案 0 :(得分:0)

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