在 Maven 的测试范围内从 Eclipse 运行 caliper

2024-02-19

我在 Eclipse 中有一个 Java 项目,在我的项目中包含 JUnit 测试src/test目录。我还使用 Caliper 微基准测试添加了一个类,并且我希望能够从 Eclipse 中运行这些测试。

由于 Caliper 代码是测试代码,因此我在 Maven 中添加了 Caliper 作为依赖项test范围。这使得当我运行 JUnit 测试时它会显示在类路径中,但我看不到在类路径中运行具有测试依赖项的任意类的方法。我尝试做的是为 Java 应用程序添加一个新的运行配置,认为我可以启动CaliperMain使用正确的类作为参数,但 Caliper jar 不在类路径上,我不知道如何添加它。

我不想将我的基准代码和依赖项移至main范围,因为它是测试代码!将其转移到一个完全独立的项目中似乎太过分了。


您应该能够使用Maven 执行插件 http://mojo.codehaus.org/exec-maven-plugin/index.html。对于我的项目,我选择制作一个可以使用 maven 命令运行的基准配置文件mvn compile -P benchmarks.

要配置类似的内容,您可以将以下内容添加到您的pom.xml,将类路径的范围指定为test使用<classpathScope> tag:

<profiles>
    <profile>
        <id>benchmarks</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <id>caliper</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <classpathScope>test</classpathScope>
                                <mainClass>com.google.caliper.runner.CaliperMain</mainClass>
                                <commandlineArgs>com.stackoverflow.BencharkClass,com.stackoverflow.AnotherBenchmark</commandlineArgs>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

或者,如果您想为卡尺指定很多选项,那么使用<arguments> tags:

<executions>
    <execution>
        <id>caliper</id>
        <phase>compile</phase>
        <goals>
            <goal>java</goal>
        </goals>
        <configuration>
            <classpathScope>test</classpathScope>
            <mainClass>com.google.caliper.runner.CaliperMain</mainClass>
            <arguments>
                <argument>com.stackoverflow.BencharkClass</argument>
                <argument>--instrument</argument>
                <argument>runtime</argument>
                <argument>-Cinstrument.allocation.options.trackAllocations=false</argument>
            </arguments>
        </configuration>
    </execution>
</executions>

更多配置选项(例如-Cinstrument.allocation.options.trackAllocations上)可以找到here https://code.google.com/p/caliper/source/browse/caliper/src/main/resources/com/google/caliper/config/global-config.properties以及更多运行时选项(例如--instrument上)可以找到here https://code.google.com/p/caliper/wiki/CommandLineOptions.

然后,如果您使用的是 Eclipse m2 Maven 插件,您可以右键单击您的项目文件夹并选择Run as... -> Maven Build...并输入类似的内容clean install in the Goals输入框和benchmarks in the Profiles输入框并点击Run您应该会在 Eclipse 控制台中看到输出。

值得注意的是,我通过使用以下命令查看源代码,使用了 Caliper 的本地快照构建:git clone https://code.google.com/p/caliper/,在撰写本文时推荐这样做,以便利用最新的 API。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Maven 的测试范围内从 Eclipse 运行 caliper 的相关文章

随机推荐