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