• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.concurrent.TimeUnit;
20 import org.openjdk.jmh.annotations.Benchmark;
21 import org.openjdk.jmh.annotations.BenchmarkMode;
22 import org.openjdk.jmh.annotations.Mode;
23 import org.openjdk.jmh.annotations.OutputTimeUnit;
24 import org.openjdk.jmh.annotations.Param;
25 import org.openjdk.jmh.annotations.Scope;
26 import org.openjdk.jmh.annotations.Setup;
27 import org.openjdk.jmh.annotations.State;
28 import org.openjdk.jmh.infra.Blackhole;
29 
30 /** Write benchmark. */
31 public class WriteBenchmark {
32   @State(Scope.Thread)
33   public static class ContextState {
34     @Param({"0", "10", "25", "100"})
35     public int preexistingKeys;
36 
37     Context.Key<Object> key1 = Context.key("key1");
38     Context.Key<Object> key2 = Context.key("key2");
39     Context.Key<Object> key3 = Context.key("key3");
40     Context.Key<Object> key4 = Context.key("key4");
41     Context context;
42     Object val = new Object();
43 
44     @Setup
setup()45     public void setup() {
46       context = Context.ROOT;
47       for (int i = 0; i < preexistingKeys; i++) {
48         context = context.withValue(Context.key("preexisting_key" + i), val);
49       }
50     }
51   }
52 
53   /** Perform the write operation. */
54   @Benchmark
55   @BenchmarkMode(Mode.SampleTime)
56   @OutputTimeUnit(TimeUnit.NANOSECONDS)
doWrite(ContextState state, Blackhole bh)57   public Context doWrite(ContextState state, Blackhole bh) {
58     return state.context.withValues(
59         state.key1, state.val,
60         state.key2, state.val,
61         state.key3, state.val,
62         state.key4, state.val);
63   }
64 }
65