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