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.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"
19 class SelectorForTest
: public TaskQueueSelector
{
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())
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.
38 work_queues_
[*out_queue_index
]->front() < work_queues_
[i
]->front())
46 void SetTaskQueueSelectorObserver(Observer
* observer
) override
{}
48 void AsValueInto(base::trace_event::TracedValue
* state
) const override
{}
51 std::vector
<const base::TaskQueue
*> work_queues_
;
53 DISALLOW_COPY_AND_ASSIGN(SelectorForTest
);
58 class TaskQueueManagerPerfTest
: public testing::Test
{
60 TaskQueueManagerPerfTest()
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;
94 // Choose a queue weighted towards queue 0.
95 unsigned int queue
= num_tasks_to_post_
% (num_queues_
+ 1);
96 if (queue
== num_queues_
) {
99 // Simulate a mix of short and longer delays.
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
;
120 void Benchmark(const std::string
& trace
, const base::Closure
& test_task
) {
121 base::TimeTicks start
= base::TimeTicks::Now();
123 unsigned long long num_iterations
= 0;
126 message_loop_
->Run();
127 now
= base::TimeTicks::Now();
129 } while (now
- start
< base::TimeDelta::FromSeconds(5));
130 perf_test::PrintResult(
132 (now
- start
).InMicroseconds() / static_cast<double>(num_iterations
),
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
) {
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
) {
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
) {
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
176 } // namespace scheduler