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 "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace cc { |
| 11 |
| 12 namespace { |
| 13 |
| 14 static const int kTimeLimitMillis = 1000; |
| 15 static const int kWarmupRuns = 5; |
| 16 static const int kTimeCheckInterval = 1000; |
| 17 |
| 18 class WorkerPoolPerfTest : public testing::Test { |
| 19 public: |
| 20 WorkerPoolPerfTest() : num_runs_(0) {} |
| 21 |
| 22 // Overridden from testing::Test: |
| 23 virtual void SetUp() OVERRIDE {} |
| 24 virtual void TearDown() OVERRIDE {} |
| 25 |
| 26 void EndTest() { |
| 27 elapsed_ = base::TimeTicks::HighResNow() - start_time_; |
| 28 } |
| 29 |
| 30 void AfterTest(const std::string test_name) { |
| 31 // Format matches chrome/test/perf/perf_test.h:PrintResult |
| 32 printf("*RESULT %s: %.2f runs/s\n", |
| 33 test_name.c_str(), |
| 34 num_runs_ / elapsed_.InSecondsF()); |
| 35 } |
| 36 |
| 37 void BuildGraph(internal::WorkerPoolTaskDependency* graph, |
| 38 unsigned current_depth, |
| 39 unsigned max_depth, |
| 40 unsigned num_children_per_node) { |
| 41 for (unsigned i = 0; i < num_children_per_node; ++i) { |
| 42 internal::WorkerPoolTaskDependency sub_graph; |
| 43 if (current_depth < max_depth) { |
| 44 BuildGraph(&sub_graph, |
| 45 current_depth + 1, |
| 46 max_depth, |
| 47 num_children_per_node); |
| 48 } |
| 49 internal::WorkerPoolTaskDependency::Create(NULL, graph, &sub_graph); |
| 50 } |
| 51 } |
| 52 |
| 53 void BuildGraph(unsigned max_depth, unsigned num_children_per_node) { |
| 54 internal::WorkerPoolTaskDependency graph; |
| 55 DCHECK(max_depth); |
| 56 BuildGraph(&graph, 1, max_depth, num_children_per_node); |
| 57 graph_.Swap(&graph); |
| 58 } |
| 59 |
| 60 bool DidRun() { |
| 61 ++num_runs_; |
| 62 if (num_runs_ == kWarmupRuns) |
| 63 start_time_ = base::TimeTicks::HighResNow(); |
| 64 |
| 65 if (!start_time_.is_null() && (num_runs_ % kTimeCheckInterval) == 0) { |
| 66 base::TimeDelta elapsed = base::TimeTicks::HighResNow() - start_time_; |
| 67 if (elapsed >= base::TimeDelta::FromMilliseconds(kTimeLimitMillis)) { |
| 68 elapsed_ = elapsed; |
| 69 return false; |
| 70 } |
| 71 } |
| 72 |
| 73 return true; |
| 74 } |
| 75 |
| 76 void RunGraphTraversalTest(const std::string test_name) { |
| 77 num_runs_ = 0; |
| 78 do { |
| 79 for (internal::WorkerPoolTaskGraph::Iterator it(&graph_); it; ++it); |
| 80 } while (DidRun()); |
| 81 |
| 82 AfterTest(test_name); |
| 83 } |
| 84 |
| 85 protected: |
| 86 internal::WorkerPoolTaskGraph graph_; |
| 87 base::TimeTicks start_time_; |
| 88 base::TimeDelta elapsed_; |
| 89 int num_runs_; |
| 90 }; |
| 91 |
| 92 TEST_F(WorkerPoolPerfTest, GraphTraversal) { |
| 93 BuildGraph(1, 10); |
| 94 RunGraphTraversalTest("graph_traversal_1_10"); |
| 95 BuildGraph(1, 1000); |
| 96 RunGraphTraversalTest("graph_traversal_1_1000"); |
| 97 BuildGraph(2, 10); |
| 98 RunGraphTraversalTest("graph_traversal_2_10"); |
| 99 BuildGraph(5, 5); |
| 100 RunGraphTraversalTest("graph_traversal_5_5"); |
| 101 BuildGraph(10, 2); |
| 102 RunGraphTraversalTest("graph_traversal_10_2"); |
| 103 BuildGraph(10, 1); |
| 104 RunGraphTraversalTest("graph_traversal_10_1"); |
| 105 BuildGraph(1000, 1); |
| 106 RunGraphTraversalTest("graph_traversal_1000_1"); |
| 107 } |
| 108 |
| 109 } // namespace |
| 110 |
| 111 } // namespace cc |
OLD | NEW |