1 // Copyright 2017 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Benchmark ChannelArgs comparison performance between grpc_channel_args and
16 // grpc_core::ChannelArgs
17
18 #include <benchmark/benchmark.h>
19 #include <grpcpp/support/channel_arguments.h>
20
21 #include <random>
22
23 #include "absl/container/btree_map.h"
24 #include "src/core/lib/channel/channel_args.h"
25
26 const char kKey[] = "a very long key";
27 const char kValue[] = "a very long value";
28
BM_ChannelArgs(benchmark::State & state)29 void BM_ChannelArgs(benchmark::State& state) {
30 grpc_core::ChannelArgs arg1, arg2;
31 arg1 = arg1.Set(kKey, kValue);
32 arg2 = arg2.Set(kKey, kValue);
33 for (auto s : state) {
34 benchmark::DoNotOptimize(arg1 < arg2);
35 }
36 }
37 BENCHMARK(BM_ChannelArgs);
38
BM_grpc_channel_args(benchmark::State & state)39 void BM_grpc_channel_args(benchmark::State& state) {
40 grpc_channel_args arg1, arg2;
41 grpc::ChannelArguments xargs;
42 xargs.SetString(kKey, kValue);
43 xargs.SetChannelArgs(&arg1);
44 xargs.SetChannelArgs(&arg2);
45 for (auto s : state) {
46 benchmark::DoNotOptimize(grpc_channel_args_compare(&arg1, &arg2));
47 }
48 }
49 BENCHMARK(BM_grpc_channel_args);
50
BM_ChannelArgsAsKeyIntoMap(benchmark::State & state)51 void BM_ChannelArgsAsKeyIntoMap(benchmark::State& state) {
52 std::map<grpc_core::ChannelArgs, int> m;
53 std::vector<grpc_core::ChannelArgs> v;
54 for (int i = 0; i < 10000; i++) {
55 const auto& a = grpc_core::ChannelArgs().Set(kKey, i);
56 m[a] = i;
57 v.push_back(a);
58 }
59 std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()()));
60 size_t n = 0;
61 for (auto s : state) {
62 benchmark::DoNotOptimize(m.find(v[n++ % v.size()]));
63 }
64 }
65 BENCHMARK(BM_ChannelArgsAsKeyIntoMap);
66
BM_ChannelArgsAsKeyIntoBTree(benchmark::State & state)67 void BM_ChannelArgsAsKeyIntoBTree(benchmark::State& state) {
68 absl::btree_map<grpc_core::ChannelArgs, int> m;
69 std::vector<grpc_core::ChannelArgs> v;
70 for (int i = 0; i < 10000; i++) {
71 const auto& a = grpc_core::ChannelArgs().Set(kKey, i);
72 m[a] = i;
73 v.push_back(a);
74 }
75 std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()()));
76 size_t n = 0;
77 for (auto s : state) {
78 benchmark::DoNotOptimize(m.find(v[n++ % v.size()]));
79 }
80 }
81 BENCHMARK(BM_ChannelArgsAsKeyIntoBTree);
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 ::benchmark::Initialize(&argc, argv);
91 benchmark::RunTheBenchmarksNamespaced();
92 return 0;
93 }
94