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 #include <benchmark/benchmark.h>
20
21 #include "src/core/lib/transport/metadata.h"
22
23 namespace grpc_core {
24 namespace {
25
BM_MetadataMapCreateDestroy(benchmark::State & state)26 void BM_MetadataMapCreateDestroy(benchmark::State& state) {
27 for (auto _ : state) {
28 auto md = Arena::MakePooledForOverwrite<ServerMetadata>();
29 }
30 }
31 BENCHMARK(BM_MetadataMapCreateDestroy);
32
BM_MetadataMapCreateDestroyOnStack(benchmark::State & state)33 void BM_MetadataMapCreateDestroyOnStack(benchmark::State& state) {
34 for (auto _ : state) {
35 ServerMetadata md;
36 }
37 }
38 BENCHMARK(BM_MetadataMapCreateDestroyOnStack);
39
BM_MetadataMapCreateDestroySetStatus(benchmark::State & state)40 void BM_MetadataMapCreateDestroySetStatus(benchmark::State& state) {
41 auto message = Slice::FromExternalString("message");
42 for (auto _ : state) {
43 auto md = Arena::MakePooledForOverwrite<ServerMetadata>();
44 md->Set(GrpcStatusMetadata(), GRPC_STATUS_UNKNOWN);
45 md->Set(GrpcMessageMetadata(), message.Copy());
46 }
47 }
48 BENCHMARK(BM_MetadataMapCreateDestroySetStatus);
49
BM_MetadataMapCreateDestroySetStatusCancelled(benchmark::State & state)50 void BM_MetadataMapCreateDestroySetStatusCancelled(benchmark::State& state) {
51 auto message = Slice::FromExternalString("message");
52 for (auto _ : state) {
53 auto md = Arena::MakePooledForOverwrite<ServerMetadata>();
54 md->Set(GrpcStatusMetadata(), GRPC_STATUS_CANCELLED);
55 }
56 }
57 BENCHMARK(BM_MetadataMapCreateDestroySetStatusCancelled);
58
BM_MetadataMapFromAbslStatusCancelled(benchmark::State & state)59 void BM_MetadataMapFromAbslStatusCancelled(benchmark::State& state) {
60 for (auto _ : state) {
61 ServerMetadataFromStatus(absl::CancelledError());
62 }
63 }
64 BENCHMARK(BM_MetadataMapFromAbslStatusCancelled);
65
BM_MetadataMapFromAbslStatusOk(benchmark::State & state)66 void BM_MetadataMapFromAbslStatusOk(benchmark::State& state) {
67 for (auto _ : state) {
68 ServerMetadataFromStatus(absl::OkStatus());
69 }
70 }
71 BENCHMARK(BM_MetadataMapFromAbslStatusOk);
72
73 } // namespace
74 } // namespace grpc_core
75
76 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
77 // and others do not. This allows us to support both modes.
78 namespace benchmark {
RunTheBenchmarksNamespaced()79 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
80 } // namespace benchmark
81
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83 ::benchmark::Initialize(&argc, argv);
84 benchmark::RunTheBenchmarksNamespaced();
85 return 0;
86 }
87