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/cpp/microbenchmarks/helpers.h"
24 #include "test/cpp/util/test_config.h"
25
26 auto& force_library_initialization = Library::get();
27
28 class ChannelDestroyerFixture {
29 public:
ChannelDestroyerFixture()30 ChannelDestroyerFixture() {}
~ChannelDestroyerFixture()31 virtual ~ChannelDestroyerFixture() {
32 if (channel_) {
33 grpc_channel_destroy(channel_);
34 }
35 }
36 virtual void Init() = 0;
37
38 protected:
39 grpc_channel* channel_ = nullptr;
40 };
41
42 class InsecureChannelFixture : public ChannelDestroyerFixture {
43 public:
InsecureChannelFixture()44 InsecureChannelFixture() {}
Init()45 void Init() override {
46 channel_ = grpc_insecure_channel_create("localhost:1234", nullptr, nullptr);
47 }
48 };
49
50 class LameChannelFixture : public ChannelDestroyerFixture {
51 public:
LameChannelFixture()52 LameChannelFixture() {}
Init()53 void Init() override {
54 channel_ = grpc_lame_client_channel_create(
55 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
56 }
57 };
58
59 template <class Fixture>
BM_InsecureChannelCreateDestroy(benchmark::State & state)60 static void BM_InsecureChannelCreateDestroy(benchmark::State& state) {
61 // In order to test if channel creation time is affected by the number of
62 // already existing channels, we create some initial channels here.
63 Fixture initial_channels[512];
64 for (int i = 0; i < state.range(0); i++) {
65 initial_channels[i].Init();
66 }
67 while (state.KeepRunning()) {
68 Fixture channel;
69 channel.Init();
70 }
71 }
72 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, InsecureChannelFixture)
73 ->Range(0, 512);
74 ;
75 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, LameChannelFixture)
76 ->Range(0, 512);
77 ;
78
79 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
80 // and others do not. This allows us to support both modes.
81 namespace benchmark {
RunTheBenchmarksNamespaced()82 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
83 } // namespace benchmark
84
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86 ::benchmark::Initialize(&argc, argv);
87 ::grpc::testing::InitTest(&argc, &argv, false);
88 benchmark::RunTheBenchmarksNamespaced();
89 return 0;
90 }
91