1 /* 2 * Copyright 2017, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.benchmarks.trace; 18 19 import static com.google.common.base.Preconditions.checkState; 20 21 import io.opencensus.trace.AttributeValue; 22 import io.opencensus.trace.BlankSpan; 23 import io.opencensus.trace.Link; 24 import io.opencensus.trace.MessageEvent.Type; 25 import io.opencensus.trace.Span; 26 import io.opencensus.trace.Tracer; 27 import io.opencensus.trace.samplers.Samplers; 28 import java.util.concurrent.TimeUnit; 29 import org.openjdk.jmh.annotations.Benchmark; 30 import org.openjdk.jmh.annotations.BenchmarkMode; 31 import org.openjdk.jmh.annotations.Mode; 32 import org.openjdk.jmh.annotations.OutputTimeUnit; 33 import org.openjdk.jmh.annotations.Param; 34 import org.openjdk.jmh.annotations.Scope; 35 import org.openjdk.jmh.annotations.Setup; 36 import org.openjdk.jmh.annotations.State; 37 import org.openjdk.jmh.annotations.TearDown; 38 39 /** Benchmarks for {@link Span} to record trace events. */ 40 @State(Scope.Benchmark) 41 public class RecordTraceEventsBenchmark { 42 private static final String SPAN_NAME = "MySpanName"; 43 private static final String ANNOTATION_DESCRIPTION = "MyAnnotation"; 44 private static final String ATTRIBUTE_KEY = "MyAttributeKey"; 45 private static final String ATTRIBUTE_VALUE = "MyAttributeValue"; 46 47 @State(Scope.Benchmark) 48 public static class Data { 49 50 private Span linkedSpan = BlankSpan.INSTANCE; 51 private Span span = BlankSpan.INSTANCE; 52 53 @Param({"impl", "impl-lite"}) 54 String implementation; 55 56 @Param({"true", "false"}) 57 boolean sampled; 58 59 @Setup 60 public void setup() { 61 Tracer tracer = BenchmarksUtil.getTracer(implementation); 62 linkedSpan = 63 tracer 64 .spanBuilderWithExplicitParent(SPAN_NAME, null) 65 .setSampler(sampled ? Samplers.alwaysSample() : Samplers.neverSample()) 66 .startSpan(); 67 span = 68 tracer 69 .spanBuilderWithExplicitParent(SPAN_NAME, null) 70 .setSampler(sampled ? Samplers.alwaysSample() : Samplers.neverSample()) 71 .startSpan(); 72 } 73 74 @TearDown 75 public void doTearDown() { 76 checkState(linkedSpan != BlankSpan.INSTANCE, "Uninitialized linkedSpan"); 77 checkState(span != BlankSpan.INSTANCE, "Uninitialized span"); 78 linkedSpan.end(); 79 span.end(); 80 } 81 } 82 83 /** This benchmark attempts to measure performance of adding an attribute to the span. */ 84 @Benchmark 85 @BenchmarkMode(Mode.SampleTime) 86 @OutputTimeUnit(TimeUnit.NANOSECONDS) 87 public Span putAttribute(Data data) { 88 data.span.putAttribute(ATTRIBUTE_KEY, AttributeValue.stringAttributeValue(ATTRIBUTE_VALUE)); 89 return data.span; 90 } 91 92 /** This benchmark attempts to measure performance of adding an annotation to the span. */ 93 @Benchmark 94 @BenchmarkMode(Mode.SampleTime) 95 @OutputTimeUnit(TimeUnit.NANOSECONDS) 96 public Span addAnnotation(Data data) { 97 data.span.addAnnotation(ANNOTATION_DESCRIPTION); 98 return data.span; 99 } 100 101 /** This benchmark attempts to measure performance of adding a network event to the span. */ 102 @Benchmark 103 @BenchmarkMode(Mode.SampleTime) 104 @OutputTimeUnit(TimeUnit.NANOSECONDS) 105 public Span addMessageEvent(Data data) { 106 data.span.addMessageEvent( 107 io.opencensus.trace.MessageEvent.builder(Type.RECEIVED, 1) 108 .setUncompressedMessageSize(3) 109 .build()); 110 return data.span; 111 } 112 113 /** This benchmark attempts to measure performance of adding a link to the span. */ 114 @Benchmark 115 @BenchmarkMode(Mode.SampleTime) 116 @OutputTimeUnit(TimeUnit.NANOSECONDS) 117 public Span addLink(Data data) { 118 data.span.addLink( 119 Link.fromSpanContext(data.linkedSpan.getContext(), Link.Type.PARENT_LINKED_SPAN)); 120 return data.span; 121 } 122 } 123