About uBench

uBench is a tiny micro benchmark kit for Java. For serious benchmarking I recommend using something like JMH instead but for quick and dirty stuff this seems to do the job and doesn't need a ton of dependencies.

The kit consists of only three classes: the Runner class (which has almost all of the code), a Benchmark annotation (with no code), and a small CSVWriter utility class. There is also an Example class with three simple example test cases.

License

This small tool is released under a very permissive BSD 2-clause license.

2016-04-12 11:27:28 UTC

A Simple Example

The included example benchmark has three test cases that each do the same thing slightly differently. Please, read its javadoc or comments from the source on how to write your own benchmarks.

Source code

This is one of the three test cases in the Example class:

...
public class Example
{
...
    @Benchmark
    public String stringValueOf(int rounds) throws Exception {
        int result = 0;
        int[] data = this.data;
        while ( --rounds >= 0 ) {
            for (int i = data.length; --i >= 0;) {
                String s = String.valueOf(data[i]);
                result |= s.length();
            }
        }
        return ""+result;
    }
...
}
...

Benchmark output

The console output looks something like this (for all three test cases in the example):

% java -cp build person.vainohelminen.ubench.Runner --count 5 --csv example.csv -- example.Example
=== uBench - Java Micro Benchmark Kit
=== Copyright © 2016 Väinö Helminen
Writing CSV to example.csv...

=== [1 / 3] example.Example.emptyPlusI
Warmup...........................................11
Measuring....11
Rounds: 244707
Op time    [L]: 0.008 ms
Op time    [M]: 0.008 ms
Op time    [H]: 0.008 ms
Throughput [L]: 121.276 ops/ms
Throughput [M]: 121.541 ops/ms
Throughput [H]: 121.741 ops/ms

=== [2 / 3] example.Example.stringBuilderAppend
Warmup............................................11
Measuring....11
Rounds: 326276
Op time    [L]: 0.006 ms
Op time    [M]: 0.006 ms
Op time    [H]: 0.006 ms
Throughput [L]: 155.960 ops/ms
Throughput [M]: 156.601 ops/ms
Throughput [H]: 156.652 ops/ms

=== [3 / 3] example.Example.stringValueOf
Warmup...........................................11
Measuring....11
Rounds: 244707
Op time    [L]: 0.008 ms
Op time    [M]: 0.008 ms
Op time    [H]: 0.008 ms
Throughput [L]: 119.408 ops/ms
Throughput [M]: 121.992 ops/ms
Throughput [H]: 122.159 ops/ms

Try -help to see the command line arguments. The [L], [M], and [H] stand for lowest, median, and highest values. The optional CSV output includes all measurements.

The CSV file from this run looks like this (it looks much nicer in LibreOffice Calc):

Class,Method,Op time #1 (ms),Op time #2 (ms),Op time #3 (ms),Op time #4 (ms),Op time #5 (ms),Throughput #1 (ops/ms),Throughput #2 (ops/ms),Throughput #3 (ops/ms),Throughput #4 (ops/ms),Throughput #5 (ops/ms)
example.Example,emptyPlusI,0.008214163938914701,0.008222944333427323,0.008227694663413797,0.008231386094390435,0.008245682624526475,121.74093522318051,121.61094122148823,121.54072810294161,121.48622219063262,121.27558693873779
example.Example,stringBuilderAppend,0.006383564096041388,0.006384118525420196,0.006385659484608124,0.006386948525175004,0.0064119064289129436,156.652300338008,156.63869585412826,156.60089649477578,156.56929064926192,155.95985547929106
example.Example,stringValueOf,0.008186083111639634,0.008191103066115804,0.008197238836649544,0.008326042871679191,0.008374616324829285,122.15854473528607,122.08367931990837,121.99229764162024,120.10507457287697,119.40845541009126
2016-04-12 11:30:55 UTC

Notes

Java has a couple of interesting and useful flags that can be used when benchmarking:

2016-04-12 11:25:17 UTC

Documentatation

Check out the API in the javadoc.

Command Line Arguments

% java person.vainohelminen.ubench.Runner --help
`
=== uBench - Java Micro Benchmark Kit
=== Copyright © 2016 Väinö Helminen
Usage: java person.vainohelminen.ubench.Runner [-h|-help|--help] [-c|--count n]
[-o|--csv results.csv] [--] class1 [class2 [class3...]]
2016-04-13 09:59:05 UTC

Download

There are currently no snapshots or releases available for download but you can clone it from:

git clone https://git.gizmo.dy.fi/ubench.git

2017-10-01 19:53:13 UTC

Email

You can reach me via email at vaino (dot) helminen (at) gmail (dot) com.