Benchmark Inheritance

Alex Averbuch alex.averbuch at neotechnology.com
Thu Jun 23 00:32:30 UTC 2016


--- Update ---
After replacing this:

    // PropertyReadBenchmark.propertyDefinition()
    protected PropertyDefinition propertyDefinition()
    {
        throw new RuntimeException( "Should never happen?" );
    }

With this:

    // PropertyReadBenchmark.propertyDefinition()
    protected PropertyDefinition propertyDefinition()
    {
        new PropertyDefinition( "int", ValueGenerator.INTEGER );
    }

The benchmarks get further, expectedly, but they still crash at the
beginning of the second benchmark.
Cause of error is that the database is trying to open a store that is
already open (was not shutdown after the last time it was opened).
Out of desperation I added printouts to get a clearer understanding of the
lifecycle.
This is what I see:

                                              <------ (1) why do these
first series of calls occur, when all involved classes are abstract?
BaseBenchmark()
PropertyReadBenchmark()
PropertyReadBenchmark.propertyDefinition()
BaseBenchmark.setup()                                  <------ (2) like
(1), why is this even called on an abstract class (PropertyReadBenchmark)?
PropertyReadBenchmark.getConfig()
                                                     <------ (3) why was
BaseBenchmark.tearDown() not called here? This is cause of error I am
guessing.

BaseBenchmark()
PropertyReadBenchmark()
IntPropertyRead.propertyDefinition()
IntPropertyRead()
BaseBenchmark.setup()
PropertyReadBenchmark.getConfig()

On Wed, Jun 22, 2016 at 4:05 PM, Alex Averbuch <
alex.averbuch at neotechnology.com> wrote:

