Home | History | Annotate | Download | only in grpc
      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 java.util.ArrayList;
     20 import java.util.Collections;
     21 import java.util.List;
     22 import java.util.Random;
     23 import java.util.concurrent.TimeUnit;
     24 import org.openjdk.jmh.annotations.Benchmark;
     25 import org.openjdk.jmh.annotations.BenchmarkMode;
     26 import org.openjdk.jmh.annotations.Mode;
     27 import org.openjdk.jmh.annotations.OutputTimeUnit;
     28 import org.openjdk.jmh.annotations.Param;
     29 import org.openjdk.jmh.annotations.Scope;
     30 import org.openjdk.jmh.annotations.Setup;
     31 import org.openjdk.jmh.annotations.State;
     32 
     33 /**
     34  * Call options benchmark.
     35  */
     36 @State(Scope.Benchmark)
     37 public class CallOptionsBenchmark {
     38 
     39   @Param({"1", "2", "4", "8"})
     40   public int customOptionsCount;
     41 
     42   private List<CallOptions.Key<String>> customOptions;
     43 
     44   private CallOptions allOpts;
     45   private List<CallOptions.Key<String>> shuffledCustomOptions;
     46 
     47   /**
     48    * Setup.
     49    */
     50   @Setup
     51   public void setUp() throws Exception {
     52     customOptions = new ArrayList<CallOptions.Key<String>>(customOptionsCount);
     53     for (int i = 0; i < customOptionsCount; i++) {
     54       customOptions.add(CallOptions.Key.createWithDefault("name " + i, "defaultvalue"));
     55     }
     56 
     57     allOpts = CallOptions.DEFAULT;
     58     for (int i = 0; i < customOptionsCount; i++) {
     59       allOpts = allOpts.withOption(customOptions.get(i), "value");
     60     }
     61 
     62     shuffledCustomOptions = new ArrayList<CallOptions.Key<String>>(customOptions);
     63     // Make the shuffling deterministic
     64     Collections.shuffle(shuffledCustomOptions, new Random(1));
     65   }
     66 
     67   /**
     68    * Adding custom call options without duplicate keys.
     69    */
     70   @Benchmark
     71   @BenchmarkMode(Mode.SampleTime)
     72   @OutputTimeUnit(TimeUnit.NANOSECONDS)
     73   public CallOptions withOption() {
     74     CallOptions opts = CallOptions.DEFAULT;
     75     for (int i = 0; i < customOptions.size(); i++) {
     76       opts = opts.withOption(customOptions.get(i), "value");
     77     }
     78     return opts;
     79   }
     80 
     81   /**
     82    * Adding custom call options, overwritting existing keys.
     83    */
     84   @Benchmark
     85   @BenchmarkMode(Mode.SampleTime)
     86   @OutputTimeUnit(TimeUnit.NANOSECONDS)
     87   public CallOptions withOptionDuplicates() {
     88     CallOptions opts = allOpts;
     89     for (int i = 1; i < shuffledCustomOptions.size(); i++) {
     90       opts = opts.withOption(shuffledCustomOptions.get(i), "value2");
     91     }
     92     return opts;
     93   }
     94 }
     95