java - 在 Eclipse 下运行 JMH 基准测试

标签 java eclipse jmh

我正在尝试在 Eclipse 下开始使用 JMH。我可以构建一个 jar 以从命令行执行,但也希望我能够直接在 Eclipse 中运行它以便于开发。

目前我得到:

java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList

我使用的是 http://nitschinger.at/Using-JMH-for-Java-Microbenchmarking/ 中的简单入门案例:

public class MyBenchmark {

    @Benchmark
    public void testMethod() {
        // This is a demo/sample template for building your JMH benchmarks. Edit as needed.
        // Put your benchmark code here.
    }

    public static void main(String... args) throws Exception {
          Options opts = new OptionsBuilder()
              .include(".*")
              .warmupIterations(10)
              .measurementIterations(10)
//            .jvmArgs("-server")
              .forks(1)
//            .outputFormat(OutputFormatType.TextReport)
              .build();

          new Runner(opts).run();
        }    
}

我生成了 JMH 文档中指定的 POM,并添加了 JMH Unable to find the resource: /META-INF/BenchmarkList 中指定的 exec-maven-plugin。 :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sample</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>JMH benchmark sample: Java</name>

    <!-- This is the demo/sample template build script for building Java benchmarks 
        with JMH. Edit as needed. -->

    <prerequisites>
        <maven>3.0</maven>
    </prerequisites>

    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>${jmh.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>${jmh.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- JMH version to use with this project. -->
        <jmh.version>1.15</jmh.version>

        <!-- Java source/target to use for compilation. -->
        <javac.target>1.8</javac.target>

        <!-- Name of the benchmark Uber-JAR to generate. -->
        <uberjar.name>benchmarks</uberjar.name>
    </properties>

    <build>
        <plugins>
<!--            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>${javac.target}</compilerVersion>
                    <source>${javac.target}</source>
                    <target>${javac.target}</target>
                </configuration>
            </plugin> -->
            <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. https://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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <compilerVersion>${javac.target}</compilerVersion>
                    <source>${javac.target}</source>
                    <target>${javac.target}</target>
                </configuration>
            </plugin>

<!--            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>com.javapapers.java.benchmark.jmh.JMHHelloWorld</argument>
                    </arguments>
                </configuration>
            </plugin> -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>run-benchmarks</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <classpathScope>test</classpathScope>
                            <executable>java</executable>
                            <arguments>
                                <argument>-classpath</argument>
                                <classpath />
                                <argument>org.openjdk.jmh.Main</argument>
                                <argument>.*</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
                <plugin>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>2.9.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.3</version>
                </plugin>
                <plugin>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>2.2.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>  
</project>

我正在启动 Eclipse 运行配置,如下所示:

enter image description here

我可以看到缺少的资源/META-INF/BenchmarkList/在 jar 中,但/not/在/target 目录中。

是否可以在 Eclipse 下执行此操作?

最佳答案

我一直在 Eclipse 中运行 JMH 测试(出于调试目的),但我在运行测试之前单独运行 maven build,而测试是作为 Java 应用程序执行的:

第一步:构建Maven项目

  1. 右键单击项目,
  2. 选择运行方式,
  3. 选择 Maven Build 并将目标指定为 clean install

(这一步也可以通过运行相同的目标在命令行中完成)

第 2 步:运行测试

  1. 右键单击项目,
  2. 选择运行方式,
  3. 选择 Java Application 并选择 Main - org.openjdk.jmh 或您创建的 main

关于java - 在 Eclipse 下运行 JMH 基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311990/

相关文章:

profiler - JMH 的 DTraceAsmProfiler 在 Mac 上失败并显示 '[sudo: a password is required'

java - JMH AnnotationProcessor NullPointerException

java - 谁能帮我理解这段代码?

java - 我可以进入高级类吗?

java - Eclipse J2EE Preview 从 .metadata 目录而不是 m2e-wtp/web-resources 读取未过滤的资源

用于 JAX-WS org.codehaus.mojo 的 Eclipse m2e 连接器

linux - java 。 Log4j。 openjdk-7。问题看起来像无限循环

java - 将对象 arrayList 对象转换为 Integer

java - 在哪里可以下载 JPA 2.0 的 JavaDoc?

java - JMH 和 Swing 导致 CompletionFailure