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