periodic_sampler_benchmark.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/profiling/internal/periodic_sampler.h"
  15. #include "benchmark/benchmark.h"
  16. namespace absl {
  17. ABSL_NAMESPACE_BEGIN
  18. namespace profiling_internal {
  19. namespace {
  20. template <typename Sampler>
  21. void BM_Sample(Sampler* sampler, benchmark::State& state) {
  22. for (auto _ : state) {
  23. benchmark::DoNotOptimize(sampler);
  24. benchmark::DoNotOptimize(sampler->Sample());
  25. }
  26. }
  27. template <typename Sampler>
  28. void BM_SampleMinunumInlined(Sampler* sampler, benchmark::State& state) {
  29. for (auto _ : state) {
  30. benchmark::DoNotOptimize(sampler);
  31. if (ABSL_PREDICT_FALSE(sampler->SubtleMaybeSample())) {
  32. benchmark::DoNotOptimize(sampler->SubtleConfirmSample());
  33. }
  34. }
  35. }
  36. void BM_PeriodicSampler_TinySample(benchmark::State& state) {
  37. struct Tag {};
  38. PeriodicSampler<Tag, 10> sampler;
  39. BM_Sample(&sampler, state);
  40. }
  41. BENCHMARK(BM_PeriodicSampler_TinySample);
  42. void BM_PeriodicSampler_ShortSample(benchmark::State& state) {
  43. struct Tag {};
  44. PeriodicSampler<Tag, 1024> sampler;
  45. BM_Sample(&sampler, state);
  46. }
  47. BENCHMARK(BM_PeriodicSampler_ShortSample);
  48. void BM_PeriodicSampler_LongSample(benchmark::State& state) {
  49. struct Tag {};
  50. PeriodicSampler<Tag, 1024 * 1024> sampler;
  51. BM_Sample(&sampler, state);
  52. }
  53. BENCHMARK(BM_PeriodicSampler_LongSample);
  54. void BM_PeriodicSampler_LongSampleMinunumInlined(benchmark::State& state) {
  55. struct Tag {};
  56. PeriodicSampler<Tag, 1024 * 1024> sampler;
  57. BM_SampleMinunumInlined(&sampler, state);
  58. }
  59. BENCHMARK(BM_PeriodicSampler_LongSampleMinunumInlined);
  60. void BM_PeriodicSampler_Disabled(benchmark::State& state) {
  61. struct Tag {};
  62. PeriodicSampler<Tag, 0> sampler;
  63. BM_Sample(&sampler, state);
  64. }
  65. BENCHMARK(BM_PeriodicSampler_Disabled);
  66. } // namespace
  67. } // namespace profiling_internal
  68. ABSL_NAMESPACE_END
  69. } // namespace absl