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 "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const int kNumRuns = 100000; | 31 const int kNumRuns = 100000; |
32 | 32 |
33 // Base class for a threading perf-test. This sets up some threads for the | 33 // Base class for a threading perf-test. This sets up some threads for the |
34 // test and measures the clock-time in addition to time spent on each thread. | 34 // test and measures the clock-time in addition to time spent on each thread. |
35 class ThreadPerfTest : public testing::Test { | 35 class ThreadPerfTest : public testing::Test { |
36 public: | 36 public: |
37 ThreadPerfTest() | 37 ThreadPerfTest() |
38 : done_(false, false) { | 38 : done_(WaitableEvent::ResetPolicy::AUTOMATIC, |
39 WaitableEvent::InitialState::NOT_SIGNALED) { | |
39 // Disable the task profiler as it adds significant cost! | 40 // Disable the task profiler as it adds significant cost! |
40 CommandLine::Init(0, NULL); | 41 CommandLine::Init(0, NULL); |
41 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 42 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
42 switches::kProfilerTiming, | 43 switches::kProfilerTiming, |
43 switches::kProfilerTimingDisabledValue); | 44 switches::kProfilerTimingDisabledValue); |
44 } | 45 } |
45 | 46 |
46 // To be implemented by each test. Subclass must uses threads_ such that | 47 // To be implemented by each test. Subclass must uses threads_ such that |
47 // their cpu-time can be measured. Test must return from PingPong() _and_ | 48 // their cpu-time can be measured. Test must return from PingPong() _and_ |
48 // call FinishMeasurement from any thread to complete the test. | 49 // call FinishMeasurement from any thread to complete the test. |
49 virtual void Init() { | 50 virtual void Init() { |
50 if (ThreadTicks::IsSupported()) | 51 if (ThreadTicks::IsSupported()) |
51 ThreadTicks::WaitUntilInitialized(); | 52 ThreadTicks::WaitUntilInitialized(); |
52 } | 53 } |
53 virtual void PingPong(int hops) = 0; | 54 virtual void PingPong(int hops) = 0; |
54 virtual void Reset() {} | 55 virtual void Reset() {} |
55 | 56 |
56 void TimeOnThread(base::ThreadTicks* ticks, base::WaitableEvent* done) { | 57 void TimeOnThread(base::ThreadTicks* ticks, base::WaitableEvent* done) { |
57 *ticks = base::ThreadTicks::Now(); | 58 *ticks = base::ThreadTicks::Now(); |
58 done->Signal(); | 59 done->Signal(); |
59 } | 60 } |
60 | 61 |
61 base::ThreadTicks ThreadNow(base::Thread* thread) { | 62 base::ThreadTicks ThreadNow(base::Thread* thread) { |
62 base::WaitableEvent done(false, false); | 63 base::WaitableEvent done(WaitableEvent::ResetPolicy::AUTOMATIC, |
64 WaitableEvent::InitialState::NOT_SIGNALED); | |
63 base::ThreadTicks ticks; | 65 base::ThreadTicks ticks; |
64 thread->task_runner()->PostTask( | 66 thread->task_runner()->PostTask( |
65 FROM_HERE, base::Bind(&ThreadPerfTest::TimeOnThread, | 67 FROM_HERE, base::Bind(&ThreadPerfTest::TimeOnThread, |
66 base::Unretained(this), &ticks, &done)); | 68 base::Unretained(this), &ticks, &done)); |
67 done.Wait(); | 69 done.Wait(); |
68 return ticks; | 70 return ticks; |
69 } | 71 } |
70 | 72 |
71 void RunPingPongTest(const std::string& name, unsigned num_threads) { | 73 void RunPingPongTest(const std::string& name, unsigned num_threads) { |
72 // Create threads and collect starting cpu-time for each thread. | 74 // Create threads and collect starting cpu-time for each thread. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 RunPingPongTest("4_Task_Threads_With_Observer", 4); | 174 RunPingPongTest("4_Task_Threads_With_Observer", 4); |
173 } | 175 } |
174 | 176 |
175 // Class to test our WaitableEvent performance by signaling back and fort. | 177 // Class to test our WaitableEvent performance by signaling back and fort. |
176 // WaitableEvent is templated so we can also compare with other versions. | 178 // WaitableEvent is templated so we can also compare with other versions. |
177 template <typename WaitableEventType> | 179 template <typename WaitableEventType> |
178 class EventPerfTest : public ThreadPerfTest { | 180 class EventPerfTest : public ThreadPerfTest { |
179 public: | 181 public: |
180 void Init() override { | 182 void Init() override { |
181 for (size_t i = 0; i < threads_.size(); i++) | 183 for (size_t i = 0; i < threads_.size(); i++) |
182 events_.push_back(new WaitableEventType(false, false)); | 184 events_.push_back( |
185 new WaitableEventType(WaitableEvent::ResetPolicy::AUTOMATIC, | |
186 WaitableEvent::InitialState::NOT_SIGNALED)); | |
gab
2016/06/01 19:16:05
@etienneb: weird that clang-tidy made this change
etienneb
2016/06/01 19:53:27
You can use this to avoid template instantiation.
| |
183 } | 187 } |
184 | 188 |
185 void Reset() override { events_.clear(); } | 189 void Reset() override { events_.clear(); } |
186 | 190 |
187 void WaitAndSignalOnThread(size_t event) { | 191 void WaitAndSignalOnThread(size_t event) { |
188 size_t next_event = (event + 1) % events_.size(); | 192 size_t next_event = (event + 1) % events_.size(); |
189 int my_hops = 0; | 193 int my_hops = 0; |
190 do { | 194 do { |
191 events_[event]->Wait(); | 195 events_[event]->Wait(); |
192 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. | 196 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 308 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
305 TEST_F(PthreadEventPerfTest, EventPingPong) { | 309 TEST_F(PthreadEventPerfTest, EventPingPong) { |
306 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 310 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
307 } | 311 } |
308 | 312 |
309 #endif | 313 #endif |
310 | 314 |
311 } // namespace | 315 } // namespace |
312 | 316 |
313 } // namespace base | 317 } // namespace base |
OLD | NEW |