-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AsyncReporter/SpanHandler: make queuedMaxBytes=0 disable pre-flight s…
…ize checks Signed-off-by: Andriy Redko <drreta@gmail.com>
- Loading branch information
Showing
6 changed files
with
406 additions
and
9 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
benchmarks/src/main/java/zipkin2/reporter/internal/BoundedQueueBenchmarks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package zipkin2.reporter.internal; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.openjdk.jmh.annotations.AuxCounters; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Group; | ||
import org.openjdk.jmh.annotations.GroupThreads; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
@Measurement(iterations = 5, time = 1) | ||
@Warmup(iterations = 10, time = 1) | ||
@Fork(3) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Group) | ||
public class BoundedQueueBenchmarks { | ||
static final byte ONE = 1; | ||
|
||
@Param( {"0", "10000"}) | ||
public int maxBytes; | ||
|
||
@AuxCounters | ||
@State(Scope.Thread) | ||
public static class OfferCounters { | ||
public int offersFailed; | ||
public int offersMade; | ||
|
||
@Setup(Level.Iteration) | ||
public void clean() { | ||
offersFailed = offersMade = 0; | ||
} | ||
} | ||
|
||
@AuxCounters | ||
@State(Scope.Thread) | ||
public static class DrainCounters { | ||
public int drained; | ||
|
||
@Setup(Level.Iteration) | ||
public void clean() { | ||
drained = 0; | ||
} | ||
} | ||
|
||
private static ThreadLocal<Object> marker = new ThreadLocal<>(); | ||
|
||
@State(Scope.Thread) | ||
public static class ConsumerMarker { | ||
public ConsumerMarker() { | ||
marker.set(this); | ||
} | ||
} | ||
|
||
BoundedQueue<Byte> q; | ||
|
||
@Setup | ||
public void setup() { | ||
q = BoundedQueue.create(10000, maxBytes); | ||
} | ||
|
||
@Benchmark @Group("no_contention") @GroupThreads(1) | ||
public void no_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("no_contention") @GroupThreads(1) | ||
public void no_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@Benchmark @Group("mild_contention") @GroupThreads(2) | ||
public void mild_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("mild_contention") @GroupThreads(1) | ||
public void mild_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@Benchmark @Group("high_contention") @GroupThreads(8) | ||
public void high_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("high_contention") @GroupThreads(1) | ||
public void high_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@TearDown(Level.Iteration) | ||
public void emptyQ() { | ||
// If this thread didn't drain, return | ||
if (marker.get() == null) return; | ||
q.clear(); | ||
} | ||
|
||
// Convenience main entry-point | ||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(".*" + BoundedQueueBenchmarks.class.getSimpleName() + ".*") | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/main/java/zipkin2/reporter/internal/BoundedQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package zipkin2.reporter.internal; | ||
|
||
/** | ||
* Multi-producer, multi-consumer queue that could be bounded by count or/and size. | ||
*/ | ||
abstract class BoundedQueue<S> implements SpanWithSizeConsumer<S> { | ||
static <S> BoundedQueue<S> create(int maxSize, int maxBytes) { | ||
if (maxBytes > 0) { | ||
return new ByteBoundedQueue<S>(maxSize, maxBytes); | ||
} else { | ||
return new SizeBoundedQueue<S>(maxSize); | ||
} | ||
} | ||
|
||
/** | ||
* Max element's count of this bounded queue | ||
*/ | ||
abstract int maxSize(); | ||
|
||
/** | ||
* Max element'size of this bounded queue | ||
*/ | ||
abstract int maxBytes(); | ||
|
||
/** | ||
* Clear this bounded queue | ||
*/ | ||
abstract int clear(); | ||
|
||
/** | ||
* Element's count of this bounded queue | ||
*/ | ||
abstract int count(); | ||
|
||
/** | ||
* Element's size of this bounded queue | ||
*/ | ||
abstract int sizeInBytes(); | ||
|
||
/** | ||
* Drains this bounded queue. Blocks for up to nanosTimeout for spans to appear. | ||
* Then, consume as many as possible. | ||
*/ | ||
abstract int drainTo(SpanWithSizeConsumer<S> bundler, long remainingNanos); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.