Micro benchmarking a loop with different values in JMH
Asked Answered
F

1

8

It is well known that using a loop inside your JMH benchmark is not a good idea because it will be optimized by the JIT compiler and should therefore be avoided. Is there a way to feed my JMH benchmark methods with different values of int inputs (list of inputs) without using a loop.

Frontwards answered 12/3, 2015 at 14:23 Comment(0)
G
20

Have a look at this example in the JMH documentation. You can use the @Param annotation on a field in order to tell JMH to inject the values of this annotation:

@Param({"1", "2"})
public int arg;

@Benchmark
public int doBenchmark() {
  return doSomethingWith(arg);
}

The benchmark is then run for both the values 1 and 2.

Note how, if the annotated field is not a String but a primitive, the values are parsed prior to assigment and are assigned in their converted forms. If you have multiple fields with the @Param annotation, JMH will run the benchmark with any possible permutation of the field values.

You can also overridde the value assignment when defining a JMH runner.

Geraldo answered 13/3, 2015 at 8:2 Comment(4)
Stupid question: Let's say I have 50-60 thousand values representing a real distribution of say around a thousand distinct values, and I'd like my micro benchmark to reflect this load. Let's say the tested code it's detecting certain patterns in URLs, just as an example, and the shape of the data is important in order to find a globally optimal implementation. What should I do?Gower
@Gower I don't know how good of a solution it is, but you can prepare an array of parameters in a @State object along with a counter. And you can move this counter in a @TearDown(Level.Iteration).Glutamine
or within benchmarked method itself, as JMH examples suggest.Glutamine
What if i need parameters that aren't primatives?Tubercle

© 2022 - 2024 — McMap. All rights reserved.