1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/base_switches.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/synchronization/condition_variable.h"
10 #include "base/synchronization/lock.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/perf/perf_test.h"
26 const int kNumRuns
= 100000;
28 // Base class for a threading perf-test. This sets up some threads for the
29 // test and measures the clock-time in addition to time spent on each thread.
30 class ThreadPerfTest
: public testing::Test
{
33 : done_(false, false) {
34 // Disable the task profiler as it adds significant cost!
35 CommandLine::Init(0, NULL
);
36 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
37 switches::kProfilerTiming
,
38 switches::kProfilerTimingDisabledValue
);
41 // To be implemented by each test. Subclass must uses threads_ such that
42 // their cpu-time can be measured. Test must return from PingPong() _and_
43 // call FinishMeasurement from any thread to complete the test.
44 virtual void Init() {}
45 virtual void PingPong(int hops
) = 0;
46 virtual void Reset() {}
48 void TimeOnThread(base::TimeTicks
* ticks
, base::WaitableEvent
* done
) {
49 *ticks
= base::TimeTicks::ThreadNow();
53 base::TimeTicks
ThreadNow(base::Thread
* thread
) {
54 base::WaitableEvent
done(false, false);
55 base::TimeTicks ticks
;
56 thread
->message_loop_proxy()->PostTask(
58 base::Bind(&ThreadPerfTest::TimeOnThread
,
59 base::Unretained(this),
66 void RunPingPongTest(const std::string
& name
, unsigned num_threads
) {
67 // Create threads and collect starting cpu-time for each thread.
68 std::vector
<base::TimeTicks
> thread_starts
;
69 while (threads_
.size() < num_threads
) {
70 threads_
.push_back(new base::Thread("PingPonger"));
71 threads_
.back()->Start();
72 if (base::TimeTicks::IsThreadNowSupported())
73 thread_starts
.push_back(ThreadNow(threads_
.back()));
78 base::TimeTicks start
= base::TimeTicks::HighResNow();
81 base::TimeTicks end
= base::TimeTicks::HighResNow();
83 // Gather the cpu-time spent on each thread. This does one extra tasks,
84 // but that should be in the noise given enough runs.
85 base::TimeDelta thread_time
;
86 while (threads_
.size()) {
87 if (base::TimeTicks::IsThreadNowSupported()) {
88 thread_time
+= ThreadNow(threads_
.back()) - thread_starts
.back();
89 thread_starts
.pop_back();
96 double num_runs
= static_cast<double>(kNumRuns
);
97 double us_per_task_clock
= (end
- start
).InMicroseconds() / num_runs
;
98 double us_per_task_cpu
= thread_time
.InMicroseconds() / num_runs
;
100 // Clock time per task.
101 perf_test::PrintResult(
102 "task", "", name
+ "_time ", us_per_task_clock
, "us/hop", true);
104 // Total utilization across threads if available (likely higher).
105 if (base::TimeTicks::IsThreadNowSupported()) {
106 perf_test::PrintResult(
107 "task", "", name
+ "_cpu ", us_per_task_cpu
, "us/hop", true);
112 void FinishMeasurement() { done_
.Signal(); }
113 ScopedVector
<base::Thread
> threads_
;
116 base::WaitableEvent done_
;
119 // Class to test task performance by posting empty tasks back and forth.
120 class TaskPerfTest
: public ThreadPerfTest
{
121 base::Thread
* NextThread(int count
) {
122 return threads_
[count
% threads_
.size()];
125 virtual void PingPong(int hops
) OVERRIDE
{
130 NextThread(hops
)->message_loop_proxy()->PostTask(
133 &ThreadPerfTest::PingPong
, base::Unretained(this), hops
- 1));
137 // This tries to test the 'best-case' as well as the 'worst-case' task posting
138 // performance. The best-case keeps one thread alive such that it never yeilds,
139 // while the worse-case forces a context switch for every task. Four threads are
140 // used to ensure the threads do yeild (with just two it might be possible for
141 // both threads to stay awake if they can signal each other fast enough).
142 TEST_F(TaskPerfTest
, TaskPingPong
) {
143 RunPingPongTest("1_Task_Threads", 1);
144 RunPingPongTest("4_Task_Threads", 4);
147 // Class to test our WaitableEvent performance by signaling back and fort.
148 // WaitableEvent is templated so we can also compare with other versions.
149 template <typename WaitableEventType
>
150 class EventPerfTest
: public ThreadPerfTest
{
152 virtual void Init() OVERRIDE
{
153 for (size_t i
= 0; i
< threads_
.size(); i
++)
154 events_
.push_back(new WaitableEventType(false, false));
157 virtual void Reset() OVERRIDE
{ events_
.clear(); }
159 void WaitAndSignalOnThread(size_t event
) {
160 size_t next_event
= (event
+ 1) % events_
.size();
163 events_
[event
]->Wait();
164 my_hops
= --remaining_hops_
; // We own 'hops' between Wait and Signal.
165 events_
[next_event
]->Signal();
166 } while (my_hops
> 0);
167 // Once we are done, all threads will signal as hops passes zero.
168 // We only signal completion once, on the thread that reaches zero.
173 virtual void PingPong(int hops
) OVERRIDE
{
174 remaining_hops_
= hops
;
175 for (size_t i
= 0; i
< threads_
.size(); i
++) {
176 threads_
[i
]->message_loop_proxy()->PostTask(
178 base::Bind(&EventPerfTest::WaitAndSignalOnThread
,
179 base::Unretained(this),
183 // Kick off the Signal ping-ponging.
184 events_
.front()->Signal();
188 ScopedVector
<WaitableEventType
> events_
;
191 // Similar to the task posting test, this just tests similar functionality
192 // using WaitableEvents. We only test four threads (worst-case), but we
193 // might want to craft a way to test the best-case (where the thread doesn't
194 // end up blocking because the event is already signalled).
195 typedef EventPerfTest
<base::WaitableEvent
> WaitableEventPerfTest
;
196 TEST_F(WaitableEventPerfTest
, EventPingPong
) {
197 RunPingPongTest("4_WaitableEvent_Threads", 4);
200 // Build a minimal event using ConditionVariable.
201 class ConditionVariableEvent
{
203 ConditionVariableEvent(bool manual_reset
, bool initially_signaled
)
204 : cond_(&lock_
), signaled_(false) {
205 DCHECK(!manual_reset
);
206 DCHECK(!initially_signaled
);
211 base::AutoLock
scoped_lock(lock_
);
218 base::AutoLock
scoped_lock(lock_
);
226 base::ConditionVariable cond_
;
230 // This is meant to test the absolute minimal context switching time
231 // using our own base synchronization code.
232 typedef EventPerfTest
<ConditionVariableEvent
> ConditionVariablePerfTest
;
233 TEST_F(ConditionVariablePerfTest
, EventPingPong
) {
234 RunPingPongTest("4_ConditionVariable_Threads", 4);
237 #if defined(OS_POSIX)
239 // Absolutely 100% minimal posix waitable event. If there is a better/faster
240 // way to force a context switch, we should use that instead.
243 PthreadEvent(bool manual_reset
, bool initially_signaled
) {
244 DCHECK(!manual_reset
);
245 DCHECK(!initially_signaled
);
246 pthread_mutex_init(&mutex_
, 0);
247 pthread_cond_init(&cond_
, 0);
252 pthread_cond_destroy(&cond_
);
253 pthread_mutex_destroy(&mutex_
);
257 pthread_mutex_lock(&mutex_
);
259 pthread_mutex_unlock(&mutex_
);
260 pthread_cond_signal(&cond_
);
264 pthread_mutex_lock(&mutex_
);
266 pthread_cond_wait(&cond_
, &mutex_
);
268 pthread_mutex_unlock(&mutex_
);
273 pthread_mutex_t mutex_
;
274 pthread_cond_t cond_
;
277 // This is meant to test the absolute minimal context switching time.
278 // If there is any faster way to do this we should substitute it in.
279 typedef EventPerfTest
<PthreadEvent
> PthreadEventPerfTest
;
280 TEST_F(PthreadEventPerfTest
, EventPingPong
) {
281 RunPingPongTest("4_PthreadCondVar_Threads", 4);