Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / scheduler / child / task_queue_manager_perftest.cc
blob909f33c07e38177339be54cee89a469fa5dfe525
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.
5 #include "components/scheduler/child/task_queue_manager.h"
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"
16 namespace scheduler {
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) {}
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")));
37 void TestDelayedTask() {
38 if (--num_tasks_to_run_ == 0) {
39 message_loop_->Quit();
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;
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_--;
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();
79 void Benchmark(const std::string& trace, const base::Closure& test_task) {
80 base::TimeTicks start = base::TimeTicks::Now();
81 base::TimeTicks now;
82 unsigned long long num_iterations = 0;
83 do {
84 test_task.Run();
85 message_loop_->Run();
86 now = base::TimeTicks::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);
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_;
105 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_OneQueue) {
106 Initialize(1u);
108 max_tasks_in_flight_ = 200;
109 Benchmark("run 10000 delayed tasks with one queue",
110 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
111 base::Unretained(this), 10000));
114 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_FourQueues) {
115 Initialize(4u);
117 max_tasks_in_flight_ = 200;
118 Benchmark("run 10000 delayed tasks with four queues",
119 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
120 base::Unretained(this), 10000));
123 TEST_F(TaskQueueManagerPerfTest, RunTenThousandDelayedTasks_EightQueues) {
124 Initialize(8u);
126 max_tasks_in_flight_ = 200;
127 Benchmark("run 10000 delayed tasks with eight queues",
128 base::Bind(&TaskQueueManagerPerfTest::ResetAndCallTestDelayedTask,
129 base::Unretained(this), 10000));
132 // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs
133 // delayed tasks.
135 } // namespace scheduler