> FYI, replacing this:
>
>     protected abstract PropertyDefinition propertyDefinition();
>
> With this:
>
>     protected PropertyDefinition propertyDefinition()
>     {
>         throw new RuntimeException( "Should never happen?" );
>     }
>
> Let's the project successfully compile but then fails at runtime because,
> it turns out, PropertyReadBenchmark.propertyDefinition() is called even
> though PropertyReadBenchmark is abstract.
>
> --- The Error ---
>
> java.lang.RuntimeException: Should never happen?
>     at
> org.me.bench.PropertyReadBenchmark.propertyDefinition(PropertyReadBenchmark.java:57)
>     at
> org.me.bench.PropertyReadBenchmark.<init>(PropertyReadBenchmark.java:38)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B1.<init>(PropertyReadBenchmark_jmhType_B1.java:3)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B2.<init>(PropertyReadBenchmark_jmhType_B2.java:3)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B3.<init>(PropertyReadBenchmark_jmhType_B3.java:2)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType.<init>(PropertyReadBenchmark_jmhType.java:2)
>     at
> org.me.bench.generated.IntPropertyRead_getProperty_jmhTest._jmh_tryInit_f_PropertyReadBenchmark3_G(IntPropertyRead_getProperty_jmhTest.java:482)
>     at
> org.me.bench.generated.IntPropertyRead_getProperty_jmhTest.getProperty_SampleTime(IntPropertyRead_getProperty_jmhTest.java:266)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:497)
>     at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:430)
>     at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:412)
>     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
>     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
>     at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
>     at java.lang.Thread.run(Thread.java:745)
>
>
> Benchmark had encountered error, and fail on error was requested
> Exception in thread "main" java.lang.RuntimeException: Error running JMH
> benchmark
>     at org.me.bench.client.RunCommand.run(RunCommand.java:156)
>     at org.me.bench.client.Main.main(Main.java:17)
> Caused by: org.openjdk.jmh.runner.RunnerException: Benchmark caught the
> exception
>     at org.openjdk.jmh.runner.Runner.runBenchmarks(Runner.java:558)
>     at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:313)
>     at org.openjdk.jmh.runner.Runner.run(Runner.java:206)
>     at org.me.bench.client.RunCommand.run(RunCommand.java:144)
>     ... 1 more
> Caused by: java.lang.RuntimeException: Should never happen?
>     at
> org.me.bench.PropertyReadBenchmark.propertyDefinition(PropertyReadBenchmark.java:57)
>     at
> org.me.bench.PropertyReadBenchmark.<init>(PropertyReadBenchmark.java:38)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B1.<init>(PropertyReadBenchmark_jmhType_B1.java:3)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B2.<init>(PropertyReadBenchmark_jmhType_B2.java:3)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType_B3.<init>(PropertyReadBenchmark_jmhType_B3.java:2)
>     at
> org.me.bench.generated.PropertyReadBenchmark_jmhType.<init>(PropertyReadBenchmark_jmhType.java:2)
>     at
> org.me.bench.generated.IntPropertyRead_getProperty_jmhTest._jmh_tryInit_f_PropertyReadBenchmark3_G(IntPropertyRead_getProperty_jmhTest.java:482)
>     at
> org.me.bench.generated.IntPropertyRead_getProperty_jmhTest.getProperty_SampleTime(IntPropertyRead_getProperty_jmhTest.java:266)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:497)
>     at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:430)
>     at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:412)
>     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
>     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
>     at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
>     at java.lang.Thread.run(Thread.java:745)
>
> On Wed, Jun 22, 2016 at 2:51 PM, Alex Averbuch <
> alex.averbuch at neotechnology.com> wrote:
>
>> I would like for multiple "benchmark" classes to inherit from one base
>> benchmark, where the base benchmark contains all @benchmark methods, and
>> child classes specify the actual data to run against (it is a database
>> benchmark).
>>
>> I've read through the JMH examples. From what I can tell the pattern I am
>> using "should" work, but it does not. My code (three-class hierarchy) & the
>> error are below.
>>
>> Any ideas?
>>
>> Thanks in advance!
>>
>> --- The Error ---
>>
>> [ERROR] Failed to execute goal
>> org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
>> (default-compile) on project jmh-benchmarks: Compilation failure
>> [ERROR]
>> /Users/me/IdeaProjects/jmh-benchmarks/target/generated-sources/annotations/org/me/bench/generated/PropertyReadBenchmark_jmhType_B1.java:[3,8]
>> org.me.bench.generated.PropertyReadBenchmark_jmhType_B1 is not abstract and
>> does not override abstract method propertyDefinition() in
>> org.me.bench.PropertyReadBenchmark
>>
>>
>> --- The Code ---
>>
>>
>> @State( Scope.Benchmark )
>> public abstract class BaseBenchmark
>> {
>>     protected Database db;
>>
>>     @Setup
>>     public void setUp()
>>     {
>>         db = new DataGenerator().generate(getConfig());
>>     }
>>
>>     protected DataGeneratorConfig getConfig()
>>     {
>>         return new DataGeneratorConfigBuilder().build();
>>     }
>>
>>     @TearDown
>>     public void tearDown()
>>     {
>>         db.shutdown();
>>     }
>> }
>>
>> public abstract class PropertyReadBenchmark extends BaseBenchmark
>> {
>>     private final PropertyDefinition propertyDefinition;
>>
>>     public PropertyReadBenchmark()
>>     {
>>         this.propertyDefinition = propertyDefinition();
>>     }
>>
>>     @Override
>>     protected DataGeneratorConfig getConfig()
>>     {
>>         return new DataGeneratorConfigBuilder()
>>                 .withProperties( propertyDefinition )
>>                 .build();
>>     }
>>
>>     // NOTE: causes compilation failure!
>>     protected abstract PropertyDefinition propertyDefinition();
>>
>>     @State( Scope.Thread )
>>     public static class TxState
>>     {
>>         Transaction tx;
>>
>>         @Setup
>>         public void setUp( PropertyReadBenchmark benchmarkState )
>>         {
>>             this.tx = benchmarkState.db.beginTx();
>>         }
>>
>>         @TearDown
>>         public void tearDown()
>>         {
>>             tx.close();
>>         }
>>
>>     }
>>
>>     @Benchmark
>>     public Object getProperty( TxState txState )
>>     {
>>         return db.getProperty( propertyDefinition.key() );
>>     }
>> }
>>
>> public class IntPropertyRead extends PropertyReadBenchmark
>> {
>>     @Override
>>     protected PropertyDefinition propertyDefinition()
>>     {
>>         return new PropertyDefinition( "int", ValueGenerator.INTEGER );
>>     }
>> }
>>
>>
>>
>>
>


More information about the jmh-dev mailing list