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