Control the order of methods using JMH
Asked Answered
M

1

8

I'm using JMH benchmark in my project. I have method 1 and method 2 annotated with @Benchmark. I want the order to be: method1, method2, mehod1, method2... and so on... Which means: I wand that method 2 will immediately follow method 1. Is there a way to do so?

Thank you!

Mightily answered 28/3, 2016 at 11:25 Comment(3)
Shouldn't you get the same result regardless of order?Fadge
Well, method 2 needs to use an ArrayList built in method 1Mightily
Hello @TaliG, I'm wondering how to make sure the method 2 execute after the execution of method 1? I tried to implement this, but method 1 and 2 will execute asynchronously. In this situation I can't control the execution order.Affiance
S
6

The order in which JMH executes @Benchmark methods is usually irrelevant: the runs are isolated from each other, and so results are independent. But in case you still want the particular order, then @Benchmark-s are executed in the lexicographical order, hence, rename your methods in that order.

Smalley answered 28/3, 2016 at 21:27 Comment(2)
This works (so thank you for the answer), but now my methods are called _0_january(), _1_feburary(), _2_march(), ... That's ugly! JMH should probably support @Benchmark(reportOrder = 2) String march() {} instead.Dualistic
I agree with Geoffrey. It would be nice to control the benchmark execution/reporting order. In my case, I'd want @Benchmark methods to get executed in the order they are present in the class file. I have 2 use-cases: 1. I am profiling the code and I want to quickly control which method runs first so I can profile it. 2. The methods are related. I want to report benchmark numbers in an order that is more intuitive for readers. I'd prefer controlling this through method ordering than annotations because the parameters are error-prone (e.g. two annotations may have the same parameter value).Barbi

© 2022 - 2024 — McMap. All rights reserved.