7

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?

Eugene
  • 117,005
  • 15
  • 201
  • 306
neelrotno
  • 159
  • 2
  • 8
  • You look up the JMH documentation and you run the performance test with JMH. If you are having trouble with that, you can come back to StackOverflow with a *specific* question. – Erwin Bolwidt Apr 06 '17 at 09:40
  • Thanks @ErwinBolwidt. Think this time you'll understand the problem I'm facing. Actually, I'm stuck with this problem. – neelrotno Apr 06 '17 at 11:49

1 Answers1

5

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.

Luke_P
  • 659
  • 2
  • 7
  • 23
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Thanks @Eugene `Migratable` is an interface and State does not support abstract classes. And seeing [this](http://hg.openjdk.java.net/code-tools/jmh/file/36a2ee9a075e/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_04_DefaultState.java) sample, it seems to me that `@Benchmark` can return void. Actually I'm new on this topic and still stuck. – neelrotno Apr 09 '17 at 06:33
  • 1
    @neelrotno for void methods read the samples about `BlackHole`. You will see that the sample that you provided is said to *measure wrong*. Its ok being new to the subject, we all were at some point in time. I use JMH for a lot of time and still get things wrong sometimes... – Eugene Apr 09 '17 at 06:50
  • 1
    @Eugene...actually I cannot apply `Blackholes' in my project seeing the sample. Now please, consider...you have this Test class. If you have to compute how long `apply(Migratable m)` method takes in each time it is being called, how would you do that? It'll be helpful for me if you give the exact code with appropriate annotation(s) – neelrotno Apr 09 '17 at 10:19
  • Here, `applyAll()` method get applicableUpdates from another class and pass it to `doUpdate(Collection applicableUpdates)`. `doUpdate(Collection applicableUpdates)` method pass each of the element of the list `aplicableUpdates` to `apply(Migratable m)` method. `apply(Migratable m)` method just update DB and return void. – neelrotno Apr 09 '17 at 10:20