您应该能够使用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。