From 80ec0c120bb133b3d3747406be7e4c543b9e0446 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 6 Jun 2017 13:56:55 -0700 Subject: Move internal & common & trace to the new package io.opencensus (#339) --- .../BinaryPropagationHandlerImplBenchmark.java | 75 ------------ .../RecordTraceEventsNonSampledSpanBenchmark.java | 83 -------------- .../RecordTraceEventsSampledSpanBenchmark.java | 83 -------------- .../trace/StartEndSpanBenchmark.java | 127 --------------------- .../BinaryPropagationHandlerImplBenchmark.java | 75 ++++++++++++ .../RecordTraceEventsNonSampledSpanBenchmark.java | 83 ++++++++++++++ .../RecordTraceEventsSampledSpanBenchmark.java | 83 ++++++++++++++ .../io/opencensus/trace/StartEndSpanBenchmark.java | 127 +++++++++++++++++++++ 8 files changed, 368 insertions(+), 368 deletions(-) delete mode 100644 benchmarks/src/jmh/java/com/google/instrumentation/trace/BinaryPropagationHandlerImplBenchmark.java delete mode 100644 benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java delete mode 100644 benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsSampledSpanBenchmark.java delete mode 100644 benchmarks/src/jmh/java/com/google/instrumentation/trace/StartEndSpanBenchmark.java create mode 100644 benchmarks/src/jmh/java/io/opencensus/trace/BinaryPropagationHandlerImplBenchmark.java create mode 100644 benchmarks/src/jmh/java/io/opencensus/trace/RecordTraceEventsNonSampledSpanBenchmark.java create mode 100644 benchmarks/src/jmh/java/io/opencensus/trace/RecordTraceEventsSampledSpanBenchmark.java create mode 100644 benchmarks/src/jmh/java/io/opencensus/trace/StartEndSpanBenchmark.java (limited to 'benchmarks') diff --git a/benchmarks/src/jmh/java/com/google/instrumentation/trace/BinaryPropagationHandlerImplBenchmark.java b/benchmarks/src/jmh/java/com/google/instrumentation/trace/BinaryPropagationHandlerImplBenchmark.java deleted file mode 100644 index fc8d9846..00000000 --- a/benchmarks/src/jmh/java/com/google/instrumentation/trace/BinaryPropagationHandlerImplBenchmark.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.text.ParseException; -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; - -/** Benchmarks for {@link BinaryPropagationHandlerImpl}. */ -@State(Scope.Benchmark) -public class BinaryPropagationHandlerImplBenchmark { - private static final byte[] traceIdBytes = - new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'}; - private static final TraceId traceId = TraceId.fromBytes(traceIdBytes); - private static final byte[] spanIdBytes = new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 0}; - private static final SpanId spanId = SpanId.fromBytes(spanIdBytes); - private static final byte[] traceOptionsBytes = new byte[] {1}; - private static final TraceOptions traceOptions = TraceOptions.fromBytes(traceOptionsBytes); - private static final SpanContext spanContext = SpanContext.create(traceId, spanId, traceOptions); - private static final BinaryPropagationHandler binaryPropagationHandler = - new BinaryPropagationHandlerImpl(); - private static final byte[] spanContextBinary = - binaryPropagationHandler.toBinaryValue(spanContext); - - /** - * This benchmark attempts to measure performance of {@link - * BinaryPropagationHandlerImpl#toBinaryValue(SpanContext)}. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public byte[] toBinaryValueSpanContext() { - return binaryPropagationHandler.toBinaryValue(spanContext); - } - - /** - * This benchmark attempts to measure performance of {@link - * BinaryPropagationHandlerImpl#fromBinaryValue(byte[])}. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public SpanContext fromBinaryValueSpanContext() throws ParseException { - return binaryPropagationHandler.fromBinaryValue(spanContextBinary); - } - - /** - * This benchmark attempts to measure performance of {@link - * BinaryPropagationHandlerImpl#toBinaryValue(SpanContext)} then {@link - * BinaryPropagationHandlerImpl#fromBinaryValue(byte[])}. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public SpanContext toFromBinarySpanContext() throws ParseException { - return binaryPropagationHandler.fromBinaryValue( - binaryPropagationHandler.toBinaryValue(spanContext)); - } -} diff --git a/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java b/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java deleted file mode 100644 index ad9a66b5..00000000 --- a/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsNonSampledSpanBenchmark.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 attributes = new HashMap(); - 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 deleted file mode 100644 index e20af6ea..00000000 --- a/benchmarks/src/jmh/java/com/google/instrumentation/trace/RecordTraceEventsSampledSpanBenchmark.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 attributes = new HashMap(); - 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 deleted file mode 100644 index ee15ad01..00000000 --- a/benchmarks/src/jmh/java/com/google/instrumentation/trace/StartEndSpanBenchmark.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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; - } -} diff --git a/benchmarks/src/jmh/java/io/opencensus/trace/BinaryPropagationHandlerImplBenchmark.java b/benchmarks/src/jmh/java/io/opencensus/trace/BinaryPropagationHandlerImplBenchmark.java new file mode 100644 index 00000000..85302d25 --- /dev/null +++ b/benchmarks/src/jmh/java/io/opencensus/trace/BinaryPropagationHandlerImplBenchmark.java @@ -0,0 +1,75 @@ +/* + * 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 io.opencensus.trace; + +import java.text.ParseException; +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; + +/** Benchmarks for {@link BinaryPropagationHandlerImpl}. */ +@State(Scope.Benchmark) +public class BinaryPropagationHandlerImplBenchmark { + private static final byte[] traceIdBytes = + new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'}; + private static final TraceId traceId = TraceId.fromBytes(traceIdBytes); + private static final byte[] spanIdBytes = new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 0}; + private static final SpanId spanId = SpanId.fromBytes(spanIdBytes); + private static final byte[] traceOptionsBytes = new byte[] {1}; + private static final TraceOptions traceOptions = TraceOptions.fromBytes(traceOptionsBytes); + private static final SpanContext spanContext = SpanContext.create(traceId, spanId, traceOptions); + private static final BinaryPropagationHandler binaryPropagationHandler = + new BinaryPropagationHandlerImpl(); + private static final byte[] spanContextBinary = + binaryPropagationHandler.toBinaryValue(spanContext); + + /** + * This benchmark attempts to measure performance of {@link + * BinaryPropagationHandlerImpl#toBinaryValue(SpanContext)}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public byte[] toBinaryValueSpanContext() { + return binaryPropagationHandler.toBinaryValue(spanContext); + } + + /** + * This benchmark attempts to measure performance of {@link + * BinaryPropagationHandlerImpl#fromBinaryValue(byte[])}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public SpanContext fromBinaryValueSpanContext() throws ParseException { + return binaryPropagationHandler.fromBinaryValue(spanContextBinary); + } + + /** + * This benchmark attempts to measure performance of {@link + * BinaryPropagationHandlerImpl#toBinaryValue(SpanContext)} then {@link + * BinaryPropagationHandlerImpl#fromBinaryValue(byte[])}. + */ + @Benchmark + @BenchmarkMode(Mode.SampleTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public SpanContext toFromBinarySpanContext() throws ParseException { + return binaryPropagationHandler.fromBinaryValue( + binaryPropagationHandler.toBinaryValue(spanContext)); + } +} diff --git a/benchmarks/src/jmh/java/io/opencensus/trace/RecordTraceEventsNonSampledSpanBenchmark.java b/benchmarks/src/jmh/java/io/opencensus/trace/RecordTraceEventsNonSampledSpanBenchmark.java new file mode 100644 index 00000000..0c3daa87 --- /dev/null +++ b/benchmarks/src/jmh/java/io/opencensus/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 io.opencensus.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 attributes = new HashMap(); + 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/io/opencensus/trace/RecordTraceEventsSampledSpanBenchmark.java b/benchmarks/src/jmh/java/io/opencensus/trace/RecordTraceEventsSampledSpanBenchmark.java new file mode 100644 index 00000000..78a587f4 --- /dev/null +++ b/benchmarks/src/jmh/java/io/opencensus/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 io.opencensus.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 attributes = new HashMap(); + 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/io/opencensus/trace/StartEndSpanBenchmark.java b/benchmarks/src/jmh/java/io/opencensus/trace/StartEndSpanBenchmark.java new file mode 100644 index 00000000..9a51ffbe --- /dev/null +++ b/benchmarks/src/jmh/java/io/opencensus/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 io.opencensus.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; + } +} -- cgit v1.2.3