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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base_switches.h" | 10 #include "base/base_switches.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void TimeOnThread(base::ThreadTicks* ticks, base::WaitableEvent* done) { | 60 void TimeOnThread(base::ThreadTicks* ticks, base::WaitableEvent* done) { |
61 *ticks = base::ThreadTicks::Now(); | 61 *ticks = base::ThreadTicks::Now(); |
62 done->Signal(); | 62 done->Signal(); |
63 } | 63 } |
64 | 64 |
65 base::ThreadTicks ThreadNow(const base::Thread& thread) { | 65 base::ThreadTicks ThreadNow(const base::Thread& thread) { |
66 base::WaitableEvent done(WaitableEvent::ResetPolicy::AUTOMATIC, | 66 base::WaitableEvent done(WaitableEvent::ResetPolicy::AUTOMATIC, |
67 WaitableEvent::InitialState::NOT_SIGNALED); | 67 WaitableEvent::InitialState::NOT_SIGNALED); |
68 base::ThreadTicks ticks; | 68 base::ThreadTicks ticks; |
69 thread.task_runner()->PostTask( | 69 thread.task_runner()->PostTask( |
70 FROM_HERE, base::Bind(&ThreadPerfTest::TimeOnThread, | 70 FROM_HERE, base::BindOnce(&ThreadPerfTest::TimeOnThread, |
71 base::Unretained(this), &ticks, &done)); | 71 base::Unretained(this), &ticks, &done)); |
72 done.Wait(); | 72 done.Wait(); |
73 return ticks; | 73 return ticks; |
74 } | 74 } |
75 | 75 |
76 void RunPingPongTest(const std::string& name, unsigned num_threads) { | 76 void RunPingPongTest(const std::string& name, unsigned num_threads) { |
77 // Create threads and collect starting cpu-time for each thread. | 77 // Create threads and collect starting cpu-time for each thread. |
78 std::vector<base::ThreadTicks> thread_starts; | 78 std::vector<base::ThreadTicks> thread_starts; |
79 while (threads_.size() < num_threads) { | 79 while (threads_.size() < num_threads) { |
80 threads_.push_back(MakeUnique<base::Thread>("PingPonger")); | 80 threads_.push_back(MakeUnique<base::Thread>("PingPonger")); |
81 threads_.back()->Start(); | 81 threads_.back()->Start(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 base::Thread* NextThread(int count) { | 131 base::Thread* NextThread(int count) { |
132 return threads_[count % threads_.size()].get(); | 132 return threads_[count % threads_.size()].get(); |
133 } | 133 } |
134 | 134 |
135 void PingPong(int hops) override { | 135 void PingPong(int hops) override { |
136 if (!hops) { | 136 if (!hops) { |
137 FinishMeasurement(); | 137 FinishMeasurement(); |
138 return; | 138 return; |
139 } | 139 } |
140 NextThread(hops)->task_runner()->PostTask( | 140 NextThread(hops)->task_runner()->PostTask( |
141 FROM_HERE, base::Bind(&ThreadPerfTest::PingPong, base::Unretained(this), | 141 FROM_HERE, base::BindOnce(&ThreadPerfTest::PingPong, |
142 hops - 1)); | 142 base::Unretained(this), hops - 1)); |
143 } | 143 } |
144 }; | 144 }; |
145 | 145 |
146 // This tries to test the 'best-case' as well as the 'worst-case' task posting | 146 // This tries to test the 'best-case' as well as the 'worst-case' task posting |
147 // performance. The best-case keeps one thread alive such that it never yeilds, | 147 // performance. The best-case keeps one thread alive such that it never yeilds, |
148 // while the worse-case forces a context switch for every task. Four threads are | 148 // while the worse-case forces a context switch for every task. Four threads are |
149 // used to ensure the threads do yeild (with just two it might be possible for | 149 // used to ensure the threads do yeild (with just two it might be possible for |
150 // both threads to stay awake if they can signal each other fast enough). | 150 // both threads to stay awake if they can signal each other fast enough). |
151 TEST_F(TaskPerfTest, TaskPingPong) { | 151 TEST_F(TaskPerfTest, TaskPingPong) { |
152 RunPingPongTest("1_Task_Threads", 1); | 152 RunPingPongTest("1_Task_Threads", 1); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 // Once we are done, all threads will signal as hops passes zero. | 203 // Once we are done, all threads will signal as hops passes zero. |
204 // We only signal completion once, on the thread that reaches zero. | 204 // We only signal completion once, on the thread that reaches zero. |
205 if (!my_hops) | 205 if (!my_hops) |
206 FinishMeasurement(); | 206 FinishMeasurement(); |
207 } | 207 } |
208 | 208 |
209 void PingPong(int hops) override { | 209 void PingPong(int hops) override { |
210 remaining_hops_ = hops; | 210 remaining_hops_ = hops; |
211 for (size_t i = 0; i < threads_.size(); i++) { | 211 for (size_t i = 0; i < threads_.size(); i++) { |
212 threads_[i]->task_runner()->PostTask( | 212 threads_[i]->task_runner()->PostTask( |
213 FROM_HERE, base::Bind(&EventPerfTest::WaitAndSignalOnThread, | 213 FROM_HERE, base::BindOnce(&EventPerfTest::WaitAndSignalOnThread, |
214 base::Unretained(this), i)); | 214 base::Unretained(this), i)); |
215 } | 215 } |
216 | 216 |
217 // Kick off the Signal ping-ponging. | 217 // Kick off the Signal ping-ponging. |
218 events_.front()->Signal(); | 218 events_.front()->Signal(); |
219 } | 219 } |
220 | 220 |
221 int remaining_hops_; | 221 int remaining_hops_; |
222 std::vector<std::unique_ptr<WaitableEventType>> events_; | 222 std::vector<std::unique_ptr<WaitableEventType>> events_; |
223 }; | 223 }; |
224 | 224 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 314 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
315 TEST_F(PthreadEventPerfTest, EventPingPong) { | 315 TEST_F(PthreadEventPerfTest, EventPingPong) { |
316 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 316 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
317 } | 317 } |
318 | 318 |
319 #endif | 319 #endif |
320 | 320 |
321 } // namespace | 321 } // namespace |
322 | 322 |
323 } // namespace base | 323 } // namespace base |
OLD | NEW |