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