Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / content / child / scheduler / task_queue_manager_perftest.cc
blob791e75fb9e119e54ba0daff286421d809eb89c2c
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 "content/child/scheduler/task_queue_manager.h"
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"
14 namespace content {
16 namespace {
18 class SelectorForTest : public TaskQueueSelector {
19 public:
20 SelectorForTest() {}
22 void RegisterWorkQueues(
23 const std::vector<const base::TaskQueue*>& work_queues) override {
24 work_queues_ = work_queues;
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;
41 CHECK(found_one);
42 return found_one;
45 void AsValueInto(base::trace_event::TracedValue* state) const override {}
47 private:
48 std::vector<const base::TaskQueue*> work_queues_;
50 DISALLOW_COPY_AND_ASSIGN(SelectorForTest);
53 } // namespace
55 class TaskQueueManagerPerfTest : public testing::Test {
56 public:
57 TaskQueueManagerPerfTest()
58 : num_queues_(0),
59 max_tasks_in_flight_(0),
60 num_tasks_in_flight_(0),
61 num_tasks_to_post_(0),
62 num_tasks_to_run_(0) {}
64 void Initialize(size_t num_queues) {
65 num_queues_ = num_queues;
66 message_loop_.reset(new base::MessageLoop());
67 selector_ = make_scoped_ptr(new SelectorForTest);
68 manager_ = make_scoped_ptr(new TaskQueueManager(
69 num_queues, SchedulerMessageLoopDelegate::Create(message_loop_.get()),
70 selector_.get(), "fake.category"));
73 void TestDelayedTask() {
74 if (--num_tasks_to_run_ == 0) {
75 message_loop_->Quit();
78 num_tasks_in_flight_--;
79 // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at
80 // any one time. Thanks to the lower_num_tasks_to_post going to zero if
81 // there are a lot of tasks in flight, the total number of task in flight at
82 // any one time is very variable.
83 unsigned int lower_num_tasks_to_post =
84 num_tasks_in_flight_ < (max_tasks_in_flight_ / 2) ? 1 : 0;
85 unsigned int max_tasks_to_post =
86 num_tasks_to_post_ % 2 ? lower_num_tasks_to_post : 10;
87 for (unsigned int i = 0;
88 i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ &&
89 num_tasks_to_post_ > 0;
90 i++) {
91 // Choose a queue weighted towards queue 0.
92 unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1);
93 if (queue == num_queues_) {
94 queue = 0;
96 // Simulate a mix of short and longer delays.
97 unsigned int delay =
98 num_tasks_to_post_ % 2 ? 1 : (10 + num_tasks_to_post_ % 10);
99 scoped_refptr<base::SingleThreadTaskRunner> runner =
100 manager_->TaskRunnerForQueue(queue);
101 runner->PostDelayedTask(
102 FROM_HERE, base::Bind(&TaskQueueManagerPerfTest::TestDelayedTask,
103 base::Unretained(this)),
104 base::TimeDelta::FromMicroseconds(delay));
105 num_tasks_in_flight_++;
106 num_tasks_to_post_--;
110 void ResetAndCallTestDelayedTask(unsigned int num_tasks_to_run) {
111 num_tasks_in_flight_ = 1;
112 num_tasks_to_post_ = num_tasks_to_run;
113 num_tasks_to_run_ = num_tasks_to_run;
114 TestDelayedTask();
117 void Benchmark(const std::string& trace, const base::Closure& test_task) {
118 base::TimeTicks start = base::TimeTicks::Now();
119 base::TimeTicks now;
120 unsigned long long num_iterations = 0;
121 do {
122 test_task.Run();
123 message_loop_->Run();
124 now = base::TimeTicks::Now();
125 num_iterations++;
126 } while (now - start < base::TimeDelta::FromSeconds(5));
127 perf_test::PrintResult(
128 "task", "", trace,
129 (now - start).InMicroseconds() / static_cast<double>(num_iterations),
130 "us/run", true);
133 size_t num_queues_;
134 unsigned int max_tasks_in_flight_;
135 unsigned int num_tasks_in_flight_;
136 unsigned int num_tasks_to_post_;
137 unsigned int num_tasks_to_run_;
138 scoped_ptr<SelectorForTest> selector_;
139 scoped_ptr<TaskQueueManager> manager_;
140 scoped_ptr<base::MessageLoop> message_loop_;
143 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) {
144 Initialize(1u);
146 max_tasks_in_flight_ = 200;
147 Benchmark("run 10000 delayed tasks with one queue",
148 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
149 base::Unretained(this), 10000));
152 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) {
153 Initialize(4u);
155 max_tasks_in_flight_ = 200;
156 Benchmark("run 10000 delayed tasks with four queues",
157 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
158 base::Unretained(this), 10000));
161 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) {
162 Initialize(8u);
164 max_tasks_in_flight_ = 200;
165 Benchmark("run 10000 delayed tasks with eight queues",
166 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
167 base::Unretained(this), 10000));
170 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs
171 // delayed tasks.
173 } // namespace content