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_task_runner_delegate_impl.h"
10 #include "components/scheduler/child/task_queue_impl.h"
11 #include "components/scheduler/child/task_queue_selector.h"
12 #include "components/scheduler/child/task_queue_sets.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/perf/perf_test.h"
18 class TaskQueueManagerPerfTest
: public testing::Test
{
20 TaskQueueManagerPerfTest()
22 max_tasks_in_flight_(0),
23 num_tasks_in_flight_(0),
24 num_tasks_to_post_(0),
25 num_tasks_to_run_(0) {}
27 void Initialize(size_t num_queues
) {
28 num_queues_
= num_queues
;
29 message_loop_
.reset(new base::MessageLoop());
30 manager_
= make_scoped_ptr(new TaskQueueManager(
31 SchedulerTaskRunnerDelegateImpl::Create(message_loop_
.get()),
32 "fake.category", "fake.category.debug"));
33 for (size_t i
= 0; i
< num_queues
; i
++)
34 queues_
.push_back(manager_
->NewTaskQueue(TaskQueue::Spec("test")));
37 void TestDelayedTask() {
38 if (--num_tasks_to_run_
== 0) {
39 message_loop_
->Quit();
42 num_tasks_in_flight_
--;
43 // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at
44 // any one time. Thanks to the lower_num_tasks_to_post going to zero if
45 // there are a lot of tasks in flight, the total number of task in flight at
46 // any one time is very variable.
47 unsigned int lower_num_tasks_to_post
=
48 num_tasks_in_flight_
< (max_tasks_in_flight_
/ 2) ? 1 : 0;
49 unsigned int max_tasks_to_post
=
50 num_tasks_to_post_
% 2 ? lower_num_tasks_to_post
: 10;
51 for (unsigned int i
= 0;
52 i
< max_tasks_to_post
&& num_tasks_in_flight_
< max_tasks_in_flight_
&&
53 num_tasks_to_post_
> 0;
55 // Choose a queue weighted towards queue 0.
56 unsigned int queue
= num_tasks_to_post_
% (num_queues_
+ 1);
57 if (queue
== num_queues_
) {
60 // Simulate a mix of short and longer delays.
62 num_tasks_to_post_
% 2 ? 1 : (10 + num_tasks_to_post_
% 10);
63 queues_
[queue
]->PostDelayedTask(
64 FROM_HERE
, base::Bind(&TaskQueueManagerPerfTest::TestDelayedTask
,
65 base::Unretained(this)),
66 base::TimeDelta::FromMicroseconds(delay
));
67 num_tasks_in_flight_
++;
72 void ResetAndCallTestDelayedTask(unsigned int num_tasks_to_run
) {
73 num_tasks_in_flight_
= 1;
74 num_tasks_to_post_
= num_tasks_to_run
;
75 num_tasks_to_run_
= num_tasks_to_run
;
79 void Benchmark(const std::string
& trace
, const base::Closure
& test_task
) {
80 base::TimeTicks start
= base::TimeTicks::Now();
82 unsigned long long num_iterations
= 0;
86 now
= base::TimeTicks::Now();
88 } while (now
- start
< base::TimeDelta::FromSeconds(5));
89 perf_test::PrintResult(
91 (now
- start
).InMicroseconds() / static_cast<double>(num_iterations
),
96 unsigned int max_tasks_in_flight_
;
97 unsigned int num_tasks_in_flight_
;
98 unsigned int num_tasks_to_post_
;
99 unsigned int num_tasks_to_run_
;
100 scoped_ptr
<TaskQueueManager
> manager_
;
101 scoped_ptr
<base::MessageLoop
> message_loop_
;
102 std::vector
<scoped_refptr
<base::SingleThreadTaskRunner
>> queues_
;
105 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_OneQueue
) {
108 max_tasks_in_flight_
= 200;
109 Benchmark("run 10000 delayed tasks with one queue",
110 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask
,
111 base::Unretained(this), 10000));
114 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_FourQueues
) {
117 max_tasks_in_flight_
= 200;
118 Benchmark("run 10000 delayed tasks with four queues",
119 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask
,
120 base::Unretained(this), 10000));
123 TEST_F(TaskQueueManagerPerfTest
, RunTenThousandDelayedTasks_EightQueues
) {
126 max_tasks_in_flight_
= 200;
127 Benchmark("run 10000 delayed tasks with eight queues",
128 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask
,
129 base::Unretained(this), 10000));
132 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs
135 } // namespace scheduler