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"
8 #include "base/threading/thread.h"
9 #include "components/scheduler/child/scheduler_message_loop_delegate.h"
10 #include "components/scheduler/child/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 SetTaskQueueSelectorObserver(Observer
* observer
) override
{}
47 void AsValueInto(base::trace_event::TracedValue
* state
) const override
{}
50 std::vector
<const base::TaskQueue
*> work_queues_
;
52 DISALLOW_COPY_AND_ASSIGN(SelectorForTest
);
57 class TaskQueueManagerPerfTest
: public testing::Test
{
59 TaskQueueManagerPerfTest()
61 max_tasks_in_flight_(0),
62 num_tasks_in_flight_(0),
63 num_tasks_to_post_(0),
64 num_tasks_to_run_(0) {}
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", "fake.category.debug"));
75 void TestDelayedTask() {
76 if (--num_tasks_to_run_
== 0) {
77 message_loop_
->Quit();
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;
93 // Choose a queue weighted towards queue 0.
94 unsigned int queue
= num_tasks_to_post_
% (num_queues_
+ 1);
95 if (queue
== num_queues_
) {
98 // Simulate a mix of short and longer delays.
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_
--;
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
;
119 void Benchmark(const std::string
& trace
, const base::Closure
& test_task
) {
120 base::TimeTicks start
= base::TimeTicks::Now();
122 unsigned long long num_iterations
= 0;
125 message_loop_
->Run();
126 now
= base::TimeTicks::Now();
128 } while (now
- start
< base::TimeDelta::FromSeconds(5));
129 perf_test::PrintResult(
131 (now
- start
).InMicroseconds() / static_cast<double>(num_iterations
),
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_
;
145 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_OneQueue
) {
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));
154 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_FourQueues
) {
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));
163 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_EightQueues
) {
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));
172 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs
175 } // namespace scheduler