【问题标题】:Running caliper from eclipse in maven's test scope在 Maven 的测试范围内从 Eclipse 运行 caliper
【发布时间】:2013-08-26 16:14:42
【问题描述】:

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

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

我不想将我的基准代码和依赖项移动到main 范围内,因为它是测试代码!将它移到一个完全独立的项目中似乎严重过度。

【问题讨论】:

  • Eclipse 运行配置只列出了我的类路径的“Maven 依赖项”。它似乎没有分离测试和非测试依赖项。您是否验证过 Caliper 确实包含在内?
  • @gk5885,它在包资源管理器中可用,在运行“JUnit”类型的配置时在类路径中可用,但在运行“Java 应用程序”类型的配置时在类路径中不可用。
  • 我不确定我是否完全理解您的场景,尤其是“在类路径中运行具有测试依赖项的任意类”。您不能只创建并运行一个 JUnit 测试,它会完全按照 CaliperMain 在其 main 方法中通常会执行的操作吗?

标签: java eclipse maven caliper


【解决方案1】:

您应该可以使用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>

或者,如果您想为卡尺指定很多选项,使用&lt;arguments&gt; 标签可能更容易:

<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,更多运行时选项(如上面的--instrument)可以找到here

然后,如果您使用的是 Eclipse m2 Maven 插件,您可以右键单击您的项目文件夹并选择 Run as... -&gt; Maven Build... 并在 Goals 输入框中输入 clean installbenchmarks 在 @ 987654336@ 输入框并单击Run,您应该会在 Eclipse 控制台中看到输出。

请务必注意,我通过使用 git clone https://code.google.com/p/caliper/ 检查源代码来使用 Caliper 的本地快照构建,在本文发布时建议使用此方法以利用最新的 API。

【讨论】:

  • 这给了我一个错误:[stderr] CICompilerCount of 1 is invalid; must be at least 2 [stderr] Error: Could not create the Java Virtual Machine. [stderr] Error: A fatal exception has occurred. Program will exit. 请给我一个如何解决这个问题的指针,即如何将所需的参数传递给 JVM?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2013-12-11
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 2014-05-22
  • 2011-07-24
相关资源
最近更新 更多