Running benchmark with main method from the generated jar

Aleksey Shipilev aleksey.shipilev at oracle.com
Wed Apr 2 10:11:32 UTC 2014


Hi Luis,

On 04/02/2014 01:58 PM, Luis IBANEZ wrote:
> But that means I lose all the nice command line options provided with the
> jar.

I assume you want the same CLI interface JMH offers for other benchmarks?

> I tried using a Properties file, but:
>   1/ The option parsing is up to me (I still have not checked how to reuse
> whatever option parsing jmh uses though)

See below.

>   2/ I also tried to set other parameters (path to the data used), but I
> need to hardwire the location of the settings file as it seems that the
> method annotated as Trial is executed even before the main (I thought it
> was injected just after the call to Runner.run)

@Setup-s should be executed after main(). Something wicked happens in
your code :)

> Short version of the question: How do I execute code sample [0] from the
> generated jar while maintaining the settings control?

I say like this:

diff -r 3b57fecd61bf
jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_25_API_GA.java
---
a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_25_API_GA.java
Tue Apr 01 21:53:13 2014 +0400
+++
b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_25_API_GA.java
Wed Apr 02 14:07:30 2014 +0400
@@ -31,6 +31,8 @@
 import org.openjdk.jmh.logic.results.RunResult;
 import org.openjdk.jmh.runner.Runner;
 import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.CommandLineOptionException;
+import org.openjdk.jmh.runner.options.CommandLineOptions;
 import org.openjdk.jmh.runner.options.Options;
 import org.openjdk.jmh.runner.options.OptionsBuilder;
 import org.openjdk.jmh.runner.options.VerboseMode;
@@ -82,11 +84,13 @@
      * performance. Try to run the sample and see if it improves
performance.
      */

-    public static void main(String[] args) throws RunnerException {
+    public static void main(String[] args) throws RunnerException,
CommandLineOptionException {
         // These are our base options. We will mix these options into the
         // measurement runs. That is, all measurement runs will inherit
these,
         // see how it's done below.
+
         Options baseOpts = new OptionsBuilder()
+                .parent(new CommandLineOptions(args))
                 .include(".*" + JMHSample_25_API_GA.class.getName() + ".*")
                 .warmupTime(TimeValue.milliseconds(200))
                 .measurementTime(TimeValue.milliseconds(200))

-Aleksey


More information about the jmh-dev mailing list