• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Finish(benchmark::State & state)23 void TrackCounters::Finish(benchmark::State& state) {
24   std::ostringstream out;
25   for (const auto& l : labels_) {
26     out << l << ' ';
27   }
28   AddToLabel(out, state);
29   std::string label = out.str();
30   if (label.length() && label[0] == ' ') {
31     label = label.substr(1);
32   }
33   state.SetLabel(label.c_str());
34 }
35 
AddLabel(const grpc::string & label)36 void TrackCounters::AddLabel(const grpc::string& label) {
37   labels_.push_back(label);
38 }
39 
AddToLabel(std::ostream & out,benchmark::State & state)40 void TrackCounters::AddToLabel(std::ostream& out, benchmark::State& state) {
41 #ifdef GRPC_COLLECT_STATS
42   grpc_stats_data stats_end;
43   grpc_stats_collect(&stats_end);
44   grpc_stats_data stats;
45   grpc_stats_diff(&stats_end, &stats_begin_, &stats);
46   for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
47     out << " " << grpc_stats_counter_name[i] << "/iter:"
48         << (static_cast<double>(stats.counters[i]) /
49             static_cast<double>(state.iterations()));
50   }
51   for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
52     out << " " << grpc_stats_histogram_name[i] << "-median:"
53         << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 50.0)
54         << " " << grpc_stats_histogram_name[i] << "-99p:"
55         << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 99.0);
56   }
57 #endif
58 #ifdef GPR_LOW_LEVEL_COUNTERS
59   grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot();
60   out << " locks/iter:"
61       << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) -
62                    mu_locks_at_start_) /
63           (double)state.iterations())
64       << " atm_cas/iter:"
65       << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) -
66                    atm_cas_at_start_) /
67           (double)state.iterations())
68       << " atm_add/iter:"
69       << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) -
70                    atm_add_at_start_) /
71           (double)state.iterations())
72       << " nows/iter:"
73       << ((double)(gpr_atm_no_barrier_load(&gpr_now_call_count) -
74                    now_calls_at_start_) /
75           (double)state.iterations())
76       << " allocs/iter:"
77       << ((double)(counters_at_end.total_allocs_absolute -
78                    counters_at_start_.total_allocs_absolute) /
79           (double)state.iterations());
80 #endif
81 }
82