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 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.TimeUnit;
22 import org.openjdk.jmh.annotations.Benchmark;
23 import org.openjdk.jmh.annotations.BenchmarkMode;
24 import org.openjdk.jmh.annotations.Mode;
25 import org.openjdk.jmh.annotations.OutputTimeUnit;
26 import org.openjdk.jmh.annotations.Scope;
27 import org.openjdk.jmh.annotations.Setup;
28 import org.openjdk.jmh.annotations.State;
29 import org.openjdk.jmh.infra.Blackhole;
30 
31 /** Read benchmark. */
32 public class ReadBenchmark {
33 
34   @State(Scope.Benchmark)
35   public static class ContextState {
36     List<Context.Key<Object>> keys = new ArrayList<Context.Key<Object>>();
37     List<Context> contexts = new ArrayList<>();
38 
39     @Setup
setup()40     public void setup() {
41       for (int i = 0; i < 8; i++) {
42         keys.add(Context.key("Key" + i));
43       }
44       contexts.add(Context.ROOT.withValue(keys.get(0), new Object()));
45       contexts.add(Context.ROOT.withValues(keys.get(0), new Object(), keys.get(1), new Object()));
46       contexts.add(
47           Context.ROOT.withValues(
48               keys.get(0), new Object(), keys.get(1), new Object(), keys.get(2), new Object()));
49       contexts.add(
50           Context.ROOT.withValues(
51               keys.get(0),
52               new Object(),
53               keys.get(1),
54               new Object(),
55               keys.get(2),
56               new Object(),
57               keys.get(3),
58               new Object()));
59       contexts.add(contexts.get(0).withValue(keys.get(1), new Object()));
60       contexts.add(
61           contexts.get(1).withValues(keys.get(2), new Object(), keys.get(3), new Object()));
62       contexts.add(
63           contexts
64               .get(2)
65               .withValues(
66                   keys.get(3), new Object(), keys.get(4), new Object(), keys.get(5), new Object()));
67       contexts.add(
68           contexts
69               .get(3)
70               .withValues(
71                   keys.get(4),
72                   new Object(),
73                   keys.get(5),
74                   new Object(),
75                   keys.get(6),
76                   new Object(),
77                   keys.get(7),
78                   new Object()));
79     }
80   }
81 
82   /** Perform the read operation. */
83   @Benchmark
84   @BenchmarkMode(Mode.SampleTime)
85   @OutputTimeUnit(TimeUnit.NANOSECONDS)
testContextLookup(ContextState state, Blackhole bh)86   public void testContextLookup(ContextState state, Blackhole bh) {
87     for (Context.Key<?> key : state.keys) {
88       for (Context ctx : state.contexts) {
89         bh.consume(key.get(ctx));
90       }
91     }
92   }
93 }
94