d4070de748138e0016d43b877b78197aaaaeddeb
[platform/upstream/grpc.git] / test / cpp / microbenchmarks / helpers.cc
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 <string.h>
20
21 #include "test/cpp/microbenchmarks/helpers.h"
22
23 static grpc::internal::GrpcLibraryInitializer g_gli_initializer;
24
25 Library::Library() {
26   g_gli_initializer.summon();
27 #ifdef GPR_LOW_LEVEL_COUNTERS
28   grpc_memory_counters_init();
29 #endif
30   init_lib_.init();
31   rq_ = grpc_resource_quota_create("bm");
32 }
33
34 void TrackCounters::Finish(benchmark::State& state) {
35   std::ostringstream out;
36   for (const auto& l : labels_) {
37     out << l << ' ';
38   }
39   AddToLabel(out, state);
40   std::string label = out.str();
41   if (label.length() && label[0] == ' ') {
42     label = label.substr(1);
43   }
44   state.SetLabel(label.c_str());
45 }
46
47 void TrackCounters::AddLabel(const grpc::string& label) {
48   labels_.push_back(label);
49 }
50
51 void TrackCounters::AddToLabel(std::ostream& out, benchmark::State& state) {
52 #ifdef GRPC_COLLECT_STATS
53   grpc_stats_data stats_end;
54   grpc_stats_collect(&stats_end);
55   grpc_stats_data stats;
56   grpc_stats_diff(&stats_end, &stats_begin_, &stats);
57   for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
58     out << " " << grpc_stats_counter_name[i] << "/iter:"
59         << (static_cast<double>(stats.counters[i]) /
60             static_cast<double>(state.iterations()));
61   }
62   for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
63     out << " " << grpc_stats_histogram_name[i] << "-median:"
64         << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 50.0)
65         << " " << grpc_stats_histogram_name[i] << "-99p:"
66         << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 99.0);
67   }
68 #endif
69 #ifdef GPR_LOW_LEVEL_COUNTERS
70   grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot();
71   out << " locks/iter:"
72       << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) -
73                    mu_locks_at_start_) /
74           (double)state.iterations())
75       << " atm_cas/iter:"
76       << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) -
77                    atm_cas_at_start_) /
78           (double)state.iterations())
79       << " atm_add/iter:"
80       << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) -
81                    atm_add_at_start_) /
82           (double)state.iterations())
83       << " nows/iter:"
84       << ((double)(gpr_atm_no_barrier_load(&gpr_now_call_count) -
85                    now_calls_at_start_) /
86           (double)state.iterations())
87       << " allocs/iter:"
88       << ((double)(counters_at_end.total_allocs_absolute -
89                    counters_at_start_.total_allocs_absolute) /
90           (double)state.iterations());
91 #endif
92 }