我在 Eclipse 中有一个 Java 项目,在我的 src/test
目录中有 JUnit 测试。我还使用 Caliper 微基准测试向我的测试添加了一个类,我希望能够从 Eclipse 中运行这些测试。
由于 Caliper 代码是测试代码,我在 test
范围内添加了 Caliper 作为 Maven 的依赖项。这使得它在我运行 JUnit 测试时显示在类路径中,但我看不到在类路径中运行具有测试依赖项的任意类的方法。我尝试做的是为 Java 应用程序添加一个新的运行配置,认为我可以使用正确的类作为参数启动 CaliperMain
,但是 Caliper jar 不在类路径上,我看不到如何添加。
我不想将我的基准测试代码和依赖项移动到 main
范围内,因为它是测试代码!将它移到一个完全独立的项目中似乎严重过头了。
最佳答案
您应该能够使用 Maven Exec Plugin 执行此操作.对于我的项目,我选择制作一个可以使用 maven 命令运行的基准配置文件 mvn compile -P benchmarks
.
要配置这样的东西,您可以将以下内容添加到您的 pom.xml
,使用 <classpathScope>
将类路径的范围指定为 test标签:
<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>
或者,如果您想为 caliper 指定很多选项,使用 <arguments>
可能更容易。标签:
<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>
可以在here 中找到更多配置选项(如上面的-Cinstrument.allocation.options.trackAllocations
)更多运行时选项(如上面的 --instrument
)可以在 here 中找到.
然后,如果您使用的是 Eclipse m2 Maven 插件,您可以右键单击您的项目文件夹并选择 Run as... -> Maven Build...
并输入类似 clean install
的内容在Goals
输入框和benchmarks
在Profiles
输入框点击Run
您应该会在 Eclipse 控制台中看到输出。
重要的是要注意,我通过使用 git clone https://code.google.com/p/caliper/
检查源代码使用了 Caliper 的本地快照构建。 ,这是在本文发布时推荐的,以便利用最新的 API。
关于java - 在 Maven 的测试范围内从 Eclipse 运行卡尺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405740/