Running caliper from eclipse in maven's test scope
Asked Answered
G

1

6

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.

Giselegisella answered 23/8, 2013 at 14:47 Comment(3)
Eclipse run configuration just lists "Maven Dependencies" for my classpath. It doesn't seem to separate test and non-test dependencies. Have you verified that Caliper is actually being included?Afterburning
@gk5885, it's available in the Package Explorer, available on the class path when running a configuration of type 'JUnit' but not available on the class path when running a configuration of type 'Java Application'.Giselegisella
I'm not sure I fully understand your scenario, especially "run an arbitrary class with test dependencies in the classpath". Can't you just create and run a JUnit test which would do exactly what CaliperMain would normally do in its main method?Lydell
D
5

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.

Daphinedaphna answered 20/9, 2013 at 14:14 Comment(1)
This gives me an error: [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. Could you please give me a pointer how to fix this, ie how to pass the needed argument to the JVM?Ut

© 2022 - 2024 — McMap. All rights reserved.