OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/scheduler/base/task_queue_manager.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/threading/thread.h" | |
13 #include "base/time/default_tick_clock.h" | |
14 #include "components/scheduler/base/task_queue_impl.h" | |
15 #include "components/scheduler/base/task_queue_manager_delegate_for_test.h" | |
16 #include "components/scheduler/base/task_queue_selector.h" | |
17 #include "components/scheduler/base/test_task_time_tracker.h" | |
18 #include "components/scheduler/base/work_queue_sets.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "testing/perf/perf_test.h" | |
21 | |
22 namespace scheduler { | |
23 | |
24 class TaskQueueManagerPerfTest : public testing::Test { | |
25 public: | |
26 TaskQueueManagerPerfTest() | |
27 : num_queues_(0), | |
28 max_tasks_in_flight_(0), | |
29 num_tasks_in_flight_(0), | |
30 num_tasks_to_post_(0), | |
31 num_tasks_to_run_(0) {} | |
32 | |
33 void SetUp() override { | |
34 if (base::ThreadTicks::IsSupported()) | |
35 base::ThreadTicks::WaitUntilInitialized(); | |
36 } | |
37 | |
38 void Initialize(size_t num_queues) { | |
39 num_queues_ = num_queues; | |
40 message_loop_.reset(new base::MessageLoop()); | |
41 run_loop_.reset(new base::RunLoop()); | |
42 manager_ = base::WrapUnique(new TaskQueueManager( | |
43 TaskQueueManagerDelegateForTest::Create( | |
44 message_loop_->task_runner(), | |
45 base::WrapUnique(new base::DefaultTickClock())), | |
46 "fake.category", "fake.category", "fake.category.debug")); | |
47 manager_->SetTaskTimeTracker(&test_task_time_tracker_); | |
48 for (size_t i = 0; i < num_queues; i++) | |
49 queues_.push_back(manager_->NewTaskQueue(TaskQueue::Spec("test"))); | |
50 } | |
51 | |
52 void TestDelayedTask() { | |
53 if (--num_tasks_to_run_ == 0) { | |
54 run_loop_->QuitWhenIdle(); | |
55 } | |
56 | |
57 num_tasks_in_flight_--; | |
58 // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at | |
59 // any one time. Thanks to the lower_num_tasks_to_post going to zero if | |
60 // there are a lot of tasks in flight, the total number of task in flight at | |
61 // any one time is very variable. | |
62 unsigned int lower_num_tasks_to_post = | |
63 num_tasks_in_flight_ < (max_tasks_in_flight_ / 2) ? 1 : 0; | |
64 unsigned int max_tasks_to_post = | |
65 num_tasks_to_post_ % 2 ? lower_num_tasks_to_post : 10; | |
66 for (unsigned int i = 0; | |
67 i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ && | |
68 num_tasks_to_post_ > 0; | |
69 i++) { | |
70 // Choose a queue weighted towards queue 0. | |
71 unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1); | |
72 if (queue == num_queues_) { | |
73 queue = 0; | |
74 } | |
75 // Simulate a mix of short and longer delays. | |
76 unsigned int delay = | |
77 num_tasks_to_post_ % 2 ? 1 : (10 + num_tasks_to_post_ % 10); | |
78 queues_[queue]->PostDelayedTask( | |
79 FROM_HERE, base::Bind(&TaskQueueManagerPerfTest::TestDelayedTask, | |
80 base::Unretained(this)), | |
81 base::TimeDelta::FromMicroseconds(delay)); | |
82 num_tasks_in_flight_++; | |
83 num_tasks_to_post_--; | |
84 } | |
85 } | |
86 | |
87 void ResetAndCallTestDelayedTask(unsigned int num_tasks_to_run) { | |
88 num_tasks_in_flight_ = 1; | |
89 num_tasks_to_post_ = num_tasks_to_run; | |
90 num_tasks_to_run_ = num_tasks_to_run; | |
91 TestDelayedTask(); | |
92 } | |
93 | |
94 void Benchmark(const std::string& trace, const base::Closure& test_task) { | |
95 base::ThreadTicks start = base::ThreadTicks::Now(); | |
96 base::ThreadTicks now; | |
97 unsigned long long num_iterations = 0; | |
98 do { | |
99 test_task.Run(); | |
100 run_loop_->Run(); | |
101 now = base::ThreadTicks::Now(); | |
102 num_iterations++; | |
103 } while (now - start < base::TimeDelta::FromSeconds(5)); | |
104 perf_test::PrintResult( | |
105 "task", "", trace, | |
106 (now - start).InMicroseconds() / static_cast<double>(num_iterations), | |
107 "us/run", true); | |
108 } | |
109 | |
110 size_t num_queues_; | |
111 unsigned int max_tasks_in_flight_; | |
112 unsigned int num_tasks_in_flight_; | |
113 unsigned int num_tasks_to_post_; | |
114 unsigned int num_tasks_to_run_; | |
115 std::unique_ptr<TaskQueueManager> manager_; | |
116 std::unique_ptr<base::MessageLoop> message_loop_; | |
117 std::unique_ptr<base::RunLoop> run_loop_; | |
118 std::vector<scoped_refptr<base::SingleThreadTaskRunner>> queues_; | |
119 // TODO(alexclarke): parameterize so we can measure with and without a | |
120 // TaskTimeTracker. | |
121 TestTaskTimeTracker test_task_time_tracker_; | |
122 }; | |
123 | |
124 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) { | |
125 if (!base::ThreadTicks::IsSupported()) | |
126 return; | |
127 Initialize(1u); | |
128 | |
129 max_tasks_in_flight_ = 200; | |
130 Benchmark("run 10000 delayed tasks with one queue", | |
131 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
132 base::Unretained(this), 10000)); | |
133 } | |
134 | |
135 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) { | |
136 if (!base::ThreadTicks::IsSupported()) | |
137 return; | |
138 Initialize(4u); | |
139 | |
140 max_tasks_in_flight_ = 200; | |
141 Benchmark("run 10000 delayed tasks with four queues", | |
142 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
143 base::Unretained(this), 10000)); | |
144 } | |
145 | |
146 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) { | |
147 if (!base::ThreadTicks::IsSupported()) | |
148 return; | |
149 Initialize(8u); | |
150 | |
151 max_tasks_in_flight_ = 200; | |
152 Benchmark("run 10000 delayed tasks with eight queues", | |
153 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
154 base::Unretained(this), 10000)); | |
155 } | |
156 | |
157 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_ThirtyTwoQueues) { | |
158 if (!base::ThreadTicks::IsSupported()) | |
159 return; | |
160 Initialize(32u); | |
161 | |
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)); | |
166 } | |
167 | |
168 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs | |
169 // delayed tasks. | |
170 | |
171 } // namespace scheduler | |
OLD | NEW |