forked from hyderabad-care-center/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion_queue_benchmark.cc
132 lines (118 loc) · 4.17 KB
/
completion_queue_benchmark.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/completion_queue.h"
#include "google/cloud/internal/default_completion_queue_impl.h"
#include <benchmark/benchmark.h>
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace {
// Run on (96 X 2000.15 MHz CPU s)
// CPU Caches:
// L1 Data 32K (x48)
// L1 Instruction 32K (x48)
// L2 Unified 1024K (x48)
// L3 Unified 39424K (x2)
// Load Average: 2.90, 9.21, 73.15
//-----------------------------------------------------------------------------
// Benchmark Time CPU Iterations
//-----------------------------------------------------------------------------
// BM_Baseline/16/512 15345 ns 15344 ns 44845
// BM_Baseline/16/1024 30807 ns 30802 ns 22416
// BM_Baseline/16/2048 62403 ns 62390 ns 11096
// BM_Baseline_BigO 30.37 N 30.37 N
// BM_Baseline_RMS 1 % 1 %
// BM_CompletionQueueRunAsync/16/512 916720 ns 189304 ns 3450
// BM_CompletionQueueRunAsync/16/1024 990193 ns 243485 ns 2668
// BM_CompletionQueueRunAsync/16/2048 1976575 ns 420878 ns 1713
// BM_CompletionQueueRunAsync_BigO 1004.78 N 219.47 N
// BM_CompletionQueueRunAsync_RMS 18 % 17 %
auto constexpr kMinThreads = 16;
auto constexpr kMaxThreads = 16;
auto constexpr kMinExecutions = 1 << 9;
auto constexpr kMaxExecutions = 1 << 11;
class Wait {
public:
explicit Wait(std::int64_t count) : count_(count) {}
void BlockUntilDone() {
std::unique_lock<std::mutex> lk(mu_);
cv_.wait(lk, [&] { return count_ == 0; });
}
void OneDone() {
std::unique_lock<std::mutex> lk(mu_);
if (--count_ == 0) cv_.notify_one();
lk.unlock();
}
private:
std::mutex mu_;
std::condition_variable cv_;
std::int64_t count_;
};
void BM_Baseline(benchmark::State& state) {
auto runner = [&](std::int64_t n) {
Wait wait(n);
std::deque<std::function<void()>> queue;
for (std::int64_t i = 0; i != n; ++i) {
queue.emplace_back([&wait] { wait.OneDone(); });
}
while (!queue.empty()) {
auto f = std::move(queue.front());
queue.pop_front();
f();
}
wait.BlockUntilDone();
return 0;
};
for (auto _ : state) {
benchmark::DoNotOptimize(runner(state.range(1)));
}
state.SetComplexityN(state.range(1));
}
BENCHMARK(BM_Baseline)
->RangeMultiplier(2)
->Ranges({{kMinThreads, kMaxThreads}, {kMinExecutions, kMaxExecutions}})
->Complexity(benchmark::oN);
void BM_CompletionQueueRunAsync(benchmark::State& state) {
CompletionQueue cq;
std::vector<std::thread> tasks(static_cast<std::size_t>(state.range(0)));
std::generate(tasks.begin(), tasks.end(), [&cq] {
return std::thread{[](CompletionQueue cq) { cq.Run(); }, cq};
});
auto runner = [&](std::int64_t n) {
Wait wait(n);
for (std::int64_t i = 0; i != n; ++i) {
cq.RunAsync([&wait] { wait.OneDone(); });
}
wait.BlockUntilDone();
return 0;
};
for (auto _ : state) {
benchmark::DoNotOptimize(runner(state.range(1)));
}
state.SetComplexityN(state.range(1));
cq.Shutdown();
for (auto& t : tasks) t.join();
}
BENCHMARK(BM_CompletionQueueRunAsync)
->RangeMultiplier(2)
->Ranges({{kMinThreads, kMaxThreads}, {kMinExecutions, kMaxExecutions}})
->Complexity(benchmark::oN);
} // namespace
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google