【问题标题】:How to use externa JARs with JMH如何在 JMH 中使用外部 JAR
【发布时间】:2018-05-27 17:57:17
【问题描述】:

我的 Java 项目使用了一些外部 JAR。为了对项目进行基准测试,我如何将这些添加到 JMH?

例如我应该使用-cp 选项将它们添加到java 命令行吗? (这实际上导致我的环境未找到类错误)

【问题讨论】:

标签: java jmh


【解决方案1】:

您可以像在任何其他项目中一样使用 jar。使用-cp,您可能调用了错误的主类,它应该是org.openjdk.jmh.Main。这是example with maven。注意pom.xml 中的依赖关系和阴影。

我将在这里复制 POM 中的重要部分:

..
<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>
...

【讨论】:

    【解决方案2】:

    使用外部依赖项而不是一个包中的所有内容。 IE。允许热交换罐子。我使用了以下命令。我还通过将类添加到 maven shade 插件的 excludes 部分从 uber-jar 中排除了这个 dep。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 2020-02-22
      • 2015-08-22
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多