helpers.cc 3.2 KB

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