OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "content/renderer/scheduler/task_queue_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/threading/thread.h" |
| 9 #include "content/renderer/scheduler/task_queue_selector.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/perf/perf_test.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 namespace { |
| 16 |
| 17 class SelectorForTest : public TaskQueueSelector { |
| 18 public: |
| 19 SelectorForTest() {} |
| 20 |
| 21 void RegisterWorkQueues( |
| 22 const std::vector<const base::TaskQueue*>& work_queues) override { |
| 23 work_queues_ = work_queues; |
| 24 } |
| 25 |
| 26 bool SelectWorkQueueToService(size_t* out_queue_index) override { |
| 27 // Choose the oldest task, if any. |
| 28 bool found_one = false; |
| 29 for (size_t i = 0; i < work_queues_.size(); i++) { |
| 30 if (work_queues_[i]->empty()) |
| 31 continue; |
| 32 // Note: the < comparison is correct due to the fact that the PendingTask |
| 33 // operator inverts its comparison operation in order to work well in a |
| 34 // heap based priority queue. |
| 35 if (!found_one || |
| 36 work_queues_[*out_queue_index]->front() < work_queues_[i]->front()) |
| 37 *out_queue_index = i; |
| 38 found_one = true; |
| 39 } |
| 40 CHECK(found_one); |
| 41 return found_one; |
| 42 } |
| 43 |
| 44 void AsValueInto(base::trace_event::TracedValue* state) const override {} |
| 45 |
| 46 private: |
| 47 std::vector<const base::TaskQueue*> work_queues_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(SelectorForTest); |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class TaskQueueManagerPerfTest : public testing::Test { |
| 55 public: |
| 56 TaskQueueManagerPerfTest() |
| 57 : num_queues_(0), |
| 58 max_tasks_in_flight_(0), |
| 59 num_tasks_in_flight_(0), |
| 60 num_tasks_to_post_(0), |
| 61 num_tasks_to_run_(0) {} |
| 62 |
| 63 void Initialize(size_t num_queues) { |
| 64 num_queues_ = num_queues; |
| 65 message_loop_.reset(new base::MessageLoop()); |
| 66 selector_ = make_scoped_ptr(new SelectorForTest); |
| 67 manager_ = make_scoped_ptr(new TaskQueueManager( |
| 68 num_queues, message_loop_->task_runner(), selector_.get())); |
| 69 } |
| 70 |
| 71 void TestDelayedTask() { |
| 72 if (--num_tasks_to_run_ == 0) { |
| 73 message_loop_->Quit(); |
| 74 } |
| 75 |
| 76 num_tasks_in_flight_--; |
| 77 // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at |
| 78 // any one time. Thanks to the lower_num_tasks_to_post going to zero if |
| 79 // there are a lot of tasks in flight, the total number of task in flight at |
| 80 // any one time is very variable. |
| 81 unsigned int lower_num_tasks_to_post = |
| 82 num_tasks_in_flight_ < (max_tasks_in_flight_ / 2) ? 1 : 0; |
| 83 unsigned int max_tasks_to_post = |
| 84 num_tasks_to_post_ % 2 ? lower_num_tasks_to_post : 10; |
| 85 for (unsigned int i = 0; |
| 86 i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ && |
| 87 num_tasks_to_post_ > 0; |
| 88 i++) { |
| 89 // Choose a queue weighted towards queue 0. |
| 90 unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1); |
| 91 if (queue == num_queues_) { |
| 92 queue = 0; |
| 93 } |
| 94 // Simulate a mix of short and longer delays. |
| 95 unsigned int delay = |
| 96 num_tasks_to_post_ % 2 ? 1 : (10 + num_tasks_to_post_ % 10); |
| 97 scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 98 manager_->TaskRunnerForQueue(queue); |
| 99 runner->PostDelayedTask( |
| 100 FROM_HERE, base::Bind(&TaskQueueManagerPerfTest::TestDelayedTask, |
| 101 base::Unretained(this)), |
| 102 base::TimeDelta::FromMicroseconds(delay)); |
| 103 num_tasks_in_flight_++; |
| 104 num_tasks_to_post_--; |
| 105 } |
| 106 } |
| 107 |
| 108 void ResetAndCallTestDelayedTask(unsigned int num_tasks_to_run) { |
| 109 num_tasks_in_flight_ = 1; |
| 110 num_tasks_to_post_ = num_tasks_to_run; |
| 111 num_tasks_to_run_ = num_tasks_to_run; |
| 112 TestDelayedTask(); |
| 113 } |
| 114 |
| 115 void Benchmark(const std::string& trace, const base::Closure& test_task) { |
| 116 base::TimeTicks start = base::TimeTicks::Now(); |
| 117 base::TimeTicks now; |
| 118 unsigned long long num_iterations = 0; |
| 119 do { |
| 120 test_task.Run(); |
| 121 message_loop_->Run(); |
| 122 now = base::TimeTicks::Now(); |
| 123 num_iterations++; |
| 124 } while (now - start < base::TimeDelta::FromSeconds(5)); |
| 125 perf_test::PrintResult( |
| 126 "task", "", trace, |
| 127 (now - start).InMicroseconds() / static_cast<double>(num_iterations), |
| 128 "us/run", true); |
| 129 } |
| 130 |
| 131 size_t num_queues_; |
| 132 unsigned int max_tasks_in_flight_; |
| 133 unsigned int num_tasks_in_flight_; |
| 134 unsigned int num_tasks_to_post_; |
| 135 unsigned int num_tasks_to_run_; |
| 136 scoped_ptr<SelectorForTest> selector_; |
| 137 scoped_ptr<TaskQueueManager> manager_; |
| 138 scoped_ptr<base::MessageLoop> message_loop_; |
| 139 }; |
| 140 |
| 141 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) { |
| 142 Initialize(1u); |
| 143 |
| 144 max_tasks_in_flight_ = 200; |
| 145 Benchmark("run 10000 delayed tasks with one queue", |
| 146 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, |
| 147 base::Unretained(this), 10000)); |
| 148 } |
| 149 |
| 150 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) { |
| 151 Initialize(4u); |
| 152 |
| 153 max_tasks_in_flight_ = 200; |
| 154 Benchmark("run 10000 delayed tasks with four queues", |
| 155 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, |
| 156 base::Unretained(this), 10000)); |
| 157 } |
| 158 |
| 159 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) { |
| 160 Initialize(8u); |
| 161 |
| 162 max_tasks_in_flight_ = 200; |
| 163 Benchmark("run 10000 delayed tasks with eight queues", |
| 164 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, |
| 165 base::Unretained(this), 10000)); |
| 166 } |
| 167 |
| 168 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs |
| 169 // delayed tasks. |
| 170 |
| 171 } // namespace content |
OLD | NEW |