thread local allocation in benchmark

12 views
Skip to first unread message

Dan Burkert

unread,
Jul 25, 2013, 10:45:24 AM7/25/13
to cal...@googlegroups.com
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.

Gregory Kick

unread,
Jul 29, 2013, 1:40:05 PM7/29/13
to cal...@googlegroups.com
I think you have a few options:
1) I think that the simple way to address the problem would be to explicitly pass in the Charset.  (It's a best practice anyway: https://plus.google.com/u/0/118010414872916542489/posts/FGpFSai9dVo)
2) Call your benchmark method explicitly from a @BeforeExperiment method. Since they are guaranteed to run first, that should initialize the data.


HTH


--
--
guava-...@googlegroups.com
Project site: http://caliper.googlecode.com
This group: http://groups.google.com/group/caliper
 
This list is for general discussion.
To report an issue: http://code.google.com/p/caliper/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "caliper")
 
---
You received this message because you are subscribed to the Google Groups "caliper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caliper+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Greg Kick
Java Core Libraries Team
Reply all
Reply to author
Forward
0 new messages