1 /* 2 * Copyright 2017 The gRPC 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.grpc; 18 19 20 import java.util.concurrent.TimeUnit; 21 import org.openjdk.jmh.annotations.Benchmark; 22 import org.openjdk.jmh.annotations.BenchmarkMode; 23 import org.openjdk.jmh.annotations.Mode; 24 import org.openjdk.jmh.annotations.OutputTimeUnit; 25 import org.openjdk.jmh.annotations.Param; 26 import org.openjdk.jmh.annotations.Scope; 27 import org.openjdk.jmh.annotations.Setup; 28 import org.openjdk.jmh.annotations.State; 29 30 /** 31 * Javadoc. 32 */ 33 @State(Scope.Benchmark) 34 public class AttributesBenchmark { 35 36 public Attributes base = Attributes.EMPTY; 37 38 public Attributes.Key<Object>[] keys; 39 public Attributes withValue = base; 40 41 /** 42 * Javadoc. 43 */ 44 @Setup 45 @SuppressWarnings({"unchecked", "rawtypes"}) 46 public void setUp() { 47 keys = new Attributes.Key[iterations]; 48 for (int i = 0; i < iterations; i++) { 49 keys[i] = Attributes.Key.create("any"); 50 withValue = withValue.toBuilder().set(keys[i], "yes").build(); 51 } 52 } 53 54 @Param({"1", "2", "10"}) 55 public int iterations; 56 57 /** 58 * Javadoc. 59 */ 60 @Benchmark 61 @BenchmarkMode(Mode.SampleTime) 62 @OutputTimeUnit(TimeUnit.NANOSECONDS) 63 public Attributes chain() { 64 Attributes attr = base; 65 for (int i = 0; i < iterations; i++) { 66 attr = attr.toBuilder().set(keys[i], new Object()).build(); 67 } 68 return attr; 69 } 70 71 /** 72 * Javadoc. 73 */ 74 @Benchmark 75 @BenchmarkMode(Mode.SampleTime) 76 @OutputTimeUnit(TimeUnit.NANOSECONDS) 77 public Object lookup() { 78 return withValue.get(keys[0]); 79 } 80 } 81