3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /* Benchmark channel */
21 #include <benchmark/benchmark.h>
23 #include <grpc/grpc.h>
25 #include "test/core/util/test_config.h"
26 #include "test/cpp/microbenchmarks/helpers.h"
27 #include "test/cpp/util/test_config.h"
29 class ChannelDestroyerFixture {
31 ChannelDestroyerFixture() {}
32 virtual ~ChannelDestroyerFixture() {
34 grpc_channel_destroy(channel_);
37 virtual void Init() = 0;
40 grpc_channel* channel_ = nullptr;
43 class InsecureChannelFixture : public ChannelDestroyerFixture {
45 InsecureChannelFixture() {}
46 void Init() override {
47 channel_ = grpc_insecure_channel_create("localhost:1234", nullptr, nullptr);
51 class LameChannelFixture : public ChannelDestroyerFixture {
53 LameChannelFixture() {}
54 void Init() override {
55 channel_ = grpc_lame_client_channel_create(
56 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
60 template <class Fixture>
61 static void BM_InsecureChannelCreateDestroy(benchmark::State& state) {
62 // In order to test if channel creation time is affected by the number of
63 // already existing channels, we create some initial channels here.
64 Fixture initial_channels[512];
65 for (int i = 0; i < state.range(0); i++) {
66 initial_channels[i].Init();
68 for (auto _ : state) {
73 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, InsecureChannelFixture)
76 BENCHMARK_TEMPLATE(BM_InsecureChannelCreateDestroy, LameChannelFixture)
80 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
81 // and others do not. This allows us to support both modes.
83 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
84 } // namespace benchmark
86 int main(int argc, char** argv) {
87 grpc::testing::TestEnvironment env(argc, argv);
88 LibraryInitializer libInit;
89 ::benchmark::Initialize(&argc, argv);
90 ::grpc::testing::InitTest(&argc, &argv, false);
91 benchmark::RunTheBenchmarksNamespaced();