Benchmark Inheritance

Aleksey Shipilev aleksey.shipilev at oracle.com
Thu Jun 23 09:41:06 UTC 2016


On 06/23/2016 12:51 AM, Alex Averbuch 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?

You are caught in a very unlucky place with benchmark hierarchy,
abstract classes, and @State DAGs. The problem goes like this:

IntProperty.getProperty(TxState txState):
  implicit state: IntPropertyRead instance (this)
  states: TxState

...resolution continues:

TxState.setUp:
  state: PropertyReadBenchmark

The underlying reason is that @State dependency matching machinery is
oblivious of class hierarchies: IntPropertyRead and
PropertyReadBenchmark are *distinct* states for it.

So it fails when PropertyReadBenchmark is abstract: it cannot be a
@State on its own -- only its subclasses can be instantiated. The
compiler would fail to produce an auxiliary JMH classes for
PropertyReadBenchmark, without having all the abstract methods implemented.

It fails if make PropertyReadBenchmark non-abstract: the compilation
will proceed, and the dependency matching machinery will feed the
*PropertyReadBenchmark* instance into TxState.setUp, not the
*IntPropertyRead* one. Of course, it you put exception into
PropertyReadBenchmark.propertyDefinition, it would throw.

I am not sure it is easily fixable, so the immediate workaround it to
avoid the complex hierarchy/dependency constructions.

Thanks,
-Aleksey



More information about the jmh-dev mailing list