| 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/task_graph_runner.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 namespace { | |
| 15 | |
| 16 static const int kTimeLimitMillis = 2000; | |
| 17 static const int kWarmupRuns = 5; | |
| 18 static const int kTimeCheckInterval = 10; | |
| 19 | |
| 20 class PerfTaskImpl : public internal::Task { | |
| 21 public: | |
| 22 PerfTaskImpl() {} | |
| 23 | |
| 24 // Overridden from internal::Task: | |
| 25 virtual void RunOnWorkerThread(unsigned thread_index) OVERRIDE {} | |
| 26 | |
| 27 private: | |
| 28 virtual ~PerfTaskImpl() {} | |
| 29 | |
| 30 DISALLOW_COPY_AND_ASSIGN(PerfTaskImpl); | |
| 31 }; | |
| 32 | |
| 33 class PerfControlTaskImpl : public internal::Task { | |
| 34 public: | |
| 35 PerfControlTaskImpl() {} | |
| 36 | |
| 37 // Overridden from internal::Task: | |
| 38 virtual void RunOnWorkerThread(unsigned thread_index) OVERRIDE { | |
| 39 did_start_.Signal(); | |
| 40 can_finish_.Wait(); | |
| 41 } | |
| 42 | |
| 43 void WaitForTaskToStartRunning() { | |
| 44 did_start_.Wait(); | |
| 45 } | |
| 46 | |
| 47 void AllowTaskToFinish() { | |
| 48 can_finish_.Signal(); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 virtual ~PerfControlTaskImpl() {} | |
| 53 | |
| 54 CompletionEvent did_start_; | |
| 55 CompletionEvent can_finish_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(PerfControlTaskImpl); | |
| 58 }; | |
| 59 | |
| 60 class TaskGraphRunnerPerfTest : public testing::Test { | |
| 61 public: | |
| 62 TaskGraphRunnerPerfTest() | |
| 63 : timer_(kWarmupRuns, | |
| 64 base::TimeDelta::FromMilliseconds(kTimeLimitMillis), | |
| 65 kTimeCheckInterval) { | |
| 66 } | |
| 67 | |
| 68 // Overridden from testing::Test: | |
| 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, | |
| 126 unsigned max_depth, | |
| 127 unsigned num_children_per_node) { | |
| 128 internal::Task::Vector tasks; | |
| 129 internal::GraphNode::Map graph; | |
| 130 | |
| 131 scoped_ptr<internal::GraphNode> root_node; | |
| 132 if (root_task) | |
| 133 root_node = make_scoped_ptr(new internal::GraphNode(root_task, 0u)); | |
| 134 | |
| 135 scoped_ptr<internal::GraphNode> leaf_node; | |
| 136 if (leaf_task) | |
| 137 leaf_node = make_scoped_ptr(new internal::GraphNode(leaf_task, 0u)); | |
| 138 | |
| 139 if (max_depth) { | |
| 140 BuildTaskGraph(&tasks, | |
| 141 &graph, | |
| 142 root_node.get(), | |
| 143 leaf_node.get(), | |
| 144 0, | |
| 145 max_depth, | |
| 146 num_children_per_node); | |
| 147 } | |
| 148 | |
| 149 if (leaf_node) | |
| 150 graph.set(leaf_task, leaf_node.Pass()); | |
| 151 | |
| 152 if (root_node) | |
| 153 graph.set(root_task, root_node.Pass()); | |
| 154 | |
| 155 task_graph_runner_->SetTaskGraph(namespace_token_, &graph); | |
| 156 | |
| 157 tasks_.swap(tasks); | |
| 158 } | |
| 159 | |
| 160 void BuildTaskGraph(internal::Task::Vector* tasks, | |
| 161 internal::GraphNode::Map* graph, | |
| 162 internal::GraphNode* dependent_node, | |
| 163 internal::GraphNode* leaf_node, | |
| 164 unsigned current_depth, | |
| 165 unsigned max_depth, | |
| 166 unsigned num_children_per_node) { | |
| 167 scoped_refptr<PerfTaskImpl> task(new PerfTaskImpl); | |
| 168 scoped_ptr<internal::GraphNode> node( | |
| 169 new internal::GraphNode(task.get(), 0u)); | |
| 170 | |
| 171 if (current_depth < max_depth) { | |
| 172 for (unsigned i = 0; i < num_children_per_node; ++i) { | |
| 173 BuildTaskGraph(tasks, | |
| 174 graph, | |
| 175 node.get(), | |
| 176 leaf_node, | |
| 177 current_depth + 1, | |
| 178 max_depth, | |
| 179 num_children_per_node); | |
| 180 } | |
| 181 } else if (leaf_node) { | |
| 182 leaf_node->add_dependent(node.get()); | |
| 183 node->add_dependency(); | |
| 184 } | |
| 185 | |
| 186 if (dependent_node) { | |
| 187 node->add_dependent(dependent_node); | |
| 188 dependent_node->add_dependency(); | |
| 189 } | |
| 190 graph->set(task.get(), node.Pass()); | |
| 191 tasks->push_back(task.get()); | |
| 192 } | |
| 193 | |
| 194 scoped_ptr<internal::TaskGraphRunner> task_graph_runner_; | |
| 195 internal::NamespaceToken namespace_token_; | |
| 196 internal::Task::Vector tasks_; | |
| 197 LapTimer timer_; | |
| 198 }; | |
| 199 | |
| 200 TEST_F(TaskGraphRunnerPerfTest, ScheduleTasks) { | |
| 201 RunScheduleTasksTest("1_10", 1, 10); | |
| 202 RunScheduleTasksTest("1_1000", 1, 1000); | |
| 203 RunScheduleTasksTest("2_10", 2, 10); | |
| 204 RunScheduleTasksTest("5_5", 5, 5); | |
| 205 RunScheduleTasksTest("10_2", 10, 2); | |
| 206 RunScheduleTasksTest("1000_1", 1000, 1); | |
| 207 RunScheduleTasksTest("10_1", 10, 1); | |
| 208 } | |
| 209 | |
| 210 TEST_F(TaskGraphRunnerPerfTest, ExecuteTasks) { | |
| 211 RunExecuteTasksTest("1_10", 1, 10); | |
| 212 RunExecuteTasksTest("1_1000", 1, 1000); | |
| 213 RunExecuteTasksTest("2_10", 2, 10); | |
| 214 RunExecuteTasksTest("5_5", 5, 5); | |
| 215 RunExecuteTasksTest("10_2", 10, 2); | |
| 216 RunExecuteTasksTest("1000_1", 1000, 1); | |
| 217 RunExecuteTasksTest("10_1", 10, 1); | |
| 218 } | |
| 219 | |
| 220 } // namespace | |
| 221 } // namespace cc | |
| OLD | NEW |