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 
15 #include "absl/profiling/internal/periodic_sampler.h"
16 #include "benchmark/benchmark.h"
17 
18 namespace absl {
19 ABSL_NAMESPACE_BEGIN
20 namespace profiling_internal {
21 namespace {
22 
23 template <typename Sampler>
BM_Sample(Sampler * sampler,benchmark::State & state)24 void BM_Sample(Sampler* sampler, benchmark::State& state) {
25   for (auto _ : state) {
26     benchmark::DoNotOptimize(sampler);
27     benchmark::DoNotOptimize(sampler->Sample());
28   }
29 }
30 
31 template <typename Sampler>
BM_SampleMinunumInlined(Sampler * sampler,benchmark::State & state)32 void BM_SampleMinunumInlined(Sampler* sampler, benchmark::State& state) {
33   for (auto _ : state) {
34     benchmark::DoNotOptimize(sampler);
35     if (ABSL_PREDICT_FALSE(sampler->SubtleMaybeSample())) {
36       benchmark::DoNotOptimize(sampler->SubtleConfirmSample());
37     }
38   }
39 }
40 
BM_PeriodicSampler_TinySample(benchmark::State & state)41 void BM_PeriodicSampler_TinySample(benchmark::State& state) {
42   struct Tag {};
43   PeriodicSampler<Tag, 10> sampler;
44   BM_Sample(&sampler, state);
45 }
46 BENCHMARK(BM_PeriodicSampler_TinySample);
47 
BM_PeriodicSampler_ShortSample(benchmark::State & state)48 void BM_PeriodicSampler_ShortSample(benchmark::State& state) {
49   struct Tag {};
50   PeriodicSampler<Tag, 1024> sampler;
51   BM_Sample(&sampler, state);
52 }
53 BENCHMARK(BM_PeriodicSampler_ShortSample);
54 
BM_PeriodicSampler_LongSample(benchmark::State & state)55 void BM_PeriodicSampler_LongSample(benchmark::State& state) {
56   struct Tag {};
57   PeriodicSampler<Tag, 1024 * 1024> sampler;
58   BM_Sample(&sampler, state);
59 }
60 BENCHMARK(BM_PeriodicSampler_LongSample);
61 
BM_PeriodicSampler_LongSampleMinunumInlined(benchmark::State & state)62 void BM_PeriodicSampler_LongSampleMinunumInlined(benchmark::State& state) {
63   struct Tag {};
64   PeriodicSampler<Tag, 1024 * 1024> sampler;
65   BM_SampleMinunumInlined(&sampler, state);
66 }
67 BENCHMARK(BM_PeriodicSampler_LongSampleMinunumInlined);
68 
BM_PeriodicSampler_Disabled(benchmark::State & state)69 void BM_PeriodicSampler_Disabled(benchmark::State& state) {
70   struct Tag {};
71   PeriodicSampler<Tag, 0> sampler;
72   BM_Sample(&sampler, state);
73 }
74 BENCHMARK(BM_PeriodicSampler_Disabled);
75 
76 }  // namespace
77 }  // namespace profiling_internal
78 ABSL_NAMESPACE_END
79 }  // namespace absl
80