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"
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"
18 class SelectorForTest
: public TaskQueueSelector
{
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())
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.
37 work_queues_
[*out_queue_index
]->front() < work_queues_
[i
]->front())
45 void AsValueInto(base::trace_event::TracedValue
* state
) const override
{}
48 std::vector
<const base::TaskQueue
*> work_queues_
;
50 DISALLOW_COPY_AND_ASSIGN(SelectorForTest
);
55 class TaskQueueManagerPerfTest
: public testing::Test
{
57 TaskQueueManagerPerfTest()
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;
91 // Choose a queue weighted towards queue 0.
92 unsigned int queue
= num_tasks_to_post_
% (num_queues_
+ 1);
93 if (queue
== num_queues_
) {
96 // Simulate a mix of short and longer delays.
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
;
117 void Benchmark(const std::string
& trace
, const base::Closure
& test_task
) {
118 base::TimeTicks start
= base::TimeTicks::Now();
120 unsigned long long num_iterations
= 0;
123 message_loop_
->Run();
124 now
= base::TimeTicks::Now();
126 } while (now
- start
< base::TimeDelta::FromSeconds(5));
127 perf_test::PrintResult(
129 (now
- start
).InMicroseconds() / static_cast<double>(num_iterations
),
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
) {
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
) {
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
) {
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
173 } // namespace content