patternjavaMinor
Simple micro-benchmarking library
Viewed 0 times
microsimplebenchmarkinglibrary
Problem
I'm working on a simple micro-benchmarking library in Java.
This library makes it easy to benchmark multiple reference implementations.
You provide the inputs and trigger the calls,
the library takes care of executing N times,
measuring the runs and printing the averaged results.
Essentially, it works like this:
-
Create a dedicated class for the benchmark
-
Prepare input data in the constructor
-
Create one method per reference implementation, annotate with
-
Add a
with an instance of this class as parameter
-
Run the class, find the results on
To set the number of warm-up iterations and iterations,
use the
for example:
The annotations:
The class that runs the benchmarks on a target object passed in, parameterized by annotations:
```
import microbench.api.annotation.Benchmark;
import microbench.api.annotation.MeasureTime;
import microbench.api.annotation.Prepare;
import microbench.api.annotation.Validate;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class BenchmarkRunner {
public static final int DEFAULT_ITERATIONS = 1;
public static final int DEFAULT_WARM_UP_ITERATIONS = 0;
private final Object target;
private final int defaultIterations;
private final int defaultWarmUpIterations;
private final List measureTimeMethods = new ArrayList<>();
private final List prepareMethods = new ArrayList<>();
private final List validateMet
This library makes it easy to benchmark multiple reference implementations.
You provide the inputs and trigger the calls,
the library takes care of executing N times,
measuring the runs and printing the averaged results.
Essentially, it works like this:
-
Create a dedicated class for the benchmark
-
Prepare input data in the constructor
-
Create one method per reference implementation, annotate with
@MeasureTime-
Add a
main method to trigger the benchmark runner,with an instance of this class as parameter
-
Run the class, find the results on
stdoutTo set the number of warm-up iterations and iterations,
use the
@Benchmark annotation on the class,for example:
@Benchmark(iterations = 10, warmUpIterations = 5)The annotations:
@Retention(RetentionPolicy.RUNTIME)
public @interface MeasureTime {
int[] iterations() default {};
int[] warmUpIterations() default {};
}
@Retention(RetentionPolicy.RUNTIME)
public @interface Benchmark {
int iterations() default BenchmarkRunner.DEFAULT_ITERATIONS;
int warmUpIterations() default BenchmarkRunner.DEFAULT_WARM_UP_ITERATIONS;
}The class that runs the benchmarks on a target object passed in, parameterized by annotations:
```
import microbench.api.annotation.Benchmark;
import microbench.api.annotation.MeasureTime;
import microbench.api.annotation.Prepare;
import microbench.api.annotation.Validate;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class BenchmarkRunner {
public static final int DEFAULT_ITERATIONS = 1;
public static final int DEFAULT_WARM_UP_ITERATIONS = 0;
private final Object target;
private final int defaultIterations;
private final int defaultWarmUpIterations;
private final List measureTimeMethods = new ArrayList<>();
private final List prepareMethods = new ArrayList<>();
private final List validateMet
Solution
I like the Idea of this very small framework and it looks like a nice way to measure runtime quickly. I just have some minor comments.
If-Statement
I think the
Code Documentation
Your code is lacking any form of comments in the code. In my humble opinion, at least every public method should be commented. For developing it might be useful on private methods as well. For example to me it is not obvious (by reading the method names) what the difference between
Other Comments
Someone with more expierence implementing anotations than me should probably answer your questions about the annotation implementation, so I'll not comment on that.
If-Statement
for (Method method : measureTimeMethods) {
MeasureTime measureTime = method.getAnnotation(MeasureTime.class);
if (measureTime != null) {
try {I think the
if (measureTime != null) { is not needed, as this is the pre-condition for adding a method to measureTimeMethods.Code Documentation
Your code is lacking any form of comments in the code. In my humble opinion, at least every public method should be commented. For developing it might be useful on private methods as well. For example to me it is not obvious (by reading the method names) what the difference between
runQuietly() and runNormally() is. Comment ratio depends on your personal taste a lot though and is very dependant on the methods' names.Other Comments
runQuietly() could probably be inlined. It is only used once in run().runNormally(): Maybe rename to to: runMeasurements() or similar?runMeasureTime(): Maybe rename to measureMethodRuntime() or similar?getParamValue(): Maybe rename to getFirstOrDefault() or similar?Someone with more expierence implementing anotations than me should probably answer your questions about the annotation implementation, so I'll not comment on that.
Code Snippets
for (Method method : measureTimeMethods) {
MeasureTime measureTime = method.getAnnotation(MeasureTime.class);
if (measureTime != null) {
try {Context
StackExchange Code Review Q#68550, answer score: 6
Revisions (0)
No revisions yet.