| 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/base/worker_pool.h" | |
| 6 | |
| 7 #include "base/time.h" | |
| 8 #include "cc/base/completion_event.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace cc { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 static const int kTimeLimitMillis = 2000; | |
| 16 static const int kWarmupRuns = 5; | |
| 17 static const int kTimeCheckInterval = 10; | |
| 18 | |
| 19 class PerfTaskImpl : public internal::WorkerPoolTask { | |
| 20 public: | |
| 21 explicit PerfTaskImpl(internal::WorkerPoolTask::TaskVector* dependencies) | |
| 22 : internal::WorkerPoolTask(dependencies) {} | |
| 23 | |
| 24 // Overridden from internal::WorkerPoolTask: | |
| 25 virtual void RunOnThread(unsigned thread_index) OVERRIDE {} | |
| 26 virtual void DispatchCompletionCallback() OVERRIDE {} | |
| 27 | |
| 28 private: | |
| 29 virtual ~PerfTaskImpl() {} | |
| 30 }; | |
| 31 | |
| 32 class PerfControlTaskImpl : public internal::WorkerPoolTask { | |
| 33 public: | |
| 34 explicit PerfControlTaskImpl( | |
| 35 internal::WorkerPoolTask::TaskVector* dependencies) | |
| 36 : internal::WorkerPoolTask(dependencies), | |
| 37 did_start_(new CompletionEvent), | |
| 38 can_finish_(new CompletionEvent) {} | |
| 39 | |
| 40 // Overridden from internal::WorkerPoolTask: | |
| 41 virtual void RunOnThread(unsigned thread_index) OVERRIDE { | |
| 42 did_start_->Signal(); | |
| 43 can_finish_->Wait(); | |
| 44 } | |
| 45 virtual void DispatchCompletionCallback() OVERRIDE {} | |
| 46 | |
| 47 void WaitForTaskToStartRunning() { | |
| 48 did_start_->Wait(); | |
| 49 } | |
| 50 | |
| 51 void AllowTaskToFinish() { | |
| 52 can_finish_->Signal(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 virtual ~PerfControlTaskImpl() {} | |
| 57 | |
| 58 scoped_ptr<CompletionEvent> did_start_; | |
| 59 scoped_ptr<CompletionEvent> can_finish_; | |
| 60 }; | |
| 61 | |
| 62 class PerfWorkerPool : public WorkerPool { | |
| 63 public: | |
| 64 PerfWorkerPool() : WorkerPool(1, "test") {} | |
| 65 virtual ~PerfWorkerPool() {} | |
| 66 | |
| 67 static scoped_ptr<PerfWorkerPool> Create() { | |
| 68 return make_scoped_ptr(new PerfWorkerPool); | |
| 69 } | |
| 70 | |
| 71 void BuildTaskGraph(internal::WorkerPoolTask* root) { | |
| 72 graph_.clear(); | |
| 73 WorkerPool::BuildTaskGraph(root, &graph_); | |
| 74 } | |
| 75 | |
| 76 void ScheduleTasks() { | |
| 77 SetTaskGraph(&graph_); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 TaskGraph graph_; | |
| 82 }; | |
| 83 | |
| 84 class WorkerPoolPerfTest : public testing::Test { | |
| 85 public: | |
| 86 WorkerPoolPerfTest() : num_runs_(0) {} | |
| 87 | |
| 88 // Overridden from testing::Test: | |
| 89 virtual void SetUp() OVERRIDE { | |
| 90 worker_pool_ = PerfWorkerPool::Create(); | |
| 91 } | |
| 92 virtual void TearDown() OVERRIDE { | |
| 93 worker_pool_->Shutdown(); | |
| 94 worker_pool_->CheckForCompletedTasks(); | |
| 95 } | |
| 96 | |
| 97 void EndTest() { | |
| 98 elapsed_ = base::TimeTicks::HighResNow() - start_time_; | |
| 99 } | |
| 100 | |
| 101 void AfterTest(const std::string test_name) { | |
| 102 // Format matches chrome/test/perf/perf_test.h:PrintResult | |
| 103 printf("*RESULT %s: %.2f runs/s\n", | |
| 104 test_name.c_str(), | |
| 105 num_runs_ / elapsed_.InSecondsF()); | |
| 106 } | |
| 107 | |
| 108 void CreateTasks(internal::WorkerPoolTask::TaskVector* dependencies, | |
| 109 unsigned current_depth, | |
| 110 unsigned max_depth, | |
| 111 unsigned num_children_per_node) { | |
| 112 internal::WorkerPoolTask::TaskVector children; | |
| 113 if (current_depth < max_depth) { | |
| 114 for (unsigned i = 0; i < num_children_per_node; ++i) { | |
| 115 CreateTasks(&children, | |
| 116 current_depth + 1, | |
| 117 max_depth, | |
| 118 num_children_per_node); | |
| 119 } | |
| 120 } else if (leaf_task_.get()) { | |
| 121 children.push_back(leaf_task_); | |
| 122 } | |
| 123 dependencies->push_back(make_scoped_refptr(new PerfTaskImpl(&children))); | |
| 124 } | |
| 125 | |
| 126 bool DidRun() { | |
| 127 ++num_runs_; | |
| 128 if (num_runs_ == kWarmupRuns) | |
| 129 start_time_ = base::TimeTicks::HighResNow(); | |
| 130 | |
| 131 if (!start_time_.is_null() && (num_runs_ % kTimeCheckInterval) == 0) { | |
| 132 base::TimeDelta elapsed = base::TimeTicks::HighResNow() - start_time_; | |
| 133 if (elapsed >= base::TimeDelta::FromMilliseconds(kTimeLimitMillis)) { | |
| 134 elapsed_ = elapsed; | |
| 135 return false; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 void RunBuildTaskGraphTest(const std::string test_name, | |
| 143 unsigned max_depth, | |
| 144 unsigned num_children_per_node) { | |
| 145 start_time_ = base::TimeTicks(); | |
| 146 num_runs_ = 0; | |
| 147 internal::WorkerPoolTask::TaskVector children; | |
| 148 CreateTasks(&children, 0, max_depth, num_children_per_node); | |
| 149 scoped_refptr<PerfTaskImpl> root_task( | |
| 150 make_scoped_refptr(new PerfTaskImpl(&children))); | |
| 151 do { | |
| 152 worker_pool_->BuildTaskGraph(root_task.get()); | |
| 153 } while (DidRun()); | |
| 154 | |
| 155 AfterTest(test_name); | |
| 156 } | |
| 157 | |
| 158 void RunScheduleTasksTest(const std::string test_name, | |
| 159 unsigned max_depth, | |
| 160 unsigned num_children_per_node) { | |
| 161 start_time_ = base::TimeTicks(); | |
| 162 num_runs_ = 0; | |
| 163 do { | |
| 164 internal::WorkerPoolTask::TaskVector empty; | |
| 165 leaf_task_ = make_scoped_refptr(new PerfControlTaskImpl(&empty)); | |
| 166 internal::WorkerPoolTask::TaskVector children; | |
| 167 CreateTasks(&children, 0, max_depth, num_children_per_node); | |
| 168 scoped_refptr<PerfTaskImpl> root_task( | |
| 169 make_scoped_refptr(new PerfTaskImpl(&children))); | |
| 170 | |
| 171 worker_pool_->BuildTaskGraph(root_task.get()); | |
| 172 worker_pool_->ScheduleTasks(); | |
| 173 leaf_task_->WaitForTaskToStartRunning(); | |
| 174 worker_pool_->BuildTaskGraph(NULL); | |
| 175 worker_pool_->ScheduleTasks(); | |
| 176 worker_pool_->CheckForCompletedTasks(); | |
| 177 leaf_task_->AllowTaskToFinish(); | |
| 178 } while (DidRun()); | |
| 179 | |
| 180 AfterTest(test_name); | |
| 181 } | |
| 182 | |
| 183 void RunExecuteTasksTest(const std::string test_name, | |
| 184 unsigned max_depth, | |
| 185 unsigned num_children_per_node) { | |
| 186 start_time_ = base::TimeTicks(); | |
| 187 num_runs_ = 0; | |
| 188 do { | |
| 189 internal::WorkerPoolTask::TaskVector children; | |
| 190 CreateTasks(&children, 0, max_depth, num_children_per_node); | |
| 191 scoped_refptr<PerfControlTaskImpl> root_task( | |
| 192 make_scoped_refptr(new PerfControlTaskImpl(&children))); | |
| 193 | |
| 194 worker_pool_->BuildTaskGraph(root_task.get()); | |
| 195 worker_pool_->ScheduleTasks(); | |
| 196 root_task->WaitForTaskToStartRunning(); | |
| 197 root_task->AllowTaskToFinish(); | |
| 198 worker_pool_->CheckForCompletedTasks(); | |
| 199 } while (DidRun()); | |
| 200 | |
| 201 AfterTest(test_name); | |
| 202 } | |
| 203 | |
| 204 protected: | |
| 205 scoped_ptr<PerfWorkerPool> worker_pool_; | |
| 206 scoped_refptr<PerfControlTaskImpl> leaf_task_; | |
| 207 base::TimeTicks start_time_; | |
| 208 base::TimeDelta elapsed_; | |
| 209 int num_runs_; | |
| 210 }; | |
| 211 | |
| 212 TEST_F(WorkerPoolPerfTest, BuildTaskGraph) { | |
| 213 RunBuildTaskGraphTest("build_task_graph_1_10", 1, 10); | |
| 214 RunBuildTaskGraphTest("build_task_graph_1_1000", 1, 1000); | |
| 215 RunBuildTaskGraphTest("build_task_graph_2_10", 2, 10); | |
| 216 RunBuildTaskGraphTest("build_task_graph_5_5", 5, 5); | |
| 217 RunBuildTaskGraphTest("build_task_graph_10_2", 10, 2); | |
| 218 RunBuildTaskGraphTest("build_task_graph_1000_1", 1000, 1); | |
| 219 RunBuildTaskGraphTest("build_task_graph_10_1", 10, 1); | |
| 220 } | |
| 221 | |
| 222 TEST_F(WorkerPoolPerfTest, ScheduleTasks) { | |
| 223 RunScheduleTasksTest("schedule_tasks_1_10", 1, 10); | |
| 224 RunScheduleTasksTest("schedule_tasks_1_1000", 1, 1000); | |
| 225 RunScheduleTasksTest("schedule_tasks_2_10", 2, 10); | |
| 226 RunScheduleTasksTest("schedule_tasks_5_5", 5, 5); | |
| 227 RunScheduleTasksTest("schedule_tasks_10_2", 10, 2); | |
| 228 RunScheduleTasksTest("schedule_tasks_1000_1", 1000, 1); | |
| 229 RunScheduleTasksTest("schedule_tasks_10_1", 10, 1); | |
| 230 } | |
| 231 | |
| 232 TEST_F(WorkerPoolPerfTest, ExecuteTasks) { | |
| 233 RunExecuteTasksTest("execute_tasks_1_10", 1, 10); | |
| 234 RunExecuteTasksTest("execute_tasks_1_1000", 1, 1000); | |
| 235 RunExecuteTasksTest("execute_tasks_2_10", 2, 10); | |
| 236 RunExecuteTasksTest("execute_tasks_5_5", 5, 5); | |
| 237 RunExecuteTasksTest("execute_tasks_10_2", 10, 2); | |
| 238 RunExecuteTasksTest("execute_tasks_1000_1", 1000, 1); | |
| 239 RunExecuteTasksTest("execute_tasks_10_1", 10, 1); | |
| 240 } | |
| 241 | |
| 242 } // namespace | |
| 243 | |
| 244 } // namespace cc | |
| OLD | NEW |