• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
BM_BlockingCounter_SingleThread(benchmark::State & state)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 
BM_BlockingCounter_DecrementCount(benchmark::State & state)41 void BM_BlockingCounter_DecrementCount(benchmark::State& state) {
42   static absl::BlockingCounter* counter =
43       new absl::BlockingCounter{std::numeric_limits<int>::max()};
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 
BM_BlockingCounter_Wait(benchmark::State & state)60 void BM_BlockingCounter_Wait(benchmark::State& state) {
61   int num_threads = state.range(0);
62   absl::synchronization_internal::ThreadPool pool(num_threads);
63   for (auto _ : state) {
64     absl::BlockingCounter counter{num_threads};
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
84