blocking_counter_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2021 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 <limits>
16 
17 #include "absl/synchronization/blocking_counter.h"
18 #include "absl/synchronization/internal/thread_pool.h"
19 #include "benchmark/benchmark.h"
20 
21 namespace {
22 
23 void BM_BlockingCounter_SingleThread(benchmark::State& state) {
24  for (auto _ : state) {
25  int iterations = state.range(0);
26  absl::BlockingCounter counter{iterations};
27  for (int i = 0; i < iterations; ++i) {
28  counter.DecrementCount();
29  }
30  counter.Wait();
31  }
32 }
33 BENCHMARK(BM_BlockingCounter_SingleThread)
34  ->ArgName("iterations")
35  ->Arg(2)
36  ->Arg(4)
37  ->Arg(16)
38  ->Arg(64)
39  ->Arg(256);
40 
41 void BM_BlockingCounter_DecrementCount(benchmark::State& state) {
44  for (auto _ : state) {
45  counter->DecrementCount();
46  }
47 }
48 BENCHMARK(BM_BlockingCounter_DecrementCount)
49  ->Threads(2)
50  ->Threads(4)
51  ->Threads(6)
52  ->Threads(8)
53  ->Threads(10)
54  ->Threads(12)
55  ->Threads(16)
56  ->Threads(32)
57  ->Threads(64)
58  ->Threads(128);
59 
60 void BM_BlockingCounter_Wait(benchmark::State& state) {
61  int num_threads = state.range(0);
63  for (auto _ : state) {
65  pool.Schedule([num_threads, &counter, &pool]() {
66  for (int i = 0; i < num_threads; ++i) {
67  pool.Schedule([&counter]() { counter.DecrementCount(); });
68  }
69  });
70  counter.Wait();
71  }
72 }
73 BENCHMARK(BM_BlockingCounter_Wait)
74  ->ArgName("threads")
75  ->Arg(2)
76  ->Arg(4)
77  ->Arg(8)
78  ->Arg(16)
79  ->Arg(32)
80  ->Arg(64)
81  ->Arg(128);
82 
83 } // namespace
absl::synchronization_internal::ThreadPool
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:33
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
counter
static int counter
Definition: abseil-cpp/absl/flags/reflection_test.cc:131
absl::BlockingCounter
Definition: abseil-cpp/absl/synchronization/blocking_counter.h:63
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
num_threads
static volatile int num_threads
Definition: benchmark-thread.c:30
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48