Annotation Type Benchmark
-
@Target(METHOD) @Retention(RUNTIME) public @interface Benchmark
Annotation for benchmark methods. To write a benchmark:- Annotate one or more methods with this annotation.
- Annotate any fields with @
Paramthat should have parameter values injected (see @Paramfor more details) - Optionally use
BeforeExperimentandAfterExperimenton setup and teardown methods
Since many benchmarks may execute in a shorter duration than is accurately measured by available timers, benchmark methods may take either an
intorlongargument representing a number of repetitions to perform in a given execution. It is critical that the work done in the benchmark method scale linearly to the number of repetitions.Benchmark methods may return any value. It will be ignored.
This class is instantiated and injected only once per child VM invocation, to measure one particular combination of parameters.
For example:
{@code public final class MyBenchmark { {@literal @}Param FeatureEnum feature; {@literal @}Param({"1", "10", "100"}) int size; private MyObject objectToBenchmark; {@literal @}BeforeExperiment void initializeObject() { objectToBenchmark = new MyObject(size); } {@literal @}Benchmark int foo(int reps) { MyObject object = objectToBenchmark; // copy to local to avoid field access overhead int dummy = 0; for (int i = 0; i < reps; i++) { dummy += object.foo(feature); } // return a dummy value so the JIT compiler doesn't optimize away the entire method. return dummy; } {@literal @}Benchmark int bar() { // benchmark another operation of MyObject that doesn't require a reps parameter } }The benchmark class MyBenchmark has two benchmark methods ({@code foo} and {@code bar}) and two {@link Param Params} ({@code feature} and {@code size}). For each experiment performed by Caliper (e.g. {@code foo} with {@code feature == FeatureEnum.A} and {@code size == 100}), {@code initializeObject} will be called exactly once, but {@code foo} may be called many times.