| 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 <random> |
| 19 |
|
| 20 |
#include <benchmark/benchmark.h> |
| 21 |
|
| 22 |
#include "absl/container/btree_map.h" |
| 23 |
|
| 24 |
#include <grpcpp/support/channel_arguments.h> |
| 25 |
|
| 26 |
#include "src/core/lib/channel/channel_args.h" |
| 27 |
|
| 28 |
const char kKey[] = "a very long key"; |
| 29 |
const char kValue[] = "a very long value"; |
| 30 |
|
| 31 |
void BM_ChannelArgs(benchmark::State& state) { |
| 32 |
grpc_core::ChannelArgs arg1, arg2; |
| 33 |
arg1 = arg1.Set(kKey, kValue); |
| 34 |
arg2 = arg2.Set(kKey, kValue); |
| 35 |
for (auto s : state) { |
| 36 |
benchmark::DoNotOptimize(arg1 < arg2); |
| 37 |
} |
| 38 |
} |
| 39 |
BENCHMARK(BM_ChannelArgs); |
| 40 |
|
| 41 |
void BM_grpc_channel_args(benchmark::State& state) { |
| 42 |
grpc_channel_args arg1, arg2; |
| 43 |
grpc::ChannelArguments xargs; |
| 44 |
xargs.SetString(kKey, kValue); |
| 45 |
xargs.SetChannelArgs(&arg1); |
| 46 |
xargs.SetChannelArgs(&arg2); |
| 47 |
for (auto s : state) { |
| 48 |
benchmark::DoNotOptimize(grpc_channel_args_compare(&arg1, &arg2)); |
| 49 |
} |
| 50 |
} |
| 51 |
BENCHMARK(BM_grpc_channel_args); |
| 52 |
|
| 53 |
void BM_ChannelArgsAsKeyIntoMap(benchmark::State& state) { |
| 54 |
std::map<grpc_core::ChannelArgs, int> m; |
| 55 |
std::vector<grpc_core::ChannelArgs> v; |
| 56 |
for (int i = 0; i < 10000; i++) { |
| 57 |
const auto& a = grpc_core::ChannelArgs().Set(kKey, i); |
| 58 |
m[a] = i; |
| 59 |
v.push_back(a); |
| 60 |
} |
| 61 |
std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()())); |
| 62 |
size_t n = 0; |
| 63 |
for (auto s : state) { |
| 64 |
benchmark::DoNotOptimize(m.find(v[n++ % v.size()])); |
| 65 |
} |
| 66 |
} |
| 67 |
BENCHMARK(BM_ChannelArgsAsKeyIntoMap); |
| 68 |
|
| 69 |
void BM_ChannelArgsAsKeyIntoBTree(benchmark::State& state) { |
| 70 |
absl::btree_map<grpc_core::ChannelArgs, int> m; |
| 71 |
std::vector<grpc_core::ChannelArgs> v; |
| 72 |
for (int i = 0; i < 10000; i++) { |
| 73 |
const auto& a = grpc_core::ChannelArgs().Set(kKey, i); |
| 74 |
m[a] = i; |
| 75 |
v.push_back(a); |
| 76 |
} |
| 77 |
std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()())); |
| 78 |
size_t n = 0; |
| 79 |
for (auto s : state) { |
| 80 |
benchmark::DoNotOptimize(m.find(v[n++ % v.size()])); |
| 81 |
} |
| 82 |
} |
| 83 |
BENCHMARK(BM_ChannelArgsAsKeyIntoBTree); |
| 84 |
|
| 85 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 86 |
// and others do not. This allows us to support both modes. |
| 87 |
namespace benchmark { |
| 88 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 89 |
} // namespace benchmark |
| 90 |
|
| 91 |
int main(int argc, char** argv) { |
| 92 |
::benchmark::Initialize(&argc, argv); |
| 93 |
benchmark::RunTheBenchmarksNamespaced(); |
| 94 |
return 0; |
| 95 |
} |