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/renderer/scheduler/task_queue_manager.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"
17 class SelectorForTest
: public TaskQueueSelector
{
21 void RegisterWorkQueues(
22 const std::vector
<const base::TaskQueue
*>& work_queues
) override
{
23 work_queues_
= work_queues
;
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())
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.
36 work_queues_
[*out_queue_index
]->front() < work_queues_
[i
]->front())
44 void AsValueInto(base::trace_event::TracedValue
* state
) const override
{}
47 std::vector
<const base::TaskQueue
*> work_queues_
;
49 DISALLOW_COPY_AND_ASSIGN(SelectorForTest
);
54 class TaskQueueManagerPerfTest
: public testing::Test
{
56 TaskQueueManagerPerfTest()
58 max_tasks_in_flight_(0),
59 num_tasks_in_flight_(0),
60 num_tasks_to_post_(0),
61 num_tasks_to_run_(0) {}
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()));
71 void TestDelayedTask() {
72 if (--num_tasks_to_run_
== 0) {
73 message_loop_
->Quit();
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;
89 // Choose a queue weighted towards queue 0.
90 unsigned int queue
= num_tasks_to_post_
% (num_queues_
+ 1);
91 if (queue
== num_queues_
) {
94 // Simulate a mix of short and longer delays.
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_
--;
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
;
115 void Benchmark(const std::string
& trace
, const base::Closure
& test_task
) {
116 base::TimeTicks start
= base::TimeTicks::Now();
118 unsigned long long num_iterations
= 0;
121 message_loop_
->Run();
122 now
= base::TimeTicks::Now();
124 } while (now
- start
< base::TimeDelta::FromSeconds(5));
125 perf_test::PrintResult(
127 (now
- start
).InMicroseconds() / static_cast<double>(num_iterations
),
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_
;
141 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_OneQueue
) {
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));
150 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_FourQueues
) {
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));
159 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_EightQueues
) {
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));
168 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs
171 } // namespace content