I'm having trouble benchmarking some code that calls
String.getBytes(). Behind the scenes this method caches some string encoder classes in a
ThreadLocal. When I try to benchmark, say something as simple as:
@Benchmark
public long bench(int reps) throws Exception {
long dummy = 0;
for (int i = 0; i < reps; i++) {
dummy |= "foobar".getBytes().hashCode();
}
return dummy;
}
the benchmark run fails with
java.lang.IllegalStateException: Your benchmark appears to have non-deterministic allocation behavior.
But even if I call String.getBytes() in a static field initializer in the benchmarking class (in order to pre-load the class caches), the benchmark still fails with the same exception. I believe it is because the benchmarks are being run on a different thread. Anyone know any way around this problem? Thanks.