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