1 /* 2 * Copyright 2019, 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 /* 18 * ./gradlew --no-daemon -PjmhIncludeSingleClass=BasicDataBenchmark clean :opencensus-benchmarks:jmh 19 */ 20 21 package io.opencensus.benchmarks.trace; 22 23 import io.opencensus.trace.Annotation; 24 import io.opencensus.trace.AttributeValue; 25 import java.util.HashMap; 26 import java.util.Map; 27 import java.util.concurrent.TimeUnit; 28 import org.openjdk.jmh.annotations.Benchmark; 29 import org.openjdk.jmh.annotations.BenchmarkMode; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.OutputTimeUnit; 32 import org.openjdk.jmh.annotations.Param; 33 import org.openjdk.jmh.annotations.Scope; 34 import org.openjdk.jmh.annotations.Setup; 35 import org.openjdk.jmh.annotations.State; 36 import org.openjdk.jmh.annotations.TearDown; 37 38 /** Benchmarks for basic data structures related to trace events. */ 39 @State(Scope.Benchmark) 40 public class BasicDataBenchmark { 41 private static final String ANNOTATION_DESCRIPTION = "MyAnnotation"; 42 private static final String ATTRIBUTE_KEY = "MyAttributeKey"; 43 private static final String ATTRIBUTE_VALUE_STRING = "MyAttributeValue"; 44 private static final long ATTRIBUTE_VALUE_LONG = 90215; 45 46 @State(Scope.Benchmark) 47 public static class Data { 48 private AttributeValue[] attributeValues; 49 private String[] attributeKeys; 50 Map<String, AttributeValue> attributeMap; 51 52 // @Param({"impl", "impl-lite"}) 53 @Param({"impl"}) 54 String implementation; 55 56 @Param({"0", "1", "4", "8", "16"}) 57 int size; 58 59 @Param({"string", "boolean", "long"}) 60 String attributeType; 61 62 @Setup setup()63 public void setup() { 64 attributeValues = getAttributeValues(size, attributeType); 65 attributeKeys = new String[size]; 66 attributeMap = new HashMap<>(size); 67 for (int i = 0; i < size; i++) { 68 attributeKeys[i] = ATTRIBUTE_KEY + "-" + i; 69 attributeMap.put(attributeKeys[i], attributeValues[i]); 70 } 71 } 72 73 @TearDown doTearDown()74 public void doTearDown() {} 75 } 76 77 /** Create attribute values. */ 78 @Benchmark 79 @BenchmarkMode(Mode.AverageTime) 80 @OutputTimeUnit(TimeUnit.NANOSECONDS) createAttributeValues(Data data)81 public AttributeValue[] createAttributeValues(Data data) { 82 return getAttributeValues(data.size, data.attributeType); 83 } 84 85 /** Create an AttributeMap. */ 86 @Benchmark 87 @BenchmarkMode(Mode.AverageTime) 88 @OutputTimeUnit(TimeUnit.NANOSECONDS) createAttributeMap(Data data)89 public Map<String, AttributeValue> createAttributeMap(Data data) { 90 Map<String, AttributeValue> attributeMap = new HashMap<>(data.size); 91 for (int i = 0; i < data.size; i++) { 92 attributeMap.put(data.attributeKeys[i], data.attributeValues[i]); 93 } 94 return attributeMap; 95 } 96 97 /** Create an Annotation. */ 98 @Benchmark 99 @BenchmarkMode(Mode.AverageTime) 100 @OutputTimeUnit(TimeUnit.NANOSECONDS) createAnnotation(Data data)101 public Annotation createAnnotation(Data data) { 102 return Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, data.attributeMap); 103 } 104 getAttributeValues(int size, String attributeType)105 private static AttributeValue[] getAttributeValues(int size, String attributeType) { 106 AttributeValue[] attributeValues = new AttributeValue[size]; 107 switch (attributeType) { 108 case "string": 109 for (int i = 0; i < size; i++) { 110 attributeValues[i] = AttributeValue.stringAttributeValue(ATTRIBUTE_VALUE_STRING + "-i"); 111 } 112 break; 113 case "boolean": 114 for (int i = 0; i < size; i++) { 115 attributeValues[i] = AttributeValue.booleanAttributeValue(i % 3 == 0); 116 } 117 break; 118 case "long": 119 for (int i = 0; i < size; i++) { 120 attributeValues[i] = AttributeValue.longAttributeValue(ATTRIBUTE_VALUE_LONG + i); 121 } 122 break; 123 default: 124 throw new IllegalArgumentException("Unknown attribute type: " + attributeType); 125 } 126 return attributeValues; 127 } 128 } 129