Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of "Method parameters should be @State classes" in JMH when parameters come from another method?

I'm working on a maven project. The scenario is something like below...

class Test {

    public void applyAll() {
        ....................
        ....................
        Collection<Migratable> applicableUpdates = SomeOtherClass.getApplicableUpdates();
        doUpdate(applicableUpdates);

    }

    @Benchmark
    public void apply(Migratable m) {
        ....................
        ....................
    }

    private void doUpdate(Collection<Migratable> applicableUpdates) throws Exception {
        for (Migratable m : applicableUpdates) {
            try {

                apply(m);

            } catch (Exception e) {
                logger.error("Falid to apply migration {}" + m, e);
                throw e;
            }
        }
    }
}

I need to compute how long it takes to execute each migration. Simply I need to compute the execution time of apply(Migratable m) method.
Now, when I build my project using "mvn clean install", build failed and it shows "Method parameters should be @State classes".

Here, parameter comes from another method doUpdate(Collection applicableUpdates) [see the scenario]. So how can I get rid of this error in given scenario?

like image 790
neelrotno Avatar asked Apr 06 '17 09:04

neelrotno


1 Answers

There are quite a few problems in your setup here and it seems you haven't actually looked at the samples of JMH; and I strongly advise you to do that.

A few notes...

1) You @Benchmark method returns void - it should return something; otherwise use BlackHoles (this is in the samples).

2) If parameters comes from another method it means that method should be a @SetUp method (this is in the samples)

3) The error that you are seeing has to do with the fact that your Migratable is not actually a @State class (this is again in the samples!)

At this point I can't stress that enough - but look and understand the samples. It's not going to be easy, this is micro-benchmark and as much as JMH tries to make things easier for us by hiding all the very complicated code, it still requires us to conform to the rules that exist there.

like image 146
Eugene Avatar answered Jan 02 '23 11:01

Eugene