-
Notifications
You must be signed in to change notification settings - Fork 936
Rework and publish metric benchmarks #8000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jack-berg
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
jack-berg:metric-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−295
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
246 changes: 246 additions & 0 deletions
246
sdk/all/src/jmh/java/io/opentelemetry/sdk/MetricRecordBenchmark.java
This file contains hidden or 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,246 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk; | ||
|
|
||
| import static io.opentelemetry.sdk.metrics.InstrumentType.COUNTER; | ||
| import static io.opentelemetry.sdk.metrics.InstrumentType.GAUGE; | ||
| import static io.opentelemetry.sdk.metrics.InstrumentType.HISTOGRAM; | ||
| import static io.opentelemetry.sdk.metrics.InstrumentType.UP_DOWN_COUNTER; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.sdk.common.export.MemoryMode; | ||
| import io.opentelemetry.sdk.metrics.Aggregation; | ||
| import io.opentelemetry.sdk.metrics.ExemplarFilter; | ||
| import io.opentelemetry.sdk.metrics.InstrumentType; | ||
| import io.opentelemetry.sdk.metrics.InstrumentValueType; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
| import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector; | ||
| import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Group; | ||
| import org.openjdk.jmh.annotations.GroupThreads; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| 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; | ||
|
|
||
| /** | ||
| * Notes on interpreting the data: | ||
| * | ||
| * <p>The benchmark has two dimensions which partially overlap: cardinality and thread count. | ||
| * Cardinality dictates how many unique attribute sets (i.e. series) are recorded to, and thread | ||
| * count dictates how many threads are simultaneously recording to those series. In all cases, the | ||
| * record path needs to look up an aggregation handle for the series corresponding to the | ||
| * measurement's {@link Attributes} in a {@link java.util.concurrent.ConcurrentHashMap}. That will | ||
| * be the case until otel adds support for <a | ||
| * href="https://github.com/open-telemetry/opentelemetry-specification/issues/4126">bound | ||
| * instruments</a>. The cardinality dictates the size of this map, which has some impact on | ||
| * performance. However, by far the dominant bottleneck is contention. That is, the number of | ||
| * threads simultaneously trying to record to the same series. Increasing the threads increases | ||
| * contention. Increasing cardinality decreases contention, as the threads are now spreading their | ||
| * record activities over more distinct series. The highest contention scenario is cardinality=1, | ||
| * threads=4. Any scenario with threads=1 has zero contention. | ||
| * | ||
| * <p>It's useful to characterize the performance of the metrics system under contention, as some | ||
| * high-performance applications may have many threads trying to record to the same series. It's | ||
| * also useful to characterize the performance of the metrics system under low contention, as some | ||
| * high-performance applications may not frequently be trying to concurrently record to the same | ||
| * series yet still care about the overhead of each record operation. | ||
| * | ||
| * <p>{@link AggregationTemporality} can impact performance because additional concurrency controls | ||
| * are needed to ensure there are no duplicate, partial, or lost writes while resetting the set of | ||
| * timeseries each collection. | ||
| */ | ||
| public class MetricRecordBenchmark { | ||
|
|
||
| private static final int recordCount = 10 * 1024; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class ThreadState { | ||
|
|
||
| @Param InstrumentTypeAndAggregation instrumentTypeAndAggregation; | ||
|
|
||
| @Param AggregationTemporality aggregationTemporality; | ||
|
|
||
| @Param({"1", "100"}) | ||
| int cardinality; | ||
|
|
||
| // The following parameters are excluded from the benchmark to reduce combinatorial explosion | ||
| // but can optionally be enabled for adhoc evaluation. | ||
|
|
||
| // InstrumentValueType doesn't materially impact performance. Uncomment to evaluate. | ||
| // @Param | ||
| // InstrumentValueType instrumentValueType; | ||
| InstrumentValueType instrumentValueType = InstrumentValueType.LONG; | ||
|
|
||
| // MemoryMode almost exclusively impacts collect from a performance standpoint. Uncomment to | ||
| // evaluate. | ||
| // @Param | ||
| // MemoryMode memoryMode; | ||
| MemoryMode memoryMode = MemoryMode.REUSABLE_DATA; | ||
|
|
||
| // Exemplars can impact performance, but we skip evaluation to limit test cases. Uncomment to | ||
| // evaluate. | ||
| // @Param({"true", "false"}) | ||
| // boolean exemplars; | ||
| boolean exemplars = false; | ||
|
|
||
| OpenTelemetrySdk openTelemetry; | ||
| Instrument instrument; | ||
| List<Long> measurements; | ||
| List<Attributes> attributesList; | ||
| Span span; | ||
| io.opentelemetry.context.Scope contextScope; | ||
|
|
||
| @Setup | ||
| @SuppressWarnings("MustBeClosedChecker") | ||
| public void setup() { | ||
| InstrumentType instrumentType = instrumentTypeAndAggregation.instrumentType; | ||
| Aggregation aggregation = instrumentTypeAndAggregation.aggregation; | ||
|
|
||
| openTelemetry = | ||
| OpenTelemetrySdk.builder() | ||
| .setTracerProvider(SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build()) | ||
| .setMeterProvider( | ||
| SdkMeterProvider.builder() | ||
| .registerMetricReader( | ||
| InMemoryMetricReader.builder() | ||
| .setAggregationTemporalitySelector(unused -> aggregationTemporality) | ||
| .setDefaultAggregationSelector( | ||
| DefaultAggregationSelector.getDefault() | ||
| .with(instrumentType, aggregation)) | ||
| .setMemoryMode(memoryMode) | ||
| .build()) | ||
| .setExemplarFilter( | ||
| exemplars ? ExemplarFilter.traceBased() : ExemplarFilter.alwaysOff()) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| Meter meter = openTelemetry.getMeter("benchmark"); | ||
| instrument = getInstrument(meter, instrumentType, instrumentValueType); | ||
| Tracer tracer = openTelemetry.getTracer("benchmark"); | ||
| span = tracer.spanBuilder("benchmark").startSpan(); | ||
| // We suppress warnings on closing here, as we rely on tests to make sure context is closed. | ||
| contextScope = span.makeCurrent(); | ||
|
|
||
| Random random = new Random(); | ||
| attributesList = new ArrayList<>(cardinality); | ||
| AttributeKey<String> key = AttributeKey.stringKey("key"); | ||
| String last = "aaaaaaaaaaaaaaaaaaaaaaaaaa"; | ||
| for (int i = 0; i < cardinality; i++) { | ||
| char[] chars = last.toCharArray(); | ||
| chars[random.nextInt(last.length())] = (char) (random.nextInt(26) + 'a'); | ||
| last = new String(chars); | ||
| attributesList.add(Attributes.of(key, last)); | ||
| } | ||
| Collections.shuffle(attributesList); | ||
|
|
||
| measurements = new ArrayList<>(recordCount); | ||
| for (int i = 0; i < recordCount; i++) { | ||
| measurements.add((long) random.nextInt(2000)); | ||
| } | ||
| Collections.shuffle(measurements); | ||
| } | ||
|
|
||
| @TearDown | ||
| public void tearDown() { | ||
| contextScope.close(); | ||
| span.end(); | ||
| openTelemetry.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Group("threads1") | ||
| @GroupThreads(1) | ||
| @Fork(1) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| public void record_1Thread(ThreadState threadState) { | ||
| record(threadState); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Group("threads4") | ||
| @GroupThreads(4) | ||
| @Fork(1) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| public void record_4Threads(ThreadState threadState) { | ||
| record(threadState); | ||
| } | ||
|
|
||
| private static void record(ThreadState threadState) { | ||
| for (int i = 0; i < recordCount; i++) { | ||
| Attributes attributes = threadState.attributesList.get(i % threadState.attributesList.size()); | ||
| long value = threadState.measurements.get(i % threadState.measurements.size()); | ||
| threadState.instrument.record(value, attributes); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("ImmutableEnumChecker") | ||
| public enum InstrumentTypeAndAggregation { | ||
| COUNTER_SUM(COUNTER, Aggregation.sum()), | ||
| UP_DOWN_COUNTER_SUM(UP_DOWN_COUNTER, Aggregation.sum()), | ||
| GAUGE_LAST_VALUE(GAUGE, Aggregation.lastValue()), | ||
| HISTOGRAM_EXPLICIT(HISTOGRAM, Aggregation.explicitBucketHistogram()), | ||
| HISTOGRAM_BASE2_EXPONENTIAL(HISTOGRAM, Aggregation.base2ExponentialBucketHistogram()); | ||
|
|
||
| InstrumentTypeAndAggregation(InstrumentType instrumentType, Aggregation aggregation) { | ||
| this.instrumentType = instrumentType; | ||
| this.aggregation = aggregation; | ||
| } | ||
|
|
||
| private final InstrumentType instrumentType; | ||
| private final Aggregation aggregation; | ||
| } | ||
|
|
||
| private interface Instrument { | ||
| void record(long value, Attributes attributes); | ||
| } | ||
|
|
||
| private static Instrument getInstrument( | ||
| Meter meter, InstrumentType instrumentType, InstrumentValueType instrumentValueType) { | ||
| String name = "instrument"; | ||
| switch (instrumentType) { | ||
| case COUNTER: | ||
| return instrumentValueType == InstrumentValueType.DOUBLE | ||
| ? meter.counterBuilder(name).ofDoubles().build()::add | ||
| : meter.counterBuilder(name).build()::add; | ||
| case UP_DOWN_COUNTER: | ||
| return instrumentValueType == InstrumentValueType.DOUBLE | ||
| ? meter.upDownCounterBuilder(name).ofDoubles().build()::add | ||
| : meter.upDownCounterBuilder(name).build()::add; | ||
| case HISTOGRAM: | ||
| return instrumentValueType == InstrumentValueType.DOUBLE | ||
| ? meter.histogramBuilder(name).build()::record | ||
| : meter.histogramBuilder(name).ofLongs().build()::record; | ||
| case GAUGE: | ||
| return instrumentValueType == InstrumentValueType.DOUBLE | ||
| ? meter.gaugeBuilder(name).build()::set | ||
| : meter.gaugeBuilder(name).ofLongs().build()::set; | ||
| case OBSERVABLE_COUNTER: | ||
| case OBSERVABLE_UP_DOWN_COUNTER: | ||
| case OBSERVABLE_GAUGE: | ||
| } | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } |
112 changes: 0 additions & 112 deletions
112
sdk/metrics/src/jmh/java/io/opentelemetry/sdk/metrics/MetricsBenchmarks.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tylerbenson this
benchmark-actiononly allows you to have a single output file path. This means we need to all the published benchmarks to be in a single module, such that we can run with them a singlejava -jar *-jmh.jar ...command. I think theopentelemetry-sdkartifact is a good spot for this.This turns out to be a useful constraint as I think it will be nice to have all the public benchmarks colocated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LTGM!