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/child/task_queue_manager.h" | |
6 | |
7 #include "base/bind.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" | |
15 | |
16 namespace scheduler { | |
17 | |
18 class TaskQueueManagerPerfTest : public testing::Test { | |
19 public: | |
20 TaskQueueManagerPerfTest() | |
21 : num_queues_(0), | |
22 max_tasks_in_flight_(0), | |
23 num_tasks_in_flight_(0), | |
24 num_tasks_to_post_(0), | |
25 num_tasks_to_run_(0) {} | |
26 | |
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"))); | |
35 } | |
36 | |
37 void TestDelayedTask() { | |
38 if (--num_tasks_to_run_ == 0) { | |
39 message_loop_->Quit(); | |
40 } | |
41 | |
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; | |
54 i++) { | |
55 // Choose a queue weighted towards queue 0. | |
56 unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1); | |
57 if (queue == num_queues_) { | |
58 queue = 0; | |
59 } | |
60 // Simulate a mix of short and longer delays. | |
61 unsigned int delay = | |
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_++; | |
68 num_tasks_to_post_--; | |
69 } | |
70 } | |
71 | |
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; | |
76 TestDelayedTask(); | |
77 } | |
78 | |
79 void Benchmark(const std::string& trace, const base::Closure& test_task) { | |
80 base::ThreadTicks start = base::ThreadTicks::Now(); | |
81 base::ThreadTicks now; | |
82 unsigned long long num_iterations = 0; | |
83 do { | |
84 test_task.Run(); | |
85 message_loop_->Run(); | |
86 now = base::ThreadTicks::Now(); | |
87 num_iterations++; | |
88 } while (now - start < base::TimeDelta::FromSeconds(5)); | |
89 perf_test::PrintResult( | |
90 "task", "", trace, | |
91 (now - start).InMicroseconds() / static_cast<double>(num_iterations), | |
92 "us/run", true); | |
93 } | |
94 | |
95 size_t num_queues_; | |
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_; | |
103 }; | |
104 | |
105 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) { | |
106 if (!base::ThreadTicks::IsSupported()) | |
107 return; | |
108 Initialize(1u); | |
109 | |
110 max_tasks_in_flight_ = 200; | |
111 Benchmark("run 10000 delayed tasks with one queue", | |
112 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
113 base::Unretained(this), 10000)); | |
114 } | |
115 | |
116 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) { | |
117 if (!base::ThreadTicks::IsSupported()) | |
118 return; | |
119 Initialize(4u); | |
120 | |
121 max_tasks_in_flight_ = 200; | |
122 Benchmark("run 10000 delayed tasks with four queues", | |
123 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
124 base::Unretained(this), 10000)); | |
125 } | |
126 | |
127 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) { | |
128 if (!base::ThreadTicks::IsSupported()) | |
129 return; | |
130 Initialize(8u); | |
131 | |
132 max_tasks_in_flight_ = 200; | |
133 Benchmark("run 10000 delayed tasks with eight queues", | |
134 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask, | |
135 base::Unretained(this), 10000)); | |
136 } | |
137 | |
138 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs | |
139 // delayed tasks. | |
140 | |
141 } // namespace scheduler | |
OLD | NEW |