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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
base / synchronization / waitable_event_perftest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/raw_ptr.h"
#include "base/synchronization/waitable_event.h"
#include <string>
#include "base/threading/simple_thread.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"
namespace base {
namespace {
constexpr char kMetricPrefixWaitableEvent[] = "WaitableEvent.";
constexpr char kMetricWaitTime[] = "wait_time_per_sample";
constexpr char kMetricSignalTime[] = "signal_time_per_sample";
constexpr char kMetricElapsedCycles[] = "elapsed_cycles";
constexpr char kStorySingleThread[] = "single_thread_1000_samples";
constexpr char kStoryMultiThreadWaiter[] = "multi_thread_1000_samples_waiter";
constexpr char kStoryMultiThreadSignaler[] =
"multi_thread_1000_samples_signaler";
constexpr char kStoryTimedThroughput[] = "timed_throughput";
perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) {
perf_test::PerfResultReporter reporter(kMetricPrefixWaitableEvent,
story_name);
reporter.RegisterImportantMetric(kMetricWaitTime, "ns");
reporter.RegisterImportantMetric(kMetricSignalTime, "ns");
reporter.RegisterImportantMetric(kMetricElapsedCycles, "count");
return reporter;
}
class TraceWaitableEvent {
public:
TraceWaitableEvent() = default;
TraceWaitableEvent(const TraceWaitableEvent&) = delete;
TraceWaitableEvent& operator=(const TraceWaitableEvent&) = delete;
~TraceWaitableEvent() = default;
void Signal() {
ElapsedTimer timer;
event_.Signal();
total_signal_time_ += timer.Elapsed();
++signal_samples_;
}
void Wait() {
ElapsedTimer timer;
event_.Wait();
total_wait_time_ += timer.Elapsed();
++wait_samples_;
}
bool TimedWaitUntil(const TimeTicks& end_time) {
ElapsedTimer timer;
const bool signaled = event_.TimedWait(end_time - timer.start_time());
total_wait_time_ += timer.Elapsed();
++wait_samples_;
return signaled;
}
bool IsSignaled() { return event_.IsSignaled(); }
TimeDelta total_signal_time() const { return total_signal_time_; }
TimeDelta total_wait_time() const { return total_wait_time_; }
size_t signal_samples() const { return signal_samples_; }
size_t wait_samples() const { return wait_samples_; }
private:
WaitableEvent event_{WaitableEvent::ResetPolicy::AUTOMATIC};
TimeDelta total_signal_time_;
TimeDelta total_wait_time_;
size_t signal_samples_ = 0U;
size_t wait_samples_ = 0U;
};
class SignalerThread : public SimpleThread {
public:
SignalerThread(TraceWaitableEvent* waiter, TraceWaitableEvent* signaler)
: SimpleThread("WaitableEventPerfTest signaler"),
waiter_(waiter),
signaler_(signaler) {}
SignalerThread(const SignalerThread&) = delete;
SignalerThread& operator=(const SignalerThread&) = delete;
~SignalerThread() override = default;
void Run() override {
while (!stop_event_.IsSignaled()) {
if (waiter_)
waiter_->Wait();
if (signaler_)
signaler_->Signal();
}
}
// Signals the thread to stop on the next iteration of its loop (which
// will happen immediately if no |waiter_| is present or is signaled.
void RequestStop() { stop_event_.Signal(); }
private:
WaitableEvent stop_event_;
raw_ptr<TraceWaitableEvent> waiter_;
raw_ptr<TraceWaitableEvent> signaler_;
};
void PrintPerfWaitableEvent(const TraceWaitableEvent* event,
const std::string& story_name,
size_t* elapsed_cycles = nullptr) {
auto reporter = SetUpReporter(story_name);
reporter.AddResult(
kMetricSignalTime,
static_cast<size_t>(event->total_signal_time().InNanoseconds()) /
event->signal_samples());
reporter.AddResult(
kMetricWaitTime,
static_cast<size_t>(event->total_wait_time().InNanoseconds()) /
event->wait_samples());
if (elapsed_cycles) {
reporter.AddResult(kMetricElapsedCycles, *elapsed_cycles);
}
}
} // namespace
TEST(WaitableEventPerfTest, SingleThread) {
const size_t kSamples = 1000;
TraceWaitableEvent event;
for (size_t i = 0; i < kSamples; ++i) {
event.Signal();
event.Wait();
}
PrintPerfWaitableEvent(&event, kStorySingleThread);
}
TEST(WaitableEventPerfTest, MultipleThreads) {
const size_t kSamples = 1000;
TraceWaitableEvent waiter;
TraceWaitableEvent signaler;
// The other thread will wait and signal on the respective opposite events.
SignalerThread thread(&signaler, &waiter);
thread.Start();
for (size_t i = 0; i < kSamples; ++i) {
signaler.Signal();
waiter.Wait();
}
// Signal the stop event and then make sure the signaler event it is
// waiting on is also signaled.
thread.RequestStop();
signaler.Signal();
thread.Join();
PrintPerfWaitableEvent(&waiter, kStoryMultiThreadWaiter);
PrintPerfWaitableEvent(&signaler, kStoryMultiThreadSignaler);
}
TEST(WaitableEventPerfTest, Throughput) {
TraceWaitableEvent event;
SignalerThread thread(nullptr, &event);
thread.Start();
const TimeTicks end_time = TimeTicks::Now() + Seconds(1);
size_t count = 0;
while (event.TimedWaitUntil(end_time)) {
++count;
}
thread.RequestStop();
thread.Join();
PrintPerfWaitableEvent(&event, kStoryTimedThroughput, &count);
}
} // namespace base