Search code examples
javaxmlbenchmarkingmicrobenchmarkjmh

JMH - run a method for multiple times


I have a method in JMH benchmark an XML processor. I want to benchmark it several times with several files by passing the file name to the method parameter but it does not work. Does anyone know how to run a JMH method for says 25 times without having 25 different methods? Thanks.

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);
    }
}

Error:

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

Solution

  • Here is an example of using @Param to benchmark a method multiple times with different parameter. In your case, maybe you can define @Param({1,2,3,4,......,25}).