Correct way too perform loops

Francesco Nigro nigro.fra at gmail.com
Mon Sep 4 09:25:09 UTC 2017


Hi guys!

I've written a benchmark to find out the throughput of some single threaded
Queues offer (with resizes and GC activity dependent by it) and I'm
struggling to find out the proper way to do it.
That's my attempt:

private Deque<Long> q;

public int chunkSize = 1024;

@Setup(Level.Iteration)
public void init() {
   this.q = new ArrayDeque<>(chunkSize);
}

@Benchmark
@Warmup(iterations = 5, batchSize = 100_000)
@Measurement(iterations = 5, batchSize = 100_000)
@OperationsPerInvocation(100_000)
public Object offer() {
   q.offer(VALUE);
   return q;
}

@TearDown(Level.Iteration)
public void clean() {
   q.clear();
}

My expectation on this test is that for each iteration a batchSize number
of offer calls will be performed against a fresh new instance of ArrayDeque
(the clean method is not needed).
And to be sure it works as expected I will configure the benchmark to do GC.
There is something I've missed?

With great regards,
Francesco


More information about the jmh-dev mailing list