在maven的测试范围内从eclipse运行caliper

And*_*ett 6 java eclipse maven caliper

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

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

我不想将我的基准代码和依赖项移动到main作用域中,因为它是测试代码!将它移到一个完全独立的项目中似乎非常严重.

ree*_*ees 5

您应该可以使用Maven Exec插件执行此操作.对于我的项目,我选择制作可以使用maven命令运行的基准测试配置文件mvn compile -P benchmarks.

要配置类似这样的内容,您可以在以下行中添加一些内容pom.xml,使用标记将类路径的范围指定为test<classpathScope>:

<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>
Run Code Online (Sandbox Code Playgroud)

或者,如果您想为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>
Run Code Online (Sandbox Code Playgroud)

更多的配置选项(如-Cinstrument.allocation.options.trackAllocations上述),可以发现在这里和多个运行时的选项(如--instrument上述),可以发现在这里.

然后,如果你正在使用Eclipse平方米Maven插件,你可以在你的项目文件夹右键单击并选择Run as... -> Maven Build...和输入类似clean installGoals输入框,benchmarksProfiles输入框中,然后点击Run,您应该看到在Eclipse控制台输出.

重要的是要注意我通过检查使用的源来使用Caliper的本地快照构建git clone https://code.google.com/p/caliper/,这是为了利用最新的API而在本文发布时推荐的.