per-invocation setup for mutable data

Travis Downs travis.downs at gmail.com
Tue Dec 13 20:57:50 UTC 2016


Let's say I want a benchmark that will test a sort() method on an array. I
want to populate this array with random values, which I can do in an @Setup
method. The issue is that if the sort mutates the underlying array, I need
to have it restored to random on each invocation.

I see a couple of approaches to this:

1) Using Level.Invocation on the @Setup method so that the array can be
randomized (more likely this is a simple arraycopy() of the existing array
into the destination) before every invocation - but there are a laundry
list of reasons in the javadoc why you don't want to use this.

2) Make a copy of the array to be sorted in the benchmark method and sort
the copy. Now the copy time will be included in the benchmark, which is
non-negligible. I could have another benchmark that *only* did the copy and
then subtract out the difference, I guess...

Any other good options? Of course, if the sort() method is not
data-dependent, the randomization may not matter, but most are...


More information about the jmh-dev mailing list