3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "test/cpp/microbenchmarks/helpers.h"
23 static grpc::internal::GrpcLibraryInitializer g_gli_initializer;
26 g_gli_initializer.summon();
27 #ifdef GPR_LOW_LEVEL_COUNTERS
28 grpc_memory_counters_init();
31 rq_ = grpc_resource_quota_create("bm");
34 void TrackCounters::Finish(benchmark::State& state) {
35 std::ostringstream out;
36 for (const auto& l : labels_) {
39 AddToLabel(out, state);
40 std::string label = out.str();
41 if (label.length() && label[0] == ' ') {
42 label = label.substr(1);
44 state.SetLabel(label.c_str());
47 void TrackCounters::AddLabel(const grpc::string& label) {
48 labels_.push_back(label);
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()));
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);
69 #ifdef GPR_LOW_LEVEL_COUNTERS
70 grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot();
72 << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) -
74 (double)state.iterations())
76 << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) -
78 (double)state.iterations())
80 << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) -
82 (double)state.iterations())
84 << ((double)(gpr_atm_no_barrier_load(&gpr_now_call_count) -
85 now_calls_at_start_) /
86 (double)state.iterations())
88 << ((double)(counters_at_end.total_allocs_absolute -
89 counters_at_start_.total_allocs_absolute) /
90 (double)state.iterations());