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:Alternatively, if you'd like to specify a lot of options for caliper, it is probably easier to use the
<arguments>
tags: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 likeclean install
in theGoals
input box andbenchmarks
in theProfiles
input box and clickRun
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.