helpers.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef TEST_CPP_MICROBENCHMARKS_COUNTERS_H
  19. #define TEST_CPP_MICROBENCHMARKS_COUNTERS_H
  20. #include <grpc/support/port_platform.h>
  21. #include <sstream>
  22. #include <vector>
  23. #include <benchmark/benchmark.h>
  24. #include <grpcpp/impl/grpc_library.h>
  25. #include "src/core/lib/debug/stats.h"
  26. class LibraryInitializer {
  27. public:
  28. LibraryInitializer();
  29. ~LibraryInitializer();
  30. static LibraryInitializer& get();
  31. private:
  32. grpc::internal::GrpcLibrary init_lib_;
  33. };
  34. #ifdef GPR_LOW_LEVEL_COUNTERS
  35. extern gpr_atm gpr_mu_locks;
  36. extern gpr_atm gpr_counter_atm_cas;
  37. extern gpr_atm gpr_counter_atm_add;
  38. extern gpr_atm gpr_now_call_count;
  39. #endif
  40. class TrackCounters {
  41. public:
  42. TrackCounters() { grpc_stats_collect(&stats_begin_); }
  43. virtual ~TrackCounters() {}
  44. virtual void Finish(benchmark::State& state);
  45. virtual void AddLabel(const std::string& label);
  46. virtual void AddToLabel(std::ostream& out, benchmark::State& state);
  47. private:
  48. grpc_stats_data stats_begin_;
  49. std::vector<std::string> labels_;
  50. #ifdef GPR_LOW_LEVEL_COUNTERS
  51. const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks);
  52. const size_t atm_cas_at_start_ =
  53. gpr_atm_no_barrier_load(&gpr_counter_atm_cas);
  54. const size_t atm_add_at_start_ =
  55. gpr_atm_no_barrier_load(&gpr_counter_atm_add);
  56. const size_t now_calls_at_start_ =
  57. gpr_atm_no_barrier_load(&gpr_now_call_count);
  58. #endif
  59. };
  60. #endif