OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/base_switches.h" | 5 #include "base/base_switches.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | |
9 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
12 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
13 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
14 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
15 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
16 #include "base/time/time.h" | 14 #include "base/time/time.h" |
17 #include "build/build_config.h" | 15 #include "build/build_config.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/perf/perf_test.h" | 17 #include "testing/perf/perf_test.h" |
20 | 18 |
(...skipping 28 matching lines...) Expand all Loading... |
49 virtual void Reset() {} | 47 virtual void Reset() {} |
50 | 48 |
51 void TimeOnThread(base::TimeTicks* ticks, base::WaitableEvent* done) { | 49 void TimeOnThread(base::TimeTicks* ticks, base::WaitableEvent* done) { |
52 *ticks = base::TimeTicks::ThreadNow(); | 50 *ticks = base::TimeTicks::ThreadNow(); |
53 done->Signal(); | 51 done->Signal(); |
54 } | 52 } |
55 | 53 |
56 base::TimeTicks ThreadNow(base::Thread* thread) { | 54 base::TimeTicks ThreadNow(base::Thread* thread) { |
57 base::WaitableEvent done(false, false); | 55 base::WaitableEvent done(false, false); |
58 base::TimeTicks ticks; | 56 base::TimeTicks ticks; |
59 thread->task_runner()->PostTask( | 57 thread->message_loop_proxy()->PostTask( |
60 FROM_HERE, base::Bind(&ThreadPerfTest::TimeOnThread, | 58 FROM_HERE, |
61 base::Unretained(this), &ticks, &done)); | 59 base::Bind(&ThreadPerfTest::TimeOnThread, |
| 60 base::Unretained(this), |
| 61 &ticks, |
| 62 &done)); |
62 done.Wait(); | 63 done.Wait(); |
63 return ticks; | 64 return ticks; |
64 } | 65 } |
65 | 66 |
66 void RunPingPongTest(const std::string& name, unsigned num_threads) { | 67 void RunPingPongTest(const std::string& name, unsigned num_threads) { |
67 // Create threads and collect starting cpu-time for each thread. | 68 // Create threads and collect starting cpu-time for each thread. |
68 std::vector<base::TimeTicks> thread_starts; | 69 std::vector<base::TimeTicks> thread_starts; |
69 while (threads_.size() < num_threads) { | 70 while (threads_.size() < num_threads) { |
70 threads_.push_back(new base::Thread("PingPonger")); | 71 threads_.push_back(new base::Thread("PingPonger")); |
71 threads_.back()->Start(); | 72 threads_.back()->Start(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 class TaskPerfTest : public ThreadPerfTest { | 121 class TaskPerfTest : public ThreadPerfTest { |
121 base::Thread* NextThread(int count) { | 122 base::Thread* NextThread(int count) { |
122 return threads_[count % threads_.size()]; | 123 return threads_[count % threads_.size()]; |
123 } | 124 } |
124 | 125 |
125 void PingPong(int hops) override { | 126 void PingPong(int hops) override { |
126 if (!hops) { | 127 if (!hops) { |
127 FinishMeasurement(); | 128 FinishMeasurement(); |
128 return; | 129 return; |
129 } | 130 } |
130 NextThread(hops)->task_runner()->PostTask( | 131 NextThread(hops)->message_loop_proxy()->PostTask( |
131 FROM_HERE, base::Bind(&ThreadPerfTest::PingPong, base::Unretained(this), | 132 FROM_HERE, |
132 hops - 1)); | 133 base::Bind( |
| 134 &ThreadPerfTest::PingPong, base::Unretained(this), hops - 1)); |
133 } | 135 } |
134 }; | 136 }; |
135 | 137 |
136 // This tries to test the 'best-case' as well as the 'worst-case' task posting | 138 // This tries to test the 'best-case' as well as the 'worst-case' task posting |
137 // performance. The best-case keeps one thread alive such that it never yeilds, | 139 // performance. The best-case keeps one thread alive such that it never yeilds, |
138 // while the worse-case forces a context switch for every task. Four threads are | 140 // while the worse-case forces a context switch for every task. Four threads are |
139 // used to ensure the threads do yeild (with just two it might be possible for | 141 // used to ensure the threads do yeild (with just two it might be possible for |
140 // both threads to stay awake if they can signal each other fast enough). | 142 // both threads to stay awake if they can signal each other fast enough). |
141 TEST_F(TaskPerfTest, TaskPingPong) { | 143 TEST_F(TaskPerfTest, TaskPingPong) { |
142 RunPingPongTest("1_Task_Threads", 1); | 144 RunPingPongTest("1_Task_Threads", 1); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } while (my_hops > 0); | 191 } while (my_hops > 0); |
190 // Once we are done, all threads will signal as hops passes zero. | 192 // Once we are done, all threads will signal as hops passes zero. |
191 // We only signal completion once, on the thread that reaches zero. | 193 // We only signal completion once, on the thread that reaches zero. |
192 if (!my_hops) | 194 if (!my_hops) |
193 FinishMeasurement(); | 195 FinishMeasurement(); |
194 } | 196 } |
195 | 197 |
196 void PingPong(int hops) override { | 198 void PingPong(int hops) override { |
197 remaining_hops_ = hops; | 199 remaining_hops_ = hops; |
198 for (size_t i = 0; i < threads_.size(); i++) { | 200 for (size_t i = 0; i < threads_.size(); i++) { |
199 threads_[i]->task_runner()->PostTask( | 201 threads_[i]->message_loop_proxy()->PostTask( |
200 FROM_HERE, base::Bind(&EventPerfTest::WaitAndSignalOnThread, | 202 FROM_HERE, |
201 base::Unretained(this), i)); | 203 base::Bind(&EventPerfTest::WaitAndSignalOnThread, |
| 204 base::Unretained(this), |
| 205 i)); |
202 } | 206 } |
203 | 207 |
204 // Kick off the Signal ping-ponging. | 208 // Kick off the Signal ping-ponging. |
205 events_.front()->Signal(); | 209 events_.front()->Signal(); |
206 } | 210 } |
207 | 211 |
208 int remaining_hops_; | 212 int remaining_hops_; |
209 ScopedVector<WaitableEventType> events_; | 213 ScopedVector<WaitableEventType> events_; |
210 }; | 214 }; |
211 | 215 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 303 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
300 TEST_F(PthreadEventPerfTest, EventPingPong) { | 304 TEST_F(PthreadEventPerfTest, EventPingPong) { |
301 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 305 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
302 } | 306 } |
303 | 307 |
304 #endif | 308 #endif |
305 | 309 |
306 } // namespace | 310 } // namespace |
307 | 311 |
308 } // namespace base | 312 } // namespace base |
OLD | NEW |