• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 io.grpc.Context.Key;
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.GroupThreads;
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.State;
28 
29 /** StatusBenchmark. */
30 @State(Scope.Benchmark)
31 public class AttachDetachBenchmark {
32 
33   private final Key<Integer> key = Context.keyWithDefault("key", 9999);
34   private final Context cu = Context.current().withValue(key, 8888);
35 
36   /**
37    * Javadoc comment.
38    */
39   @Benchmark
40   @BenchmarkMode(Mode.SampleTime)
41   @OutputTimeUnit(TimeUnit.NANOSECONDS)
42   @GroupThreads(6)
attachDetach()43   public int attachDetach() {
44     Context old = cu.attach();
45     try {
46       return key.get();
47     } finally {
48       Context.current().detach(old);
49     }
50   }
51 }
52