jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 1 | // Copyright 2014 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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 8 | #include "base/bind.h" |
Peter Kasting | 383640f5 | 2018-02-13 06:22:40 | [diff] [blame] | 9 | #include "base/bind_helpers.h" |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 10 | #include "base/format_macros.h" |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
Carlos Caballero | 14712af9 | 2019-04-17 16:13:01 | [diff] [blame] | 13 | #include "base/message_loop/message_loop_current.h" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 14 | #include "base/message_loop/message_pump_type.h" |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
| 17 | #include "base/synchronization/condition_variable.h" |
| 18 | #include "base/synchronization/lock.h" |
| 19 | #include "base/synchronization/waitable_event.h" |
Carlos Caballero | 14712af9 | 2019-04-17 16:13:01 | [diff] [blame] | 20 | #include "base/task/sequence_manager/sequence_manager_impl.h" |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 21 | #include "base/threading/thread.h" |
| 22 | #include "base/time/time.h" |
| 23 | #include "build/build_config.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | #include "testing/perf/perf_test.h" |
| 26 | |
| 27 | #if defined(OS_ANDROID) |
| 28 | #include "base/android/java_handler_thread.h" |
| 29 | #endif |
| 30 | |
| 31 | namespace base { |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | #if defined(OS_ANDROID) |
| 35 | class JavaHandlerThreadForTest : public android::JavaHandlerThread { |
| 36 | public: |
| 37 | explicit JavaHandlerThreadForTest(const char* name) |
| 38 | : android::JavaHandlerThread(name, base::ThreadPriority::NORMAL) {} |
| 39 | |
Gabriel Charette | 35f2625 | 2019-08-12 12:01:20 | [diff] [blame^] | 40 | using android::JavaHandlerThread::state; |
| 41 | using android::JavaHandlerThread::State; |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 42 | }; |
| 43 | #endif |
| 44 | |
| 45 | } // namespace |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 46 | |
| 47 | class ScheduleWorkTest : public testing::Test { |
| 48 | public: |
| 49 | ScheduleWorkTest() : counter_(0) {} |
| 50 | |
fdoray | dc2a659 | 2015-10-15 03:46:40 | [diff] [blame] | 51 | void SetUp() override { |
| 52 | if (base::ThreadTicks::IsSupported()) |
| 53 | base::ThreadTicks::WaitUntilInitialized(); |
| 54 | } |
| 55 | |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 56 | void Increment(uint64_t amount) { counter_ += amount; } |
| 57 | |
| 58 | void Schedule(int index) { |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 59 | base::TimeTicks start = base::TimeTicks::Now(); |
miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 60 | base::ThreadTicks thread_start; |
| 61 | if (ThreadTicks::IsSupported()) |
| 62 | thread_start = base::ThreadTicks::Now(); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 63 | base::TimeDelta minimum = base::TimeDelta::Max(); |
| 64 | base::TimeDelta maximum = base::TimeDelta(); |
| 65 | base::TimeTicks now, lastnow = start; |
| 66 | uint64_t schedule_calls = 0u; |
| 67 | do { |
| 68 | for (size_t i = 0; i < kBatchSize; ++i) { |
Alex Clarke | 00c5639 | 2019-02-15 20:22:56 | [diff] [blame] | 69 | target_message_loop_base()->GetMessagePump()->ScheduleWork(); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 70 | schedule_calls++; |
| 71 | } |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 72 | now = base::TimeTicks::Now(); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 73 | base::TimeDelta laptime = now - lastnow; |
| 74 | lastnow = now; |
| 75 | minimum = std::min(minimum, laptime); |
| 76 | maximum = std::max(maximum, laptime); |
| 77 | } while (now - start < base::TimeDelta::FromSeconds(kTargetTimeSec)); |
| 78 | |
| 79 | scheduling_times_[index] = now - start; |
miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 80 | if (ThreadTicks::IsSupported()) |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 81 | scheduling_thread_times_[index] = |
miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 82 | base::ThreadTicks::Now() - thread_start; |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 83 | min_batch_times_[index] = minimum; |
| 84 | max_batch_times_[index] = maximum; |
Alex Clarke | 00c5639 | 2019-02-15 20:22:56 | [diff] [blame] | 85 | target_message_loop_base()->GetTaskRunner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 86 | FROM_HERE, base::BindOnce(&ScheduleWorkTest::Increment, |
| 87 | base::Unretained(this), schedule_calls)); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 88 | } |
| 89 | |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 90 | void ScheduleWork(MessagePumpType target_type, int num_scheduling_threads) { |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 91 | #if defined(OS_ANDROID) |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 92 | if (target_type == MessagePumpType::JAVA) { |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 93 | java_thread_.reset(new JavaHandlerThreadForTest("target")); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 94 | java_thread_->Start(); |
| 95 | } else |
| 96 | #endif |
| 97 | { |
Alex Clarke | 636d6b6 | 2019-02-22 01:39:04 | [diff] [blame] | 98 | target_.reset(new Thread("test")); |
| 99 | |
| 100 | Thread::Options options(target_type, 0u); |
| 101 | |
| 102 | std::unique_ptr<MessageLoop> message_loop = |
| 103 | MessageLoop::CreateUnbound(target_type); |
| 104 | message_loop_ = message_loop.get(); |
| 105 | options.task_environment = |
| 106 | new internal::MessageLoopTaskEnvironment(std::move(message_loop)); |
| 107 | target_->StartWithOptions(options); |
amistry | 7fc519fa | 2015-08-10 23:50:44 | [diff] [blame] | 108 | |
| 109 | // Without this, it's possible for the scheduling threads to start and run |
| 110 | // before the target thread. In this case, the scheduling threads will |
| 111 | // call target_message_loop()->ScheduleWork(), which dereferences the |
| 112 | // loop's message pump, which is only created after the target thread has |
| 113 | // finished starting. |
| 114 | target_->WaitUntilThreadStarted(); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 115 | } |
| 116 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 117 | std::vector<std::unique_ptr<Thread>> scheduling_threads; |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 118 | scheduling_times_.reset(new base::TimeDelta[num_scheduling_threads]); |
| 119 | scheduling_thread_times_.reset(new base::TimeDelta[num_scheduling_threads]); |
| 120 | min_batch_times_.reset(new base::TimeDelta[num_scheduling_threads]); |
| 121 | max_batch_times_.reset(new base::TimeDelta[num_scheduling_threads]); |
| 122 | |
| 123 | for (int i = 0; i < num_scheduling_threads; ++i) { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 124 | scheduling_threads.push_back(std::make_unique<Thread>("posting thread")); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 125 | scheduling_threads[i]->Start(); |
| 126 | } |
| 127 | |
| 128 | for (int i = 0; i < num_scheduling_threads; ++i) { |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 129 | scheduling_threads[i]->task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 130 | FROM_HERE, base::BindOnce(&ScheduleWorkTest::Schedule, |
| 131 | base::Unretained(this), i)); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | for (int i = 0; i < num_scheduling_threads; ++i) { |
| 135 | scheduling_threads[i]->Stop(); |
| 136 | } |
| 137 | #if defined(OS_ANDROID) |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 138 | if (target_type == MessagePumpType::JAVA) { |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 139 | java_thread_->Stop(); |
| 140 | java_thread_.reset(); |
| 141 | } else |
| 142 | #endif |
| 143 | { |
| 144 | target_->Stop(); |
| 145 | target_.reset(); |
| 146 | } |
| 147 | base::TimeDelta total_time; |
| 148 | base::TimeDelta total_thread_time; |
| 149 | base::TimeDelta min_batch_time = base::TimeDelta::Max(); |
| 150 | base::TimeDelta max_batch_time = base::TimeDelta(); |
| 151 | for (int i = 0; i < num_scheduling_threads; ++i) { |
| 152 | total_time += scheduling_times_[i]; |
| 153 | total_thread_time += scheduling_thread_times_[i]; |
| 154 | min_batch_time = std::min(min_batch_time, min_batch_times_[i]); |
| 155 | max_batch_time = std::max(max_batch_time, max_batch_times_[i]); |
| 156 | } |
| 157 | std::string trace = StringPrintf( |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 158 | "%d_threads_scheduling_to_%s_pump", num_scheduling_threads, |
| 159 | target_type == MessagePumpType::IO |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 160 | ? "io" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 161 | : (target_type == MessagePumpType::UI ? "ui" : "default")); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 162 | perf_test::PrintResult( |
| 163 | "task", |
| 164 | "", |
| 165 | trace, |
| 166 | total_time.InMicroseconds() / static_cast<double>(counter_), |
| 167 | "us/task", |
| 168 | true); |
| 169 | perf_test::PrintResult( |
| 170 | "task", |
| 171 | "_min_batch_time", |
| 172 | trace, |
| 173 | min_batch_time.InMicroseconds() / static_cast<double>(kBatchSize), |
| 174 | "us/task", |
| 175 | false); |
| 176 | perf_test::PrintResult( |
| 177 | "task", |
| 178 | "_max_batch_time", |
| 179 | trace, |
| 180 | max_batch_time.InMicroseconds() / static_cast<double>(kBatchSize), |
| 181 | "us/task", |
| 182 | false); |
miu | 1ab506a | 2015-05-29 23:57:12 | [diff] [blame] | 183 | if (ThreadTicks::IsSupported()) { |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 184 | perf_test::PrintResult( |
| 185 | "task", |
| 186 | "_thread_time", |
| 187 | trace, |
| 188 | total_thread_time.InMicroseconds() / static_cast<double>(counter_), |
| 189 | "us/task", |
| 190 | true); |
| 191 | } |
| 192 | } |
| 193 | |
Carlos Caballero | 14712af9 | 2019-04-17 16:13:01 | [diff] [blame] | 194 | sequence_manager::internal::SequenceManagerImpl* target_message_loop_base() { |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 195 | #if defined(OS_ANDROID) |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 196 | if (java_thread_) { |
| 197 | return static_cast<sequence_manager::internal::SequenceManagerImpl*>( |
Gabriel Charette | 35f2625 | 2019-08-12 12:01:20 | [diff] [blame^] | 198 | java_thread_->state()->sequence_manager.get()); |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 199 | } |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 200 | #endif |
Carlos Caballero | 14712af9 | 2019-04-17 16:13:01 | [diff] [blame] | 201 | return MessageLoopCurrent::Get()->GetCurrentSequenceManagerImpl(); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private: |
Alex Clarke | 636d6b6 | 2019-02-22 01:39:04 | [diff] [blame] | 205 | std::unique_ptr<Thread> target_; |
| 206 | MessageLoop* message_loop_; |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 207 | #if defined(OS_ANDROID) |
Alex Clarke | e1cc1bb | 2019-06-06 17:24:25 | [diff] [blame] | 208 | std::unique_ptr<JavaHandlerThreadForTest> java_thread_; |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 209 | #endif |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 210 | std::unique_ptr<base::TimeDelta[]> scheduling_times_; |
| 211 | std::unique_ptr<base::TimeDelta[]> scheduling_thread_times_; |
| 212 | std::unique_ptr<base::TimeDelta[]> min_batch_times_; |
| 213 | std::unique_ptr<base::TimeDelta[]> max_batch_times_; |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 214 | uint64_t counter_; |
| 215 | |
| 216 | static const size_t kTargetTimeSec = 5; |
| 217 | static const size_t kBatchSize = 1000; |
| 218 | }; |
| 219 | |
| 220 | TEST_F(ScheduleWorkTest, ThreadTimeToIOFromOneThread) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 221 | ScheduleWork(MessagePumpType::IO, 1); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | TEST_F(ScheduleWorkTest, ThreadTimeToIOFromTwoThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 225 | ScheduleWork(MessagePumpType::IO, 2); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | TEST_F(ScheduleWorkTest, ThreadTimeToIOFromFourThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 229 | ScheduleWork(MessagePumpType::IO, 4); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | TEST_F(ScheduleWorkTest, ThreadTimeToUIFromOneThread) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 233 | ScheduleWork(MessagePumpType::UI, 1); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST_F(ScheduleWorkTest, ThreadTimeToUIFromTwoThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 237 | ScheduleWork(MessagePumpType::UI, 2); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST_F(ScheduleWorkTest, ThreadTimeToUIFromFourThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 241 | ScheduleWork(MessagePumpType::UI, 4); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(ScheduleWorkTest, ThreadTimeToDefaultFromOneThread) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 245 | ScheduleWork(MessagePumpType::DEFAULT, 1); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST_F(ScheduleWorkTest, ThreadTimeToDefaultFromTwoThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 249 | ScheduleWork(MessagePumpType::DEFAULT, 2); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_F(ScheduleWorkTest, ThreadTimeToDefaultFromFourThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 253 | ScheduleWork(MessagePumpType::DEFAULT, 4); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #if defined(OS_ANDROID) |
| 257 | TEST_F(ScheduleWorkTest, ThreadTimeToJavaFromOneThread) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 258 | ScheduleWork(MessagePumpType::JAVA, 1); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | TEST_F(ScheduleWorkTest, ThreadTimeToJavaFromTwoThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 262 | ScheduleWork(MessagePumpType::JAVA, 2); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(ScheduleWorkTest, ThreadTimeToJavaFromFourThreads) { |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 266 | ScheduleWork(MessagePumpType::JAVA, 4); |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 267 | } |
| 268 | #endif |
| 269 | |
jamesr | 2e146d7 | 2014-09-29 21:12:44 | [diff] [blame] | 270 | } // namespace base |