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/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 private: | 116 private: |
117 base::WaitableEvent done_; | 117 base::WaitableEvent done_; |
118 }; | 118 }; |
119 | 119 |
120 // Class to test task performance by posting empty tasks back and forth. | 120 // Class to test task performance by posting empty tasks back and forth. |
121 class TaskPerfTest : public ThreadPerfTest { | 121 class TaskPerfTest : public ThreadPerfTest { |
122 base::Thread* NextThread(int count) { | 122 base::Thread* NextThread(int count) { |
123 return threads_[count % threads_.size()]; | 123 return threads_[count % threads_.size()]; |
124 } | 124 } |
125 | 125 |
126 virtual void PingPong(int hops) OVERRIDE { | 126 virtual void PingPong(int hops) override { |
127 if (!hops) { | 127 if (!hops) { |
128 FinishMeasurement(); | 128 FinishMeasurement(); |
129 return; | 129 return; |
130 } | 130 } |
131 NextThread(hops)->message_loop_proxy()->PostTask( | 131 NextThread(hops)->message_loop_proxy()->PostTask( |
132 FROM_HERE, | 132 FROM_HERE, |
133 base::Bind( | 133 base::Bind( |
134 &ThreadPerfTest::PingPong, base::Unretained(this), hops - 1)); | 134 &ThreadPerfTest::PingPong, base::Unretained(this), hops - 1)); |
135 } | 135 } |
136 }; | 136 }; |
137 | 137 |
138 // 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 |
139 // 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, |
140 // 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 |
141 // 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 |
142 // 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). |
143 TEST_F(TaskPerfTest, TaskPingPong) { | 143 TEST_F(TaskPerfTest, TaskPingPong) { |
144 RunPingPongTest("1_Task_Threads", 1); | 144 RunPingPongTest("1_Task_Threads", 1); |
145 RunPingPongTest("4_Task_Threads", 4); | 145 RunPingPongTest("4_Task_Threads", 4); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 // Same as above, but add observers to test their perf impact. | 149 // Same as above, but add observers to test their perf impact. |
150 class MessageLoopObserver : public base::MessageLoop::TaskObserver { | 150 class MessageLoopObserver : public base::MessageLoop::TaskObserver { |
151 public: | 151 public: |
152 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE { | 152 virtual void WillProcessTask(const base::PendingTask& pending_task) override { |
153 } | 153 } |
154 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE { | 154 virtual void DidProcessTask(const base::PendingTask& pending_task) override { |
155 } | 155 } |
156 }; | 156 }; |
157 MessageLoopObserver message_loop_observer; | 157 MessageLoopObserver message_loop_observer; |
158 | 158 |
159 class TaskObserverPerfTest : public TaskPerfTest { | 159 class TaskObserverPerfTest : public TaskPerfTest { |
160 public: | 160 public: |
161 virtual void Init() OVERRIDE { | 161 virtual void Init() override { |
162 TaskPerfTest::Init(); | 162 TaskPerfTest::Init(); |
163 for (size_t i = 0; i < threads_.size(); i++) { | 163 for (size_t i = 0; i < threads_.size(); i++) { |
164 threads_[i]->message_loop()->AddTaskObserver(&message_loop_observer); | 164 threads_[i]->message_loop()->AddTaskObserver(&message_loop_observer); |
165 } | 165 } |
166 } | 166 } |
167 }; | 167 }; |
168 | 168 |
169 TEST_F(TaskObserverPerfTest, TaskPingPong) { | 169 TEST_F(TaskObserverPerfTest, TaskPingPong) { |
170 RunPingPongTest("1_Task_Threads_With_Observer", 1); | 170 RunPingPongTest("1_Task_Threads_With_Observer", 1); |
171 RunPingPongTest("4_Task_Threads_With_Observer", 4); | 171 RunPingPongTest("4_Task_Threads_With_Observer", 4); |
172 } | 172 } |
173 | 173 |
174 // Class to test our WaitableEvent performance by signaling back and fort. | 174 // Class to test our WaitableEvent performance by signaling back and fort. |
175 // WaitableEvent is templated so we can also compare with other versions. | 175 // WaitableEvent is templated so we can also compare with other versions. |
176 template <typename WaitableEventType> | 176 template <typename WaitableEventType> |
177 class EventPerfTest : public ThreadPerfTest { | 177 class EventPerfTest : public ThreadPerfTest { |
178 public: | 178 public: |
179 virtual void Init() OVERRIDE { | 179 virtual void Init() override { |
180 for (size_t i = 0; i < threads_.size(); i++) | 180 for (size_t i = 0; i < threads_.size(); i++) |
181 events_.push_back(new WaitableEventType(false, false)); | 181 events_.push_back(new WaitableEventType(false, false)); |
182 } | 182 } |
183 | 183 |
184 virtual void Reset() OVERRIDE { events_.clear(); } | 184 virtual void Reset() override { events_.clear(); } |
185 | 185 |
186 void WaitAndSignalOnThread(size_t event) { | 186 void WaitAndSignalOnThread(size_t event) { |
187 size_t next_event = (event + 1) % events_.size(); | 187 size_t next_event = (event + 1) % events_.size(); |
188 int my_hops = 0; | 188 int my_hops = 0; |
189 do { | 189 do { |
190 events_[event]->Wait(); | 190 events_[event]->Wait(); |
191 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. | 191 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. |
192 events_[next_event]->Signal(); | 192 events_[next_event]->Signal(); |
193 } while (my_hops > 0); | 193 } while (my_hops > 0); |
194 // Once we are done, all threads will signal as hops passes zero. | 194 // Once we are done, all threads will signal as hops passes zero. |
195 // We only signal completion once, on the thread that reaches zero. | 195 // We only signal completion once, on the thread that reaches zero. |
196 if (!my_hops) | 196 if (!my_hops) |
197 FinishMeasurement(); | 197 FinishMeasurement(); |
198 } | 198 } |
199 | 199 |
200 virtual void PingPong(int hops) OVERRIDE { | 200 virtual void PingPong(int hops) override { |
201 remaining_hops_ = hops; | 201 remaining_hops_ = hops; |
202 for (size_t i = 0; i < threads_.size(); i++) { | 202 for (size_t i = 0; i < threads_.size(); i++) { |
203 threads_[i]->message_loop_proxy()->PostTask( | 203 threads_[i]->message_loop_proxy()->PostTask( |
204 FROM_HERE, | 204 FROM_HERE, |
205 base::Bind(&EventPerfTest::WaitAndSignalOnThread, | 205 base::Bind(&EventPerfTest::WaitAndSignalOnThread, |
206 base::Unretained(this), | 206 base::Unretained(this), |
207 i)); | 207 i)); |
208 } | 208 } |
209 | 209 |
210 // Kick off the Signal ping-ponging. | 210 // Kick off the Signal ping-ponging. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 305 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
306 TEST_F(PthreadEventPerfTest, EventPingPong) { | 306 TEST_F(PthreadEventPerfTest, EventPingPong) { |
307 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 307 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
308 } | 308 } |
309 | 309 |
310 #endif | 310 #endif |
311 | 311 |
312 } // namespace | 312 } // namespace |
313 | 313 |
314 } // namespace base | 314 } // namespace base |
OLD | NEW |