JMH Thread index?

Aleksey Shipilev aleksey.shipilev at oracle.com
Mon May 6 12:39:34 PDT 2013


On 05/06/2013 10:10 PM, Nitsan Wakart wrote:
> What I meant was programmatically... in my benchmark code I have an
> array which is the benchmark state with an element per thread, I'd like
> to know the number of threads so I can allocate for the number of
> threads. 

You can't possibly do this at this point. There's no feedback to the
benchmark how many threads are running, although it should be fixed with
future @Param works.

> Similarly, each thread needs to access it's own element, so I'd
> like to have a thread index which correlates to the thread stats
> reported in the output. 

The best way to showcase this would be having the asymmetric benchmark.
I remember doing the false-sharing benchmark like this:

@State(Scope.Group)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyFalseSharingBench {

    volatile int x1;
    volatile int x2;
    volatile int  x3,  x4,  x5,  x6,  x7,  x8,  x9, x10;
    volatile int x11, x12, x13, x14, x15, x16, x17, x18, x19;
    volatile int x20;

    @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
    @Group("sharing")
    public void thread1() {
       x1++;
    }

    @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
    @Group("sharing")
    public void thread2() {
       x2++;
    }

    @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
    @Group("sharing")
    public void thread3() {
       x20++;
    }
}

$ ...
Iteration   2 (5s in 3 threads): thread1{t=1} = 28.039, thread2{t=1} =
32.116, thread3{t=1} = 11.998 nsec/op

It gets really beautiful with @Contended.

-Aleksey.


More information about the jmh-dev mailing list