0

My Java project uses a few external JARs. To benchmark the project, how can I add these to JMH?

E.g. Should I add them to the java command line with the -cp option? (which actually results in a class not found error for my env)

bergtwvd
  • 165
  • 1
  • 9
  • is it an option to add those jars as dependency of JMH? For maven it looks like https://stackoverflow.com/questions/15383322/how-do-i-add-a-project-as-a-dependency-of-another-project/15383584#15383584 – VB_ Jun 01 '18 at 13:45

2 Answers2

0

You can use jars just like in any other project. With -cp you probably call wrong main class, it should be org.openjdk.jmh.Main. Here is an example with maven. Note the dependencies and shading in pom.xml.

I'll copy important parts from POM here:

..
<dependencies>
  ..
  <!-- This is the lib I want to add -->
  <dependency>
      <groupId>io.lettuce</groupId>
      <artifactId>lettuce-core</artifactId>
      <version>5.0.3.RELEASE</version>
  </dependency>
  ..
</dependencies>
...
<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <finalName>${uberjar.name}</finalName>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.openjdk.jmh.Main</mainClass>
                </transformer>
              </transformers>
              <filters>
                <filter>
                  <!--
                      Shading signed JARs will fail without this.
                      http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                  -->
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
</plugin>
...
Imaskar
  • 2,773
  • 24
  • 35
0

To use an external dependency rather than the all in one bundle. I.e. to allow hot-swapping the jar. I used the below command. I also excluded this dep from the uber-jar by adding the class to the excludes sections of the maven shade plugin.

java -cp benchmarks.jar:.jar org.openjdk.jmh.Main