在maven测试范围内从日食运行卡钳 [英] Running caliper from eclipse in maven's test scope

查看:193
本文介绍了在maven测试范围内从日食运行卡钳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我不想将我的基准代码和依赖关系移动到 main scope,因为它是测试代码!

解决方案

您应该可以使用 Maven Exec插件。对于我的项目,我选择使用maven命令 mvn compile -P benchmarkmarks 来运行基准配置文件。



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

 <型材> 
<个人资料>
< id>基准< / id>
< build>
< plugins>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
< version> 1.2.1< / version>
<执行>
< 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> 标签:

 <处决> 
< 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 以上)可以找到这里和更多的运行时选项(如 - instrument 以上)可以找到 here



然后,如果你使用Eclipse m2 Maven插件,您可以右键单击项目文件夹,然后选择运行为... - > Maven Build ... ,并在目标输入框中输入如 clean install 基准测试个人资料输入框中,然后点击运行应该看到Eclipse控制台中的输出。



重要的是要注意,我使用了Caliper的本地快照构建,通过使用 git克隆https://code.google.com/p/caliper/ ,这是为了利用最新的API,在此信中推荐使用。


I have a Java project in Eclipse, with JUnit tests in my src/test directory. I've also added a class to my tests with Caliper microbenchmarks, and I'd like to be able to run these tests from within Eclipse.

As the Caliper code is test code, I've added Caliper as a dependency in Maven in test scope. That makes it show up in the classpath when I run JUnit tests, but I can't see a way to run an arbitrary class with test dependencies in the classpath. What I tried doing was adding a new Run Configuration for a Java Application, thinking I could launch CaliperMain with the right class as a parameter, but the Caliper jar is not on the classpath and I can't see how to add it.

I don't want to move my benchmark code and dependency into the main scope, as it's test code! It seems seriously overkill to move it into a completely separate project.

解决方案

You should be able to do this with the Maven Exec Plugin. For my project, I opted to make a benchmark profile that can be run with the maven command mvn compile -P benchmarks.

To configure something like this, you can add something along the lines of the following to your pom.xml, specifying scope of the classpath as test using the <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>

Alternatively, if you'd like to specify a lot of options for caliper, it is probably easier to use the <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>

More configuration options (like -Cinstrument.allocation.options.trackAllocations above) can be found here and more runtime options (like --instrument above) can be found here.

Then, if you are using the Eclipse m2 Maven plugin, you can right-click on your project folder and select Run as... -> Maven Build... and enter something like clean install in the Goals input box and benchmarks in the Profiles input box and click Run and you should see the output in your Eclipse console.

It's important to note that I used a local snapshot build of Caliper by checking out the source using git clone https://code.google.com/p/caliper/, which is recommended at the time of this post in order to take advantage of the latest API.

这篇关于在maven测试范围内从日食运行卡钳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