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 protected: | |
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 TestTask() { | |
Sami
2015/03/04 13:00:09
nit: TestDelayedTask() since that's what this is m
alex clarke (OOO till 29th)
2015/03/04 16:34:20
Done.
| |
65 num_tasks_in_flight_--; | |
66 unsigned int max_tasks_to_post = num_tasks_to_run_ % 2 ? 1 : 10; | |
Sami
2015/03/04 13:00:09
How deep queues do you see in DoWork() with this a
alex clarke (OOO till 29th)
2015/03/04 16:34:20
The number of delayed tasks tasks is capped to max
Sami
2015/03/04 18:33:29
I more meant how many tasks there are pending in e
alex clarke (OOO till 29th)
2015/03/05 17:44:39
There is a maximum of 200 tasks in flight. It was
| |
67 for (unsigned int i = 0; | |
68 i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ && | |
69 num_tasks_to_run_ > 0; | |
70 i++) { | |
71 // Choose a queue weighted towards queue 0. | |
72 unsigned int queue = num_tasks_to_run_ % (num_queues_ + 1); | |
73 if (queue == num_queues_) { | |
74 queue = 0; | |
75 } | |
76 // Simulate a mix of short and longer delays. | |
77 unsigned int delay = | |
78 num_tasks_to_run_ % 2 ? 1 : (10 + num_tasks_to_run_ % 10); | |
79 scoped_refptr<base::SingleThreadTaskRunner> runner = | |
80 manager_->TaskRunnerForQueue(queue); | |
81 runner->PostDelayedTask(FROM_HERE, | |
82 base::Bind(&TaskQueueManagerPerfTest::TestTask, | |
83 base::Unretained(this)), | |
84 base::TimeDelta::FromMicroseconds(delay)); | |
85 num_tasks_in_flight_++; | |
86 num_tasks_to_run_--; | |
87 } | |
88 | |
89 if (num_tasks_to_run_ == 0) | |
90 message_loop_->Quit(); | |
91 } | |
92 | |
93 void Benchmark(const std::string& trace, unsigned int num_tasks_to_run) { | |
Sami
2015/03/04 13:00:09
I guess you could make this a template if you don'
alex clarke (OOO till 29th)
2015/03/04 16:34:20
Good point, but I think base::Closure is more read
Sami
2015/03/04 18:33:29
Works for me.
| |
94 base::TimeTicks start = base::TimeTicks::Now(); | |
95 base::TimeTicks now; | |
96 unsigned long long num_iterations = 0; | |
97 do { | |
98 num_tasks_in_flight_ = 1; | |
99 num_tasks_to_run_ = num_tasks_to_run; | |
100 TestTask(); | |
101 message_loop_->Run(); | |
102 now = base::TimeTicks::Now(); | |
103 num_iterations++; | |
104 } while (now - start < base::TimeDelta::FromSeconds(5)); | |
105 perf_test::PrintResult( | |
106 "task", "", trace, | |
107 (now - start).InMicroseconds() / static_cast<double>(num_iterations), | |
108 "us/run", true); | |
109 } | |
110 | |
111 size_t num_queues_; | |
112 unsigned int max_tasks_in_flight_; | |
113 unsigned int num_tasks_in_flight_; | |
114 unsigned int num_tasks_to_run_; | |
115 scoped_ptr<SelectorForTest> selector_; | |
116 scoped_ptr<TaskQueueManager> manager_; | |
117 scoped_ptr<base::MessageLoop> message_loop_; | |
118 }; | |
119 | |
120 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) { | |
121 Initialize(1u); | |
122 | |
123 max_tasks_in_flight_ = 200; | |
124 Benchmark("post 10000 delayed tasks with one queue", 10000); | |
125 } | |
126 | |
127 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) { | |
128 Initialize(4u); | |
129 | |
130 max_tasks_in_flight_ = 200; | |
131 Benchmark("post 10000 delayed tasks with four queues", 10000); | |
132 } | |
133 | |
134 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) { | |
135 Initialize(8u); | |
136 | |
137 max_tasks_in_flight_ = 200; | |
138 Benchmark("post 10000 delayed tasks with eight queues", 10000); | |
139 } | |
140 | |
141 } // namespace content | |
OLD | NEW |