How do you specify a JMH microchip test to run WITHOUT using a resource file?

jmh 0.6. I have jmh-core, jmh-generator-annprocess, jmh-generator-reflection as dependencies.

Firstly, the documentation is bad, unfortunately. First, I use gradle, not maven, so using the maven archetype is not an option.

Secondly, I want to use the Java API, not the command line.

My very simple code:

public final class TestBenchmark { private static final int COUNT = 100_000; private static final List<Integer> LIST = new ArrayList<>(); static { for (int i = 0; i < COUNT; i++) LIST.add(i); } @GenerateMicroBenchmark public void foreachLoop() { int dummy; for (final int i: LIST) dummy = i; } @GenerateMicroBenchmark public void forLoop() { int dummy; final int size = LIST.size(); for (int i = 0; i < size; i++) dummy = LIST.get(i); } public static void main(final String... args) throws RunnerException { final Options options = new OptionsBuilder() .forks(1) .warmupIterations(1) .measurementIterations(20) .verbosity(VerboseMode.EXTRA) .build(); new Runner(options).run(); } } 

Since I don't have .include() , this means .* As a regular expression, so all benchmarks. This is the only class I have in my project.

But no: "no benchmarks."

So, as a last resort, I tried to create a META-INF/MicroBenchmarks , as suggested elsewhere; content, class name:

 com.github.parboiled1.grappa.TestBenchmark 

but it does not work:

 Exception in thread "main" java.lang.IllegalStateException: Mismatched format for the line: com.github.parboiled1.grappa.TestBenchmark 

and the format of this file is, of course, not documented.

But I do not want to use this file to start; I want to specify a list of classes to run.

How to do it?

+7
java jmh
source share
2 answers

Your problem is not with inappropriate criteria, but with the wrong assembly configuration that prevented JMH from generating and compiling synthetic control code for you. You must allow annotation processors to run, and the rest of the assembly should pack the generated source and resources into a complete set, even if you intend to use the API afterwards. A project created by the Maven archetype does just that. There is also an Ant sample that determines exactly what should happen to other build systems. If you want to with Gradle, try this .

PS In JMH, META-INF / MicroBenchmarks is an internal API, and it is subject to change without notice. No surprises you can not use.

+5
source share

I use Maven, so this is a Maven solution, but I'm sure you can adapt it to Gradle if you follow these steps. I must admit that the greatness of JMH is overshadowed by a lack of documentation.

I could not get it to work correctly at first, so in the end I created JUnit test cases that run the test. For me, there is no main one, since for Jenkins it is easier, and I do not have a created project of a bank.

  • You execute the APT processor either by code or from a script assembly. In Maven, it works out of the box .
  • You can verify success if the JMH source code is generated.
  • Now the JMH source code needs to be compiled. Maven makes out of the box. Not sure about Gradle.
  • Verify that class files exist.
  • Now you can execute the main method.

This profile tells maven to add * Benchmark.java to error-free execution.

 <profile> <id>benchmark</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Benchmark.java</include> </includes> </configuration> </plugin> </plugins> </build> </profile> 

when I start Maven with a profile test. Then the benchmarks are performed.

+1
source share

All Articles