|
1 | | -package datadog.trace.util.stacktrace.queue; |
| 1 | +package datadog.trace.util.queue; |
2 | 2 |
|
3 | | -import datadog.trace.util.queue.SpmcArrayQueueVarHandle; |
4 | 3 | import java.util.concurrent.CountDownLatch; |
5 | 4 | import java.util.concurrent.TimeUnit; |
6 | 5 | import org.openjdk.jmh.annotations.Benchmark; |
|
20 | 19 | import org.openjdk.jmh.infra.Blackhole; |
21 | 20 |
|
22 | 21 | /* |
23 | | -MPSCQueueBenchmark.queueTest 1024 thrpt 145.261 ops/us |
24 | | -MPSCQueueBenchmark.queueTest:consume 1024 thrpt 84.185 ops/us |
25 | | -MPSCQueueBenchmark.queueTest:produce 1024 thrpt 61.076 ops/us |
26 | | -MPSCQueueBenchmark.queueTest 65536 thrpt 187.609 ops/us |
27 | | -MPSCQueueBenchmark.queueTest:consume 65536 thrpt 117.097 ops/us |
28 | | -MPSCQueueBenchmark.queueTest:produce 65536 thrpt 70.512 ops/us |
29 | | - */ |
| 22 | +Benchmark (capacity) Mode Cnt Score Error Units |
| 23 | +MPSCBlockingConsumerQueueBenchmark.queueTest 1024 thrpt 121.534 ops/us |
| 24 | +MPSCBlockingConsumerQueueBenchmark.queueTest:async 1024 thrpt NaN --- |
| 25 | +MPSCBlockingConsumerQueueBenchmark.queueTest:consume 1024 thrpt 110.962 ops/us |
| 26 | +MPSCBlockingConsumerQueueBenchmark.queueTest:produce 1024 thrpt 10.572 ops/us |
| 27 | +MPSCBlockingConsumerQueueBenchmark.queueTest 65536 thrpt 126.856 ops/us |
| 28 | +MPSCBlockingConsumerQueueBenchmark.queueTest:async 65536 thrpt NaN --- |
| 29 | +MPSCBlockingConsumerQueueBenchmark.queueTest:consume 65536 thrpt 113.213 ops/us |
| 30 | +MPSCBlockingConsumerQueueBenchmark.queueTest:produce 65536 thrpt 13.644 ops/us |
| 31 | +*/ |
30 | 32 | @BenchmarkMode(Mode.Throughput) |
31 | 33 | @Warmup(iterations = 1, time = 30) |
32 | 34 | @Measurement(iterations = 1, time = 30) |
33 | 35 | @Fork(1) |
34 | 36 | @OutputTimeUnit(TimeUnit.MICROSECONDS) |
35 | 37 | @State(Scope.Benchmark) |
36 | | -public class SPMCQueueBenchmark { |
| 38 | +public class MPSCBlockingConsumerQueueBenchmark { |
37 | 39 | @State(Scope.Group) |
38 | 40 | public static class QueueState { |
39 | | - SpmcArrayQueueVarHandle<Integer> queue; |
40 | | - CountDownLatch producerReady; |
| 41 | + MpscBlockingConsumerArrayQueueVarHandle<Integer> queue; |
| 42 | + CountDownLatch consumerReady; |
41 | 43 |
|
42 | 44 | @Param({"1024", "65536"}) |
43 | 45 | int capacity; |
44 | 46 |
|
45 | 47 | @Setup(Level.Iteration) |
46 | 48 | public void setup() { |
47 | | - queue = new SpmcArrayQueueVarHandle<>(capacity); |
48 | | - producerReady = new CountDownLatch(1); |
| 49 | + queue = new MpscBlockingConsumerArrayQueueVarHandle<>(capacity); |
| 50 | + consumerReady = new CountDownLatch(1); |
49 | 51 | } |
50 | 52 | } |
51 | 53 |
|
52 | 54 | @Benchmark |
53 | 55 | @Group("queueTest") |
54 | 56 | @GroupThreads(4) |
55 | | - public void consume(QueueState state, Blackhole bh) { |
| 57 | + public void produce(QueueState state) { |
56 | 58 | try { |
57 | | - state.producerReady.await(); // wait until consumer is ready |
| 59 | + state.consumerReady.await(); // wait until consumer is ready |
58 | 60 | } catch (InterruptedException ignored) { |
59 | 61 | } |
60 | | - Integer v = state.queue.poll(); |
61 | | - if (v != null) { |
62 | | - bh.consume(v); |
| 62 | + |
| 63 | + // bounded attempt: try once, then yield if full |
| 64 | + boolean offered = state.queue.offer(0); |
| 65 | + if (!offered) { |
| 66 | + Thread.yield(); |
63 | 67 | } |
64 | 68 | } |
65 | 69 |
|
66 | 70 | @Benchmark |
67 | 71 | @Group("queueTest") |
68 | 72 | @GroupThreads(1) |
69 | | - public void produce(QueueState state) { |
70 | | - state.producerReady.countDown(); // signal consumers can start |
71 | | - // bounded attempt: try once, then yield if full |
72 | | - boolean offered = state.queue.offer(0); |
73 | | - if (!offered) { |
74 | | - Thread.yield(); |
| 73 | + public void consume(QueueState state, Blackhole bh) { |
| 74 | + state.consumerReady.countDown(); // signal producers can start |
| 75 | + Integer v = state.queue.poll(); |
| 76 | + if (v != null) { |
| 77 | + bh.consume(v); |
75 | 78 | } |
76 | 79 | } |
77 | 80 | } |
0 commit comments