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 "content/renderer/scheduler/task_queue_manager.h" | |
6 | |
7 #include "base/bind.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" | |
12 | |
13 namespace content { | |
14 | |
15 namespace { | |
16 | |
17 class SelectorForTest : public TaskQueueSelector { | |
18 public: | |
19 SelectorForTest() {} | |
20 | |
21 void RegisterWorkQueues( | |
22 const std::vector<const base::TaskQueue*>& work_queues) override { | |
23 work_queues_ = work_queues; | |
24 } | |
25 | |
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()) | |
31 continue; | |
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. | |
35 if (!found_one || | |
36 work_queues_[*out_queue_index]->front() < work_queues_[i]->front()) | |
37 *out_queue_index = i; | |
38 found_one = true; | |
39 } | |
40 CHECK(found_one); | |
41 return found_one; | |
42 } | |
43 | |
44 void AsValueInto(base::trace_event::TracedValue* state) const override {} | |
45 | |
46 private: | |
47 std::vector<const base::TaskQueue*> work_queues_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(SelectorForTest); | |
50 }; | |
51 | |
52 } // namespace | |
53 | |
54 class TaskQueueManagerPerfTest : public testing::Test { | |
55 public: | |
56 void Initialize(size_t num_queues) { | |
57 num_queues_ = num_queues; | |
58 message_loop_.reset(new base::MessageLoop()); | |
59 selector_ = make_scoped_ptr(new SelectorForTest); | |
60 manager_ = make_scoped_ptr(new TaskQueueManager( | |
61 num_queues, message_loop_->task_runner(), selector_.get())); | |
62 } | |
63 | |
64 void TestDelayedTask() { | |
65 if (--num_tasks_to_run_ == 0) { | |
66 message_loop_->Quit(); | |
67 } | |
68 | |
69 num_tasks_in_flight_--; | |
70 // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at | |
71 // any one time. Thanks to the lower_num_tasks_to_post going to zero if | |
72 // there are a lot of tasks in flight, the total number of task in flight at | |
73 // any one time is very variable. | |
74 unsigned int lower_num_tasks_to_post = | |
75 num_tasks_in_flight_ < (max_tasks_in_flight_ / 2) ? 1 : 0; | |
76 unsigned int max_tasks_to_post = | |
77 num_tasks_to_post_ % 2 ? lower_num_tasks_to_post : 10; | |
78 for (unsigned int i = 0; | |
79 i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ && | |
80 num_tasks_to_post_ > 0; | |
81 i++) { | |
82 // Choose a queue weighted towards queue 0. | |
83 unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1); | |
84 if (queue == num_queues_) { | |
85 queue = 0; | |
86 } | |
87 // Simulate a mix of short and longer delays. | |
88 unsigned int delay = | |
89 num_tasks_to_post_ % 2 ? 1 : (10 + num_tasks_to_post_ % 10); | |
rmcilroy
2015/03/06 09:52:30
Drive by comment (sorry if this has been covered a
alex clarke (OOO till 29th)
2015/03/06 11:07:30
Yes it does appear to be strongly influenced by th
rmcilroy
2015/03/06 11:25:17
Great, sounds good - thanks!
| |
90 scoped_refptr<base::SingleThreadTaskRunner> runner = | |
91 manager_->TaskRunnerForQueue(queue); | |
92 runner->PostDelayedTask( | |
93 FROM_HERE, base::Bind(&TaskQueueManagerPerfTest::TestDelayedTask, | |
94 base::Unretained(this)), | |
95 base::TimeDelta::FromMicroseconds(delay)); | |
96 num_tasks_in_flight_++; | |
97 num_tasks_to_post_--; | |
98 } | |
99 } | |
100 | |
101 void ResetAndCallTestDelayedTask(unsigned int num_tasks_to_run) { | |
102 num_tasks_in_flight_ = 1; | |
103 num_tasks_to_post_ = num_tasks_to_run; | |
104 num_tasks_to_run_ = num_tasks_to_run; | |
105 TestDelayedTask(); | |
106 } | |
107 | |
108 void Benchmark(const std::string& trace, const base::Closure& test_task) { | |
109 base::TimeTicks start = base::TimeTicks::Now(); | |
110 base::TimeTicks now; | |
111 unsigned long long num_iterations = 0; | |
112 do { | |
113 test_task.Run(); | |
114 message_loop_->Run(); | |
115 now = base::TimeTicks::Now(); | |
116 num_iterations++; | |
117 } while (now - start < base::TimeDelta::FromSeconds(5)); | |
118 perf_test::PrintResult( | |
119 "task", "", trace, | |
120 (now - start).InMicroseconds() / static_cast<double>(num_iterations), | |
121 "us/run", true); | |
122 } | |
123 | |
124 size_t num_queues_; | |
125 unsigned int max_tasks_in_flight_; | |
Sami
2015/03/05 19:26:10
Could you initialize these to zeros in the constru
alex clarke (OOO till 29th)
2015/03/06 11:07:30
Done.
| |
126 unsigned int num_tasks_in_flight_; | |
127 unsigned int num_tasks_to_post_; | |
128 unsigned int num_tasks_to_run_; | |
129 scoped_ptr<SelectorForTest> selector_; | |
130 scoped_ptr<TaskQueueManager> manager_; | |
131 scoped_ptr<base::MessageLoop> message_loop_; | |
132 }; | |
133 | |
134 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) { | |
135 Initialize(1u); | |
136 | |
137 max_tasks_in_flight_ = 200; | |
138 Benchmark("run 10000 delayed tasks with one queue", | |
139 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
140 base::Unretained(this), 10000)); | |
141 } | |
142 | |
143 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) { | |
144 Initialize(4u); | |
145 | |
146 max_tasks_in_flight_ = 200; | |
147 Benchmark("run 10000 delayed tasks with four queues", | |
148 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
149 base::Unretained(this), 10000)); | |
150 } | |
151 | |
152 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) { | |
153 Initialize(8u); | |
154 | |
155 max_tasks_in_flight_ = 200; | |
156 Benchmark("run 10000 delayed tasks with eight queues", | |
157 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
158 base::Unretained(this), 10000)); | |
159 } | |
160 | |
161 } // namespace content | |
OLD | NEW |