OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cc/resources/worker_pool.h" |
| 6 |
| 7 #include "base/time/time.h" |
| 8 #include "cc/base/completion_event.h" |
| 9 #include "cc/test/lap_timer.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/perf/perf_test.h" |
| 12 |
| 13 namespace cc { |
| 14 |
| 15 namespace { |
| 16 |
| 17 static const int kTimeLimitMillis = 2000; |
| 18 static const int kWarmupRuns = 5; |
| 19 static const int kTimeCheckInterval = 10; |
| 20 |
| 21 class PerfWorkerPoolTaskImpl : public internal::WorkerPoolTask { |
| 22 public: |
| 23 // Overridden from internal::WorkerPoolTask: |
| 24 virtual void RunOnWorkerThread(unsigned thread_index) OVERRIDE {} |
| 25 virtual void CompleteOnOriginThread() OVERRIDE {} |
| 26 |
| 27 private: |
| 28 virtual ~PerfWorkerPoolTaskImpl() {} |
| 29 }; |
| 30 |
| 31 class PerfControlWorkerPoolTaskImpl : public internal::WorkerPoolTask { |
| 32 public: |
| 33 PerfControlWorkerPoolTaskImpl() : did_start_(new CompletionEvent), |
| 34 can_finish_(new CompletionEvent) {} |
| 35 |
| 36 // Overridden from internal::WorkerPoolTask: |
| 37 virtual void RunOnWorkerThread(unsigned thread_index) OVERRIDE { |
| 38 did_start_->Signal(); |
| 39 can_finish_->Wait(); |
| 40 } |
| 41 virtual void CompleteOnOriginThread() OVERRIDE {} |
| 42 |
| 43 void WaitForTaskToStartRunning() { |
| 44 did_start_->Wait(); |
| 45 } |
| 46 |
| 47 void AllowTaskToFinish() { |
| 48 can_finish_->Signal(); |
| 49 } |
| 50 |
| 51 private: |
| 52 virtual ~PerfControlWorkerPoolTaskImpl() {} |
| 53 |
| 54 scoped_ptr<CompletionEvent> did_start_; |
| 55 scoped_ptr<CompletionEvent> can_finish_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(PerfControlWorkerPoolTaskImpl); |
| 58 }; |
| 59 |
| 60 class PerfWorkerPool : public WorkerPool { |
| 61 public: |
| 62 PerfWorkerPool() : WorkerPool() {} |
| 63 virtual ~PerfWorkerPool() {} |
| 64 |
| 65 static scoped_ptr<PerfWorkerPool> Create() { |
| 66 return make_scoped_ptr(new PerfWorkerPool); |
| 67 } |
| 68 |
| 69 void ScheduleTasks(internal::WorkerPoolTask* root_task, |
| 70 internal::WorkerPoolTask* leaf_task, |
| 71 unsigned max_depth, |
| 72 unsigned num_children_per_node) { |
| 73 TaskVector tasks; |
| 74 TaskGraph graph; |
| 75 |
| 76 scoped_ptr<internal::GraphNode> root_node; |
| 77 if (root_task) |
| 78 root_node = make_scoped_ptr(new internal::GraphNode(root_task, 0u)); |
| 79 |
| 80 scoped_ptr<internal::GraphNode> leaf_node; |
| 81 if (leaf_task) |
| 82 leaf_node = make_scoped_ptr(new internal::GraphNode(leaf_task, 0u)); |
| 83 |
| 84 if (max_depth) { |
| 85 BuildTaskGraph(&tasks, |
| 86 &graph, |
| 87 root_node.get(), |
| 88 leaf_node.get(), |
| 89 0, |
| 90 max_depth, |
| 91 num_children_per_node); |
| 92 } |
| 93 |
| 94 if (leaf_node) |
| 95 graph.set(leaf_task, leaf_node.Pass()); |
| 96 |
| 97 if (root_node) |
| 98 graph.set(root_task, root_node.Pass()); |
| 99 |
| 100 SetTaskGraph(&graph); |
| 101 |
| 102 tasks_.swap(tasks); |
| 103 } |
| 104 |
| 105 void CheckForCompletedTasks() { |
| 106 CheckForCompletedWorkerTasks(); |
| 107 } |
| 108 |
| 109 private: |
| 110 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; |
| 111 |
| 112 void BuildTaskGraph(TaskVector* tasks, |
| 113 TaskGraph* graph, |
| 114 internal::GraphNode* dependent_node, |
| 115 internal::GraphNode* leaf_node, |
| 116 unsigned current_depth, |
| 117 unsigned max_depth, |
| 118 unsigned num_children_per_node) { |
| 119 scoped_refptr<PerfWorkerPoolTaskImpl> task(new PerfWorkerPoolTaskImpl); |
| 120 scoped_ptr<internal::GraphNode> node( |
| 121 new internal::GraphNode(task.get(), 0u)); |
| 122 |
| 123 if (current_depth < max_depth) { |
| 124 for (unsigned i = 0; i < num_children_per_node; ++i) { |
| 125 BuildTaskGraph(tasks, |
| 126 graph, |
| 127 node.get(), |
| 128 leaf_node, |
| 129 current_depth + 1, |
| 130 max_depth, |
| 131 num_children_per_node); |
| 132 } |
| 133 } else if (leaf_node) { |
| 134 leaf_node->add_dependent(node.get()); |
| 135 node->add_dependency(); |
| 136 } |
| 137 |
| 138 if (dependent_node) { |
| 139 node->add_dependent(dependent_node); |
| 140 dependent_node->add_dependency(); |
| 141 } |
| 142 graph->set(task.get(), node.Pass()); |
| 143 tasks->push_back(task.get()); |
| 144 } |
| 145 |
| 146 TaskVector tasks_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(PerfWorkerPool); |
| 149 }; |
| 150 |
| 151 class WorkerPoolPerfTest : public testing::Test { |
| 152 public: |
| 153 WorkerPoolPerfTest() |
| 154 : timer_(kWarmupRuns, |
| 155 base::TimeDelta::FromMilliseconds(kTimeLimitMillis), |
| 156 kTimeCheckInterval) {} |
| 157 |
| 158 // Overridden from testing::Test: |
| 159 virtual void SetUp() OVERRIDE { |
| 160 worker_pool_ = PerfWorkerPool::Create(); |
| 161 } |
| 162 virtual void TearDown() OVERRIDE { |
| 163 worker_pool_->Shutdown(); |
| 164 worker_pool_->CheckForCompletedTasks(); |
| 165 } |
| 166 |
| 167 void AfterTest(const std::string& test_name) { |
| 168 // Format matches chrome/test/perf/perf_test.h:PrintResult |
| 169 printf( |
| 170 "*RESULT %s: %.2f runs/s\n", test_name.c_str(), timer_.LapsPerSecond()); |
| 171 } |
| 172 |
| 173 void RunScheduleTasksTest(const std::string& test_name, |
| 174 unsigned max_depth, |
| 175 unsigned num_children_per_node) { |
| 176 timer_.Reset(); |
| 177 do { |
| 178 scoped_refptr<PerfControlWorkerPoolTaskImpl> leaf_task( |
| 179 new PerfControlWorkerPoolTaskImpl); |
| 180 worker_pool_->ScheduleTasks( |
| 181 NULL, leaf_task.get(), max_depth, num_children_per_node); |
| 182 leaf_task->WaitForTaskToStartRunning(); |
| 183 worker_pool_->ScheduleTasks(NULL, NULL, 0, 0); |
| 184 worker_pool_->CheckForCompletedTasks(); |
| 185 leaf_task->AllowTaskToFinish(); |
| 186 timer_.NextLap(); |
| 187 } while (!timer_.HasTimeLimitExpired()); |
| 188 |
| 189 perf_test::PrintResult("schedule_tasks", "", test_name, |
| 190 timer_.LapsPerSecond(), "runs/s", true); |
| 191 } |
| 192 |
| 193 void RunExecuteTasksTest(const std::string& test_name, |
| 194 unsigned max_depth, |
| 195 unsigned num_children_per_node) { |
| 196 timer_.Reset(); |
| 197 do { |
| 198 scoped_refptr<PerfControlWorkerPoolTaskImpl> root_task( |
| 199 new PerfControlWorkerPoolTaskImpl); |
| 200 worker_pool_->ScheduleTasks( |
| 201 root_task.get(), NULL, max_depth, num_children_per_node); |
| 202 root_task->WaitForTaskToStartRunning(); |
| 203 root_task->AllowTaskToFinish(); |
| 204 worker_pool_->CheckForCompletedTasks(); |
| 205 timer_.NextLap(); |
| 206 } while (!timer_.HasTimeLimitExpired()); |
| 207 |
| 208 perf_test::PrintResult("execute_tasks", "", test_name, |
| 209 timer_.LapsPerSecond(), "runs/s", true); |
| 210 } |
| 211 |
| 212 protected: |
| 213 scoped_ptr<PerfWorkerPool> worker_pool_; |
| 214 LapTimer timer_; |
| 215 }; |
| 216 |
| 217 TEST_F(WorkerPoolPerfTest, ScheduleTasks) { |
| 218 RunScheduleTasksTest("1_10", 1, 10); |
| 219 RunScheduleTasksTest("1_1000", 1, 1000); |
| 220 RunScheduleTasksTest("2_10", 2, 10); |
| 221 RunScheduleTasksTest("5_5", 5, 5); |
| 222 RunScheduleTasksTest("10_2", 10, 2); |
| 223 RunScheduleTasksTest("1000_1", 1000, 1); |
| 224 RunScheduleTasksTest("10_1", 10, 1); |
| 225 } |
| 226 |
| 227 TEST_F(WorkerPoolPerfTest, ExecuteTasks) { |
| 228 RunExecuteTasksTest("1_10", 1, 10); |
| 229 RunExecuteTasksTest("1_1000", 1, 1000); |
| 230 RunExecuteTasksTest("2_10", 2, 10); |
| 231 RunExecuteTasksTest("5_5", 5, 5); |
| 232 RunExecuteTasksTest("10_2", 10, 2); |
| 233 RunExecuteTasksTest("1000_1", 1000, 1); |
| 234 RunExecuteTasksTest("10_1", 10, 1); |
| 235 } |
| 236 |
| 237 } // namespace |
| 238 |
| 239 } // namespace cc |
OLD | NEW |