How to run JMH Benchmark in Maven Using exec: java instead of exec: exec?

This post on javapapers.com shows how to run JMH in Maven by typing mvn exec:exec. Launching JMH on Maven is quite convenient, as you can easily launch it from the Eclipse launch configuration or even into the Maven phase.

However, there are two problems with this setup:

  • When you kill Maven, JMH will continue to run in the background, as it exec:execlaunches it in a separate virtual machine.

  • Typically, JMH launches another virtual machine to run tests, so you will end up with at least three virtual machines at the same time.

Fortunately, Exec Maven Plugin comes with a second purpose exec:java, which executes the main class directly in Maven VM entries. However, when I tried to configure Maven to run JMH using exec:java, the benchmark crashes due to missing classes:

# JMH 1.11.3 (released 40 days ago)
# VM version: Error: Could not find or load main class org.openjdk.jmh.runner.VersionMain
# VM invoker: C:\Program Files\Java\jdk1.7.0\jre\bin\java.exe
[...]
# Run progress: 0.00% complete, ETA 00:02:40
# Fork: 1 of 1
Error: Could not find or load main class org.openjdk.jmh.runner.ForkedMain
<forked VM failed with exit code 1>

Here is the relevant part pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <configuration>
        <mainClass>my.Benchmark</mainClass>
    </configuration>
</plugin>

And this is how I run JMH from my.Benchmark:

public static void main(String[] args) throws RunnerException {
    Options options = new OptionsBuilder().include(my.Benchmark.class.getSimpleName())
            .forks(1).build();
    new Runner(options).run();
}

I understand that JMH uses a system property java.class.pathto determine the path to the forked VM classes and that this property does not contain the dependencies of the Maven project. But what is the preferred way to handle this?

+4
source share
2 answers

, POM-, java.class.path classpath runtime " " :

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>build-classpath</id>
            <goals>
                <goal>build-classpath</goal>
            </goals>
            <configuration>
                <includeScope>runtime</includeScope>
                <outputProperty>depClasspath</outputProperty>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
        <mainClass>my.Benchmark</mainClass>
        <systemProperties>
            <systemProperty>
                <key>java.class.path</key>
                <value>${project.build.outputDirectory}${path.separator}${depClasspath}</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>
+2

- "" my.Benchmark JMH :

URLClassLoader classLoader = (URLClassLoader) my.Benchmark.class.getClassLoader();
StringBuilder classpath = new StringBuilder();
for(URL url : classLoader.getURLs())
    classpath.append(url.getPath()).append(File.pathSeparator);
System.setProperty("java.class.path", classpath.toString());

, , , ...

+3

Source: https://habr.com/ru/post/1630044/


All Articles