• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* Benchmark channel */
20 
21 #include <benchmark/benchmark.h>
22 #include <grpc/grpc.h>
23 #include "test/core/util/test_config.h"
24 #include "test/cpp/microbenchmarks/helpers.h"
25 #include "test/cpp/util/test_config.h"
26 
27 class ChannelDestroyerFixture {
28  public:
ChannelDestroyerFixture()29   ChannelDestroyerFixture() {}
~ChannelDestroyerFixture()30   virtual ~ChannelDestroyerFixture() {
31     if (channel_) {
32       grpc_channel_destroy(channel_);
33     }
34   }
35   virtual void Init() = 0;
36 
37  protected:
38   grpc_channel* channel_ = nullptr;
39 };
40 
41 class InsecureChannelFixture : public ChannelDestroyerFixture {
42  public:
InsecureChannelFixture()43   InsecureChannelFixture() {}
Init()44   void Init() override {
45     channel_ = grpc_insecure_channel_create("localhost:1234", nullptr, nullptr);
46   }
47 };
48 
49 class LameChannelFixture : public ChannelDestroyerFixture {
50  public:
LameChannelFixture()51   LameChannelFixture() {}
Init()52   void Init() override {
53     channel_ = grpc_lame_client_channel_create(
54         "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
55   }
56 };
57 
58 template <class Fixture>
BM_InsecureChannelCreateDestroy(benchmark::State & state)59 static void BM_InsecureChannelCreateDestroy(benchmark::State& state) {
60   // In order to test if channel creation time is affected by the number of
61   // already existing channels, we create some initial channels here.
62   Fixture initial_channels[512];
63   for (int i = 0; i < state.range(0); i++) {
64     initial_channels[i].Init();
65   }
66   for (auto _ : state) {
67     Fixture channel;
68     channel.Init();
69   }
70 }
71 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, InsecureChannelFixture)
72     ->Range(0, 512);
73 ;
74 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, LameChannelFixture)
75     ->Range(0, 512);
76 ;
77 
78 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
79 // and others do not. This allows us to support both modes.
80 namespace benchmark {
RunTheBenchmarksNamespaced()81 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
82 }  // namespace benchmark
83 
main(int argc,char ** argv)84 int main(int argc, char** argv) {
85   grpc::testing::TestEnvironment env(argc, argv);
86   LibraryInitializer libInit;
87   ::benchmark::Initialize(&argc, argv);
88   ::grpc::testing::InitTest(&argc, &argv, false);
89   benchmark::RunTheBenchmarksNamespaced();
90   return 0;
91 }
92