xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/log/log_benchmark.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2022 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/base/attributes.h"
16 #include "absl/base/log_severity.h"
17 #include "absl/flags/flag.h"
18 #include "absl/log/check.h"
19 #include "absl/log/globals.h"
20 #include "absl/log/log.h"
21 #include "absl/log/log_entry.h"
22 #include "absl/log/log_sink.h"
23 #include "absl/log/log_sink_registry.h"
24 #include "benchmark/benchmark.h"
25 
26 namespace {
27 
28 class NullLogSink : public absl::LogSink {
29  public:
NullLogSink()30   NullLogSink() { absl::AddLogSink(this); }
31 
~NullLogSink()32   ~NullLogSink() override { absl::RemoveLogSink(this); }
33 
Send(const absl::LogEntry &)34   void Send(const absl::LogEntry&) override {}
35 };
36 
37 constexpr int x = -1;
38 
BM_SuccessfulBinaryCheck(benchmark::State & state)39 void BM_SuccessfulBinaryCheck(benchmark::State& state) {
40   int n = 0;
41   while (state.KeepRunningBatch(8)) {
42     CHECK_GE(n, x);
43     CHECK_GE(n, x);
44     CHECK_GE(n, x);
45     CHECK_GE(n, x);
46     CHECK_GE(n, x);
47     CHECK_GE(n, x);
48     CHECK_GE(n, x);
49     CHECK_GE(n, x);
50     ++n;
51   }
52   benchmark::DoNotOptimize(n);
53 }
54 BENCHMARK(BM_SuccessfulBinaryCheck);
55 
BM_SuccessfulUnaryCheck(benchmark::State & state)56 static void BM_SuccessfulUnaryCheck(benchmark::State& state) {
57   int n = 0;
58   while (state.KeepRunningBatch(8)) {
59     CHECK(n >= x);
60     CHECK(n >= x);
61     CHECK(n >= x);
62     CHECK(n >= x);
63     CHECK(n >= x);
64     CHECK(n >= x);
65     CHECK(n >= x);
66     CHECK(n >= x);
67     ++n;
68   }
69   benchmark::DoNotOptimize(n);
70 }
71 BENCHMARK(BM_SuccessfulUnaryCheck);
72 
BM_DisabledLogOverhead(benchmark::State & state)73 static void BM_DisabledLogOverhead(benchmark::State& state) {
74   absl::ScopedStderrThreshold disable_stderr_logging(
75       absl::LogSeverityAtLeast::kInfinity);
76   absl::log_internal::ScopedMinLogLevel scoped_min_log_level(
77       absl::LogSeverityAtLeast::kInfinity);
78   for (auto _ : state) {
79     LOG(INFO);
80   }
81 }
82 BENCHMARK(BM_DisabledLogOverhead);
83 
BM_EnabledLogOverhead(benchmark::State & state)84 static void BM_EnabledLogOverhead(benchmark::State& state) {
85   absl::ScopedStderrThreshold stderr_logging(
86       absl::LogSeverityAtLeast::kInfinity);
87   absl::log_internal::ScopedMinLogLevel scoped_min_log_level(
88       absl::LogSeverityAtLeast::kInfo);
89   ABSL_ATTRIBUTE_UNUSED NullLogSink null_sink;
90   for (auto _ : state) {
91     LOG(INFO);
92   }
93 }
94 BENCHMARK(BM_EnabledLogOverhead);
95 
96 }  // namespace
97 
98