Benchmark Inheritance

Alex Averbuch alex.averbuch at neotechnology.com
Wed Jun 22 21:51:01 UTC 2016


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