| 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 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 void WillProcessTask(const base::PendingTask& pending_task) override {} |
| 153 } | 153 void DidProcessTask(const base::PendingTask& pending_task) override {} |
| 154 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE { | |
| 155 } | |
| 156 }; | 154 }; |
| 157 MessageLoopObserver message_loop_observer; | 155 MessageLoopObserver message_loop_observer; |
| 158 | 156 |
| 159 class TaskObserverPerfTest : public TaskPerfTest { | 157 class TaskObserverPerfTest : public TaskPerfTest { |
| 160 public: | 158 public: |
| 161 virtual void Init() OVERRIDE { | 159 void Init() override { |
| 162 TaskPerfTest::Init(); | 160 TaskPerfTest::Init(); |
| 163 for (size_t i = 0; i < threads_.size(); i++) { | 161 for (size_t i = 0; i < threads_.size(); i++) { |
| 164 threads_[i]->message_loop()->AddTaskObserver(&message_loop_observer); | 162 threads_[i]->message_loop()->AddTaskObserver(&message_loop_observer); |
| 165 } | 163 } |
| 166 } | 164 } |
| 167 }; | 165 }; |
| 168 | 166 |
| 169 TEST_F(TaskObserverPerfTest, TaskPingPong) { | 167 TEST_F(TaskObserverPerfTest, TaskPingPong) { |
| 170 RunPingPongTest("1_Task_Threads_With_Observer", 1); | 168 RunPingPongTest("1_Task_Threads_With_Observer", 1); |
| 171 RunPingPongTest("4_Task_Threads_With_Observer", 4); | 169 RunPingPongTest("4_Task_Threads_With_Observer", 4); |
| 172 } | 170 } |
| 173 | 171 |
| 174 // Class to test our WaitableEvent performance by signaling back and fort. | 172 // Class to test our WaitableEvent performance by signaling back and fort. |
| 175 // WaitableEvent is templated so we can also compare with other versions. | 173 // WaitableEvent is templated so we can also compare with other versions. |
| 176 template <typename WaitableEventType> | 174 template <typename WaitableEventType> |
| 177 class EventPerfTest : public ThreadPerfTest { | 175 class EventPerfTest : public ThreadPerfTest { |
| 178 public: | 176 public: |
| 179 virtual void Init() OVERRIDE { | 177 void Init() override { |
| 180 for (size_t i = 0; i < threads_.size(); i++) | 178 for (size_t i = 0; i < threads_.size(); i++) |
| 181 events_.push_back(new WaitableEventType(false, false)); | 179 events_.push_back(new WaitableEventType(false, false)); |
| 182 } | 180 } |
| 183 | 181 |
| 184 virtual void Reset() OVERRIDE { events_.clear(); } | 182 void Reset() override { events_.clear(); } |
| 185 | 183 |
| 186 void WaitAndSignalOnThread(size_t event) { | 184 void WaitAndSignalOnThread(size_t event) { |
| 187 size_t next_event = (event + 1) % events_.size(); | 185 size_t next_event = (event + 1) % events_.size(); |
| 188 int my_hops = 0; | 186 int my_hops = 0; |
| 189 do { | 187 do { |
| 190 events_[event]->Wait(); | 188 events_[event]->Wait(); |
| 191 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. | 189 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. |
| 192 events_[next_event]->Signal(); | 190 events_[next_event]->Signal(); |
| 193 } while (my_hops > 0); | 191 } while (my_hops > 0); |
| 194 // 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. |
| 195 // We only signal completion once, on the thread that reaches zero. | 193 // We only signal completion once, on the thread that reaches zero. |
| 196 if (!my_hops) | 194 if (!my_hops) |
| 197 FinishMeasurement(); | 195 FinishMeasurement(); |
| 198 } | 196 } |
| 199 | 197 |
| 200 virtual void PingPong(int hops) OVERRIDE { | 198 void PingPong(int hops) override { |
| 201 remaining_hops_ = hops; | 199 remaining_hops_ = hops; |
| 202 for (size_t i = 0; i < threads_.size(); i++) { | 200 for (size_t i = 0; i < threads_.size(); i++) { |
| 203 threads_[i]->message_loop_proxy()->PostTask( | 201 threads_[i]->message_loop_proxy()->PostTask( |
| 204 FROM_HERE, | 202 FROM_HERE, |
| 205 base::Bind(&EventPerfTest::WaitAndSignalOnThread, | 203 base::Bind(&EventPerfTest::WaitAndSignalOnThread, |
| 206 base::Unretained(this), | 204 base::Unretained(this), |
| 207 i)); | 205 i)); |
| 208 } | 206 } |
| 209 | 207 |
| 210 // Kick off the Signal ping-ponging. | 208 // Kick off the Signal ping-ponging. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 303 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
| 306 TEST_F(PthreadEventPerfTest, EventPingPong) { | 304 TEST_F(PthreadEventPerfTest, EventPingPong) { |
| 307 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 305 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
| 308 } | 306 } |
| 309 | 307 |
| 310 #endif | 308 #endif |
| 311 | 309 |
| 312 } // namespace | 310 } // namespace |
| 313 | 311 |
| 314 } // namespace base | 312 } // namespace base |
| OLD | NEW |