diff options
author | Bogdan Drutu <bdrutu@google.com> | 2017-05-15 14:54:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-15 14:54:01 -0700 |
commit | 8ec67cf9c391101bdf3c5e1de49794e2ccde9d68 (patch) | |
tree | 79a5fffd6ef1586a95e9a7c33c02111272109404 /benchmarks/src | |
parent | 9fdbe23fd960ea42c3674dd30e0522104a455f9f (diff) | |
download | opencensus-java-8ec67cf9c391101bdf3c5e1de49794e2ccde9d68.tar.gz |
Add benchmarks for start/end span and record trace events. (#297)
Diffstat (limited to 'benchmarks/src')
3 files changed, 293 insertions, 0 deletions
diff --git a/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java b/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java new file mode 100644 index 00000000..ad9a66b5 --- /dev/null +++ b/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017, Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.instrumentation.trace; + +import java.util.HashMap; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; + +/** Benchmarks for {@link SpanImpl} to record trace events. */ +@State(Scope.Benchmark) +public class RecordTraceEventsNonSampledSpanBenchmark { + private static final Tracer tracer = Tracing.getTracer(); + private static final String SPAN_NAME = "MySpanName"; + private static final String ANNOTATION_DESCRIPTION = "MyAnnotation"; + private static final String ATTRIBUTE_KEY = "MyAttributeKey"; + private static final String ATTRIBUTE_VALUE = "MyAttributeValue"; + private Span linkedSpan = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.neverSample()).startSpan(); + private Span span = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.neverSample()).startSpan(); + + /** TearDown method. */ + @TearDown + public void doTearDown() { + span.end(); + linkedSpan.end(); + } + + /** This benchmark attempts to measure performance of adding an attribute to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addAttributes() { + HashMap<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); + attributes.put(ATTRIBUTE_KEY, AttributeValue.stringAttributeValue(ATTRIBUTE_VALUE)); + span.addAttributes(attributes); + return span; + } + + /** This benchmark attempts to measure performance of adding an annotation to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addAnnotation() { + span.addAnnotation(ANNOTATION_DESCRIPTION); + return span; + } + + /** This benchmark attempts to measure performance of adding a network event to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addNetworkEvent() { + span.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setMessageSize(3).build()); + return span; + } + + /** This benchmark attempts to measure performance of adding a link to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addLink() { + span.addLink(Link.fromSpanContext(linkedSpan.getContext(), Link.Type.PARENT)); + return span; + } +} diff --git a/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsSampledSpanBenchmark.java b/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsSampledSpanBenchmark.java new file mode 100644 index 00000000..e20af6ea --- /dev/null +++ b/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsSampledSpanBenchmark.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017, Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.instrumentation.trace; + +import java.util.HashMap; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; + +/** Benchmarks for {@link SpanImpl} to record trace events. */ +@State(Scope.Benchmark) +public class RecordTraceEventsSampledSpanBenchmark { + private static final Tracer tracer = Tracing.getTracer(); + private static final String SPAN_NAME = "MySpanName"; + private static final String ANNOTATION_DESCRIPTION = "MyAnnotation"; + private static final String ATTRIBUTE_KEY = "MyAttributeKey"; + private static final String ATTRIBUTE_VALUE = "MyAttributeValue"; + private Span linkedSpan = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.alwaysSample()).startSpan(); + private Span span = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.alwaysSample()).startSpan(); + + /** TearDown method. */ + @TearDown + public void doTearDown() { + span.end(); + linkedSpan.end(); + } + + /** This benchmark attempts to measure performance of adding an attribute to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addAttributes() { + HashMap<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); + attributes.put(ATTRIBUTE_KEY, AttributeValue.stringAttributeValue(ATTRIBUTE_VALUE)); + span.addAttributes(attributes); + return span; + } + + /** This benchmark attempts to measure performance of adding an annotation to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addAnnotation() { + span.addAnnotation(ANNOTATION_DESCRIPTION); + return span; + } + + /** This benchmark attempts to measure performance of adding a network event to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addNetworkEvent() { + span.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setMessageSize(3).build()); + return span; + } + + /** This benchmark attempts to measure performance of adding a link to the span. */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span addLink() { + span.addLink(Link.fromSpanContext(linkedSpan.getContext(), Link.Type.PARENT)); + return span; + } +} diff --git a/benchmarks/src/jmh/java/com/google/instrumentation/trace/StartEndSpanBenchmark.java b/benchmarks/src/jmh/java/com/google/instrumentation/trace/StartEndSpanBenchmark.java new file mode 100644 index 00000000..ee15ad01 --- /dev/null +++ b/benchmarks/src/jmh/java/com/google/instrumentation/trace/StartEndSpanBenchmark.java @@ -0,0 +1,127 @@ +/* + * Copyright 2017, Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.instrumentation.trace; + +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; + +/** Benchmarks for {@link SpanFactoryImpl} and {@link SpanImpl}. */ +@State(Scope.Benchmark) +public class StartEndSpanBenchmark { + private static final Tracer tracer = Tracing.getTracer(); + private static final String SPAN_NAME = "MySpanName"; + private Span rootSpan = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.neverSample()).startSpan(); + + @TearDown + public void doTearDown() { + rootSpan.end(); + } + + /** + * This benchmark attempts to measure performance of start/end for a non-sampled root {@code + * Span}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndNonSampledRootSpan() { + Span span = + tracer.spanBuilder(SPAN_NAME).becomeRoot().setSampler(Samplers.neverSample()).startSpan(); + span.end(); + return span; + } + + /** + * This benchmark attempts to measure performance of start/end for a root {@code Span} with record + * events option. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndRecordEventsRootSpan() { + Span span = + tracer + .spanBuilder(SPAN_NAME) + .becomeRoot() + .setSampler(Samplers.neverSample()) + .setRecordEvents(true) + .startSpan(); + span.end(); + return span; + } + + /** + * This benchmark attempts to measure performance of start/end for a sampled root {@code Span}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndSampledRootSpan() { + Span span = tracer.spanBuilder(SPAN_NAME).setSampler(Samplers.alwaysSample()).startSpan(); + span.end(); + return span; + } + + /** + * This benchmark attempts to measure performance of start/end for a non-sampled child {@code + * Span}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndNonSampledChildSpan() { + Span span = + tracer.spanBuilder(rootSpan, SPAN_NAME).setSampler(Samplers.neverSample()).startSpan(); + span.end(); + return span; + } + + /** + * This benchmark attempts to measure performance of start/end for a child {@code Span} with + * record events option. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndRecordEventsChildSpan() { + Span span = + tracer + .spanBuilder(rootSpan, SPAN_NAME) + .setSampler(Samplers.neverSample()) + .setRecordEvents(true) + .startSpan(); + span.end(); + return span; + } + + /** + * This benchmark attempts to measure performance of start/end for a sampled child {@code Span}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public Span startEndSampledChildSpan() { + Span span = + tracer.spanBuilder(rootSpan, SPAN_NAME).setSampler(Samplers.alwaysSample()).startSpan(); + span.end(); + return span; + } +} |