alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 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 | |
Greg Kraynov | 8ae7ac323 | 2018-07-04 14:56:27 | [diff] [blame] | 5 | #include "base/task/sequence_manager/sequence_manager.h" |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 6 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 7 | #include <stddef.h> |
Gyuyoung Kim | 8a77f8c | 2017-09-27 12:24:16 | [diff] [blame] | 8 | #include <memory> |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 9 | |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 10 | #include "base/bind.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
Greg Kraynov | 9969398 | 2018-08-09 09:05:19 | [diff] [blame] | 12 | #include "base/message_loop/message_pump_default.h" |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Francois Doray | e2bfbf32 | 2018-11-19 14:04:45 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
Gabriel Charette | 5ff87ce | 2017-05-16 18:03:45 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 17 | #include "base/synchronization/condition_variable.h" |
| 18 | #include "base/task/post_task.h" |
Greg Kraynov | 8ae7ac323 | 2018-07-04 14:56:27 | [diff] [blame] | 19 | #include "base/task/sequence_manager/task_queue_impl.h" |
Greg Kraynov | 1d6ba626 | 2018-07-11 18:29:48 | [diff] [blame] | 20 | #include "base/task/sequence_manager/test/mock_time_domain.h" |
| 21 | #include "base/task/sequence_manager/test/sequence_manager_for_test.h" |
| 22 | #include "base/task/sequence_manager/test/test_task_queue.h" |
| 23 | #include "base/task/sequence_manager/test/test_task_time_observer.h" |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 24 | #include "base/task/sequence_manager/thread_controller_with_message_pump_impl.h" |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 25 | #include "base/task/task_traits.h" |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 26 | #include "base/task/thread_pool/thread_pool.h" |
| 27 | #include "base/task/thread_pool/thread_pool_impl.h" |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 28 | #include "base/threading/thread.h" |
fdoray | 19e49e9 | 2016-09-30 20:42:16 | [diff] [blame] | 29 | #include "base/threading/thread_task_runner_handle.h" |
alexclarke | f65eb2d | 2015-11-03 18:24:16 | [diff] [blame] | 30 | #include "base/time/default_tick_clock.h" |
Alex Clarke | fd95959 | 2018-10-30 15:32:46 | [diff] [blame] | 31 | #include "build/build_config.h" |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | #include "testing/perf/perf_test.h" |
| 34 | |
Greg Kraynov | 8f478446 | 2018-05-14 10:08:21 | [diff] [blame] | 35 | namespace base { |
| 36 | namespace sequence_manager { |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 37 | namespace { |
| 38 | const int kNumTasks = 1000000; |
| 39 | } |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 40 | |
Greg Kraynov | fd592fd | 2018-06-29 18:37:37 | [diff] [blame] | 41 | // To reduce noise related to the OS timer, we use a mock time domain to |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 42 | // fast forward the timers. |
Greg Kraynov | fd592fd | 2018-06-29 18:37:37 | [diff] [blame] | 43 | class PerfTestTimeDomain : public MockTimeDomain { |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 44 | public: |
Greg Kraynov | fd592fd | 2018-06-29 18:37:37 | [diff] [blame] | 45 | PerfTestTimeDomain() : MockTimeDomain(TimeTicks::Now()) {} |
Chris Watkins | 6c388179 | 2018-01-09 04:05:18 | [diff] [blame] | 46 | ~PerfTestTimeDomain() override = default; |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 47 | |
Greg Kraynov | 8f478446 | 2018-05-14 10:08:21 | [diff] [blame] | 48 | Optional<TimeDelta> DelayTillNextTask(LazyNow* lazy_now) override { |
Greg Kraynov | f8240bda | 2018-06-28 17:42:36 | [diff] [blame] | 49 | Optional<TimeTicks> run_time = NextScheduledRunTime(); |
| 50 | if (!run_time) |
| 51 | return nullopt; |
Greg Kraynov | fd592fd | 2018-06-29 18:37:37 | [diff] [blame] | 52 | SetNowTicks(*run_time); |
| 53 | // Makes SequenceManager to continue immediately. |
| 54 | return TimeDelta(); |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 55 | } |
| 56 | |
Greg Kraynov | e28bfbc | 2018-06-29 14:53:37 | [diff] [blame] | 57 | void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) override { |
alexclarke | efc263f | 2016-10-20 13:51:49 | [diff] [blame] | 58 | // De-dupe DoWorks. |
altimin | 8053f44 | 2017-04-11 15:05:08 | [diff] [blame] | 59 | if (NumberOfScheduledWakeUps() == 1u) |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 60 | RequestDoWork(); |
| 61 | } |
| 62 | |
Greg Kraynov | fd592fd | 2018-06-29 18:37:37 | [diff] [blame] | 63 | private: |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 64 | DISALLOW_COPY_AND_ASSIGN(PerfTestTimeDomain); |
| 65 | }; |
| 66 | |
Gabriel Charette | 75110a5 | 2019-01-29 22:39:33 | [diff] [blame] | 67 | // TODO(crbug.com/891670): This can be simplified after the transition away from |
| 68 | // the old base::MessageLoop. |
| 69 | enum class PerfTestType { |
| 70 | // A SequenceManager on top of a MessageLoop (which is SequenceManager based). |
| 71 | // This configuration is now strictly overkill. |
| 72 | kUseSequenceManagerWithMessageLoop, |
| 73 | kUseSequenceManagerWithUIMessageLoop, |
| 74 | kUseSequenceManagerWithIOMessageLoop, |
| 75 | |
| 76 | // A SequenceManager with a ThreadControllerWithMessagePumpImpl driving the |
| 77 | // thread. |
| 78 | kUseSequenceManagerWithMessagePump, |
| 79 | kUseSequenceManagerWithUIMessagePump, |
| 80 | kUseSequenceManagerWithIOMessagePump, |
| 81 | kUseSequenceManagerWithMessagePumpAndRandomSampling, |
| 82 | |
| 83 | // A SequenceManager backed base::MessageLoop (now the default and only |
| 84 | // base::MessageLoop configuration). |
| 85 | kUseMessageLoop, |
| 86 | kUseUIMessageLoop, |
| 87 | kUseIOMessageLoop, |
| 88 | |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 89 | // A SingleThreadTaskRunner in the thread pool. |
| 90 | kUseSingleThreadInThreadPool, |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 91 | }; |
| 92 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 93 | // Customization point for SequenceManagerPerfTest which allows us to test |
| 94 | // various implementations. |
| 95 | class PerfTestDelegate { |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 96 | public: |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 97 | virtual ~PerfTestDelegate() = default; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 98 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 99 | virtual const char* GetName() const = 0; |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 100 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 101 | virtual bool VirtualTimeIsSupported() const = 0; |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 102 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 103 | virtual bool MultipleQueuesSupported() const = 0; |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 104 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 105 | virtual scoped_refptr<TaskRunner> CreateTaskRunner() = 0; |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 106 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 107 | virtual void WaitUntilDone() = 0; |
| 108 | |
| 109 | virtual void SignalDone() = 0; |
| 110 | }; |
| 111 | |
| 112 | class BaseSequenceManagerPerfTestDelegate : public PerfTestDelegate { |
| 113 | public: |
| 114 | BaseSequenceManagerPerfTestDelegate() {} |
| 115 | |
| 116 | ~BaseSequenceManagerPerfTestDelegate() override = default; |
| 117 | |
| 118 | bool VirtualTimeIsSupported() const override { return true; } |
| 119 | |
| 120 | bool MultipleQueuesSupported() const override { return true; } |
| 121 | |
| 122 | scoped_refptr<TaskRunner> CreateTaskRunner() override { |
| 123 | scoped_refptr<TestTaskQueue> task_queue = |
Alex Clarke | a9487227 | 2018-11-20 09:42:10 | [diff] [blame] | 124 | manager_->CreateTaskQueueWithType<TestTaskQueue>( |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 125 | TaskQueue::Spec("test").SetTimeDomain(time_domain_.get())); |
| 126 | owned_task_queues_.push_back(task_queue); |
| 127 | return task_queue->task_runner(); |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 128 | } |
| 129 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 130 | void WaitUntilDone() override { |
| 131 | run_loop_.reset(new RunLoop()); |
| 132 | run_loop_->Run(); |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 133 | } |
| 134 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 135 | void SignalDone() override { run_loop_->Quit(); } |
| 136 | |
| 137 | SequenceManager* GetManager() const { return manager_.get(); } |
| 138 | |
| 139 | void SetSequenceManager(std::unique_ptr<SequenceManager> manager) { |
| 140 | manager_ = std::move(manager); |
| 141 | time_domain_ = std::make_unique<PerfTestTimeDomain>(); |
| 142 | manager_->RegisterTimeDomain(time_domain_.get()); |
| 143 | } |
| 144 | |
| 145 | void ShutDown() { |
| 146 | owned_task_queues_.clear(); |
| 147 | manager_->UnregisterTimeDomain(time_domain_.get()); |
| 148 | manager_.reset(); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | std::unique_ptr<SequenceManager> manager_; |
| 153 | std::unique_ptr<TimeDomain> time_domain_; |
| 154 | std::unique_ptr<RunLoop> run_loop_; |
| 155 | std::vector<scoped_refptr<TestTaskQueue>> owned_task_queues_; |
| 156 | }; |
| 157 | |
| 158 | template <class MessageLoopType> |
| 159 | class SequenceManagerWithMessageLoopPerfTestDelegate |
| 160 | : public BaseSequenceManagerPerfTestDelegate { |
| 161 | public: |
| 162 | explicit SequenceManagerWithMessageLoopPerfTestDelegate(const char* name) |
| 163 | : name_(name), message_loop_(new MessageLoopType()) { |
Carlos Caballero | b9fb1b1e | 2019-04-01 11:49:17 | [diff] [blame] | 164 | SetSequenceManager(CreateSequenceManagerOnCurrentThread( |
Alex Clarke | 5f3d461b | 2019-04-29 14:23:11 | [diff] [blame] | 165 | SequenceManager::Settings::Builder() |
| 166 | .SetRandomisedSamplingEnabled(false) |
| 167 | .Build())); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ~SequenceManagerWithMessageLoopPerfTestDelegate() override { ShutDown(); } |
| 171 | |
| 172 | const char* GetName() const override { return name_; } |
| 173 | |
| 174 | private: |
| 175 | const char* const name_; |
| 176 | std::unique_ptr<MessageLoop> message_loop_; |
| 177 | }; |
| 178 | |
| 179 | class SequenceManagerWithMessagePumpPerfTestDelegate |
| 180 | : public BaseSequenceManagerPerfTestDelegate { |
| 181 | public: |
Karolina Soltys | 1dccaeb | 2018-12-05 15:20:01 | [diff] [blame] | 182 | SequenceManagerWithMessagePumpPerfTestDelegate( |
| 183 | const char* name, |
| 184 | MessageLoop::Type type, |
| 185 | bool randomised_sampling_enabled = false) |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 186 | : name_(name) { |
Alex Clarke | 5f3d461b | 2019-04-29 14:23:11 | [diff] [blame] | 187 | auto settings = |
| 188 | SequenceManager::Settings::Builder() |
| 189 | .SetRandomisedSamplingEnabled(randomised_sampling_enabled) |
| 190 | .Build(); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 191 | SetSequenceManager(SequenceManagerForTest::Create( |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 192 | std::make_unique<internal::ThreadControllerWithMessagePumpImpl>( |
Alex Clarke | 5f3d461b | 2019-04-29 14:23:11 | [diff] [blame] | 193 | MessageLoop ::CreateMessagePumpForType(type), settings), |
| 194 | std::move(settings))); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 195 | |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 196 | // ThreadControllerWithMessagePumpImpl doesn't provide a default task |
| 197 | // runner. |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 198 | scoped_refptr<TaskQueue> default_task_queue = |
Alex Clarke | a9487227 | 2018-11-20 09:42:10 | [diff] [blame] | 199 | GetManager()->template CreateTaskQueueWithType<TestTaskQueue>( |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 200 | TaskQueue::Spec("default")); |
| 201 | GetManager()->SetDefaultTaskRunner(default_task_queue->task_runner()); |
fdoray | dc2a659 | 2015-10-15 03:46:40 | [diff] [blame] | 202 | } |
| 203 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 204 | ~SequenceManagerWithMessagePumpPerfTestDelegate() override { ShutDown(); } |
| 205 | |
| 206 | const char* GetName() const override { return name_; } |
| 207 | |
| 208 | private: |
| 209 | const char* const name_; |
| 210 | }; |
| 211 | |
| 212 | class MessageLoopPerfTestDelegate : public PerfTestDelegate { |
| 213 | public: |
| 214 | MessageLoopPerfTestDelegate(const char* name, |
| 215 | std::unique_ptr<MessageLoop> message_loop) |
| 216 | : name_(name), message_loop_(std::move(message_loop)) {} |
| 217 | |
| 218 | ~MessageLoopPerfTestDelegate() override = default; |
| 219 | |
| 220 | const char* GetName() const override { return name_; } |
| 221 | |
| 222 | bool VirtualTimeIsSupported() const override { return false; } |
| 223 | |
| 224 | bool MultipleQueuesSupported() const override { return false; } |
| 225 | |
| 226 | scoped_refptr<TaskRunner> CreateTaskRunner() override { |
| 227 | return message_loop_->task_runner(); |
Alex Clarke | fd95959 | 2018-10-30 15:32:46 | [diff] [blame] | 228 | } |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 229 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 230 | void WaitUntilDone() override { |
| 231 | run_loop_.reset(new RunLoop()); |
| 232 | run_loop_->Run(); |
| 233 | } |
| 234 | |
| 235 | void SignalDone() override { run_loop_->Quit(); } |
| 236 | |
| 237 | private: |
| 238 | const char* const name_; |
| 239 | std::unique_ptr<MessageLoop> message_loop_; |
| 240 | std::unique_ptr<RunLoop> run_loop_; |
| 241 | }; |
| 242 | |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 243 | class SingleThreadInThreadPoolPerfTestDelegate : public PerfTestDelegate { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 244 | public: |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 245 | SingleThreadInThreadPoolPerfTestDelegate() : done_cond_(&done_lock_) { |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame^] | 246 | ThreadPoolInstance::Set( |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 247 | std::make_unique<::base::internal::ThreadPoolImpl>("Test")); |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame^] | 248 | ThreadPoolInstance::Get()->StartWithDefaultParams(); |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 249 | } |
| 250 | |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 251 | ~SingleThreadInThreadPoolPerfTestDelegate() override { |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame^] | 252 | ThreadPoolInstance::Get()->JoinForTesting(); |
| 253 | ThreadPoolInstance::Set(nullptr); |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 254 | } |
| 255 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 256 | const char* GetName() const override { |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 257 | return " single thread in ThreadPool "; |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 258 | } |
| 259 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 260 | bool VirtualTimeIsSupported() const override { return false; } |
| 261 | |
| 262 | bool MultipleQueuesSupported() const override { return false; } |
| 263 | |
| 264 | scoped_refptr<TaskRunner> CreateTaskRunner() override { |
| 265 | return CreateSingleThreadTaskRunnerWithTraits( |
| 266 | {TaskPriority::USER_BLOCKING}); |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 267 | } |
| 268 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 269 | void WaitUntilDone() override { |
| 270 | AutoLock auto_lock(done_lock_); |
| 271 | done_cond_.Wait(); |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 272 | } |
| 273 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 274 | void SignalDone() override { |
| 275 | AutoLock auto_lock(done_lock_); |
| 276 | done_cond_.Signal(); |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 277 | } |
| 278 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 279 | private: |
| 280 | Lock done_lock_; |
| 281 | ConditionVariable done_cond_; |
| 282 | }; |
| 283 | |
| 284 | class TestCase { |
| 285 | public: |
| 286 | // |delegate| is assumed to outlive TestCase. |
| 287 | explicit TestCase(PerfTestDelegate* delegate) : delegate_(delegate) {} |
| 288 | |
| 289 | virtual ~TestCase() = default; |
| 290 | |
| 291 | virtual void Start() = 0; |
| 292 | |
| 293 | protected: |
| 294 | PerfTestDelegate* const delegate_; // NOT OWNED |
| 295 | }; |
| 296 | |
| 297 | class TaskSource { |
| 298 | public: |
| 299 | virtual ~TaskSource() = default; |
| 300 | |
| 301 | virtual void Start() = 0; |
| 302 | }; |
| 303 | |
| 304 | class SameThreadTaskSource : public TaskSource { |
| 305 | public: |
| 306 | SameThreadTaskSource(std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 307 | size_t num_tasks) |
| 308 | : num_queues_(task_runners.size()), |
| 309 | num_tasks_(num_tasks), |
| 310 | task_closure_( |
| 311 | BindRepeating(&SameThreadTaskSource::TestTask, Unretained(this))), |
Francois Doray | e2bfbf32 | 2018-11-19 14:04:45 | [diff] [blame] | 312 | task_runners_(std::move(task_runners)) { |
| 313 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 314 | } |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 315 | |
| 316 | void Start() override { |
| 317 | num_tasks_in_flight_ = 1; |
| 318 | num_tasks_to_post_ = num_tasks_; |
| 319 | num_tasks_to_run_ = num_tasks_; |
Francois Doray | e2bfbf32 | 2018-11-19 14:04:45 | [diff] [blame] | 320 | // Post the initial task instead of running it synchronously to ensure that |
| 321 | // all invocations happen on the same sequence. |
| 322 | PostTask(0); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | protected: |
| 326 | virtual void PostTask(unsigned int queue) = 0; |
| 327 | |
| 328 | virtual void SignalDone() = 0; |
| 329 | |
| 330 | void TestTask() { |
Francois Doray | e2bfbf32 | 2018-11-19 14:04:45 | [diff] [blame] | 331 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 332 | |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 333 | if (--num_tasks_to_run_ == 0) { |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 334 | SignalDone(); |
alexclarke | 5768f7d | 2016-10-06 13:23:39 | [diff] [blame] | 335 | return; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | num_tasks_in_flight_--; |
| 339 | // NOTE there are only up to max_tasks_in_flight_ pending delayed tasks at |
| 340 | // any one time. Thanks to the lower_num_tasks_to_post going to zero if |
| 341 | // there are a lot of tasks in flight, the total number of task in flight at |
| 342 | // any one time is very variable. |
| 343 | unsigned int lower_num_tasks_to_post = |
| 344 | num_tasks_in_flight_ < (max_tasks_in_flight_ / 2) ? 1 : 0; |
| 345 | unsigned int max_tasks_to_post = |
| 346 | num_tasks_to_post_ % 2 ? lower_num_tasks_to_post : 10; |
| 347 | for (unsigned int i = 0; |
| 348 | i < max_tasks_to_post && num_tasks_in_flight_ < max_tasks_in_flight_ && |
skyostil | 0bbc9d2 | 2015-09-29 13:07:23 | [diff] [blame] | 349 | num_tasks_to_post_ > 0; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 350 | i++) { |
| 351 | // Choose a queue weighted towards queue 0. |
| 352 | unsigned int queue = num_tasks_to_post_ % (num_queues_ + 1); |
| 353 | if (queue == num_queues_) { |
| 354 | queue = 0; |
| 355 | } |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 356 | PostTask(queue); |
| 357 | num_tasks_in_flight_++; |
| 358 | num_tasks_to_post_--; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | const size_t num_queues_; |
| 363 | const size_t num_tasks_; |
| 364 | const RepeatingClosure task_closure_; |
| 365 | const std::vector<scoped_refptr<TaskRunner>> task_runners_; |
| 366 | const unsigned int max_tasks_in_flight_ = 200; |
| 367 | unsigned int num_tasks_in_flight_; |
| 368 | unsigned int num_tasks_to_post_; |
| 369 | unsigned int num_tasks_to_run_; |
Francois Doray | e2bfbf32 | 2018-11-19 14:04:45 | [diff] [blame] | 370 | SEQUENCE_CHECKER(sequence_checker_); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | class CrossThreadTaskSource : public TaskSource { |
| 374 | public: |
| 375 | CrossThreadTaskSource(std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 376 | size_t num_tasks) |
| 377 | : num_queues_(task_runners.size()), |
| 378 | num_tasks_(num_tasks), |
| 379 | task_closure_( |
| 380 | BindRepeating(&CrossThreadTaskSource::TestTask, Unretained(this))), |
| 381 | task_runners_(std::move(task_runners)) {} |
| 382 | |
| 383 | void Start() override { |
| 384 | num_tasks_in_flight_ = 0; |
| 385 | num_tasks_to_run_ = num_tasks_; |
| 386 | |
| 387 | for (size_t i = 0; i < num_tasks_; i++) { |
| 388 | while (num_tasks_in_flight_.load(std::memory_order_acquire) > |
| 389 | max_tasks_in_flight_) { |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 390 | PlatformThread::YieldCurrentThread(); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 391 | } |
| 392 | // Choose a queue weighted towards queue 0. |
| 393 | unsigned int queue = i % (num_queues_ + 1); |
| 394 | if (queue == num_queues_) { |
| 395 | queue = 0; |
| 396 | } |
| 397 | PostTask(queue); |
| 398 | num_tasks_in_flight_++; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | protected: |
| 403 | virtual void PostTask(unsigned int queue) = 0; |
| 404 | |
| 405 | // Will be called on the main thread. |
| 406 | virtual void SignalDone() = 0; |
| 407 | |
| 408 | void TestTask() { |
| 409 | if (num_tasks_to_run_.fetch_sub(1) == 1) { |
| 410 | SignalDone(); |
| 411 | return; |
| 412 | } |
| 413 | num_tasks_in_flight_--; |
| 414 | } |
| 415 | |
| 416 | const size_t num_queues_; |
| 417 | const size_t num_tasks_; |
| 418 | const RepeatingClosure task_closure_; |
| 419 | const std::vector<scoped_refptr<TaskRunner>> task_runners_; |
| 420 | const unsigned int max_tasks_in_flight_ = 200; |
| 421 | std::atomic<unsigned int> num_tasks_in_flight_; |
| 422 | std::atomic<unsigned int> num_tasks_to_run_; |
| 423 | }; |
| 424 | |
| 425 | class SingleThreadImmediateTestCase : public TestCase { |
| 426 | public: |
| 427 | SingleThreadImmediateTestCase( |
| 428 | PerfTestDelegate* delegate, |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 429 | std::vector<scoped_refptr<TaskRunner>> task_runners) |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 430 | : TestCase(delegate), |
| 431 | task_source_(std::make_unique<SingleThreadImmediateTaskSource>( |
| 432 | delegate, |
| 433 | std::move(task_runners), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 434 | kNumTasks)) {} |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 435 | |
| 436 | void Start() override { task_source_->Start(); } |
| 437 | |
| 438 | private: |
| 439 | class SingleThreadImmediateTaskSource : public SameThreadTaskSource { |
| 440 | public: |
| 441 | SingleThreadImmediateTaskSource( |
| 442 | PerfTestDelegate* delegate, |
| 443 | std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 444 | size_t num_tasks) |
| 445 | : SameThreadTaskSource(std::move(task_runners), num_tasks), |
| 446 | delegate_(delegate) {} |
| 447 | |
| 448 | ~SingleThreadImmediateTaskSource() override = default; |
| 449 | |
| 450 | void PostTask(unsigned int queue) override { |
| 451 | task_runners_[queue]->PostTask(FROM_HERE, task_closure_); |
| 452 | } |
| 453 | |
| 454 | void SignalDone() override { delegate_->SignalDone(); } |
| 455 | |
| 456 | PerfTestDelegate* delegate_; // NOT OWNED. |
| 457 | }; |
| 458 | |
| 459 | const std::unique_ptr<TaskSource> task_source_; |
| 460 | }; |
| 461 | |
| 462 | class SingleThreadDelayedTestCase : public TestCase { |
| 463 | public: |
| 464 | SingleThreadDelayedTestCase( |
| 465 | PerfTestDelegate* delegate, |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 466 | std::vector<scoped_refptr<TaskRunner>> task_runners) |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 467 | : TestCase(delegate), |
| 468 | task_source_(std::make_unique<SingleThreadDelayedTaskSource>( |
| 469 | delegate, |
| 470 | std::move(task_runners), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 471 | kNumTasks)) {} |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 472 | |
| 473 | void Start() override { task_source_->Start(); } |
| 474 | |
| 475 | private: |
| 476 | class SingleThreadDelayedTaskSource : public SameThreadTaskSource { |
| 477 | public: |
| 478 | explicit SingleThreadDelayedTaskSource( |
| 479 | PerfTestDelegate* delegate, |
| 480 | std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 481 | size_t num_tasks) |
| 482 | : SameThreadTaskSource(std::move(task_runners), num_tasks), |
| 483 | delegate_(delegate) {} |
| 484 | |
| 485 | ~SingleThreadDelayedTaskSource() override = default; |
| 486 | |
| 487 | void PostTask(unsigned int queue) override { |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 488 | unsigned int delay = |
| 489 | num_tasks_to_post_ % 2 ? 1 : (10 + num_tasks_to_post_ % 10); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 490 | task_runners_[queue]->PostDelayedTask(FROM_HERE, task_closure_, |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 491 | TimeDelta::FromMilliseconds(delay)); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void SignalDone() override { delegate_->SignalDone(); } |
| 495 | |
| 496 | PerfTestDelegate* delegate_; // NOT OWNED. |
| 497 | }; |
| 498 | |
| 499 | const std::unique_ptr<TaskSource> task_source_; |
| 500 | }; |
| 501 | |
| 502 | class TwoThreadTestCase : public TestCase { |
| 503 | public: |
| 504 | TwoThreadTestCase(PerfTestDelegate* delegate, |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 505 | std::vector<scoped_refptr<TaskRunner>> task_runners) |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 506 | : TestCase(delegate), |
| 507 | task_runners_(std::move(task_runners)), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 508 | num_tasks_(kNumTasks), |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 509 | auxiliary_thread_("auxillary thread") { |
| 510 | auxiliary_thread_.Start(); |
| 511 | } |
| 512 | |
| 513 | ~TwoThreadTestCase() override { auxiliary_thread_.Stop(); } |
| 514 | |
| 515 | protected: |
| 516 | void Start() override { |
| 517 | done_count_ = 0; |
| 518 | same_thread_task_source_ = |
| 519 | std::make_unique<SingleThreadImmediateTaskSource>(this, task_runners_, |
| 520 | num_tasks_ / 2); |
| 521 | cross_thread_task_scorce_ = |
| 522 | std::make_unique<CrossThreadImmediateTaskSource>(this, task_runners_, |
| 523 | num_tasks_ / 2); |
| 524 | |
| 525 | auxiliary_thread_.task_runner()->PostTask( |
| 526 | FROM_HERE, base::BindOnce(&CrossThreadImmediateTaskSource::Start, |
| 527 | Unretained(cross_thread_task_scorce_.get()))); |
| 528 | same_thread_task_source_->Start(); |
| 529 | } |
| 530 | |
| 531 | class SingleThreadImmediateTaskSource : public SameThreadTaskSource { |
| 532 | public: |
| 533 | SingleThreadImmediateTaskSource( |
| 534 | TwoThreadTestCase* two_thread_test_case, |
| 535 | std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 536 | size_t num_tasks) |
| 537 | : SameThreadTaskSource(std::move(task_runners), num_tasks), |
| 538 | two_thread_test_case_(two_thread_test_case) {} |
| 539 | |
| 540 | ~SingleThreadImmediateTaskSource() override = default; |
| 541 | |
| 542 | void PostTask(unsigned int queue) override { |
| 543 | task_runners_[queue]->PostTask(FROM_HERE, task_closure_); |
| 544 | } |
| 545 | |
| 546 | // Will be called on the main thread. |
| 547 | void SignalDone() override { two_thread_test_case_->SignalDone(); } |
| 548 | |
| 549 | TwoThreadTestCase* two_thread_test_case_; // NOT OWNED. |
| 550 | }; |
| 551 | |
| 552 | class CrossThreadImmediateTaskSource : public CrossThreadTaskSource { |
| 553 | public: |
| 554 | CrossThreadImmediateTaskSource( |
| 555 | TwoThreadTestCase* two_thread_test_case, |
| 556 | std::vector<scoped_refptr<TaskRunner>> task_runners, |
| 557 | size_t num_tasks) |
| 558 | : CrossThreadTaskSource(std::move(task_runners), num_tasks), |
| 559 | two_thread_test_case_(two_thread_test_case) {} |
| 560 | |
| 561 | ~CrossThreadImmediateTaskSource() override = default; |
| 562 | |
| 563 | void PostTask(unsigned int queue) override { |
| 564 | task_runners_[queue]->PostTask(FROM_HERE, task_closure_); |
| 565 | } |
| 566 | |
| 567 | // Will be called on the main thread. |
| 568 | void SignalDone() override { two_thread_test_case_->SignalDone(); } |
| 569 | |
| 570 | TwoThreadTestCase* two_thread_test_case_; // NOT OWNED. |
| 571 | }; |
| 572 | |
| 573 | void SignalDone() { |
| 574 | if (++done_count_ == 2) |
| 575 | delegate_->SignalDone(); |
| 576 | } |
| 577 | |
| 578 | private: |
| 579 | const std::vector<scoped_refptr<TaskRunner>> task_runners_; |
| 580 | const size_t num_tasks_; |
| 581 | Thread auxiliary_thread_; |
| 582 | std::unique_ptr<SingleThreadImmediateTaskSource> same_thread_task_source_; |
| 583 | std::unique_ptr<CrossThreadImmediateTaskSource> cross_thread_task_scorce_; |
| 584 | int done_count_ = 0; |
| 585 | }; |
| 586 | |
| 587 | class SequenceManagerPerfTest : public testing::TestWithParam<PerfTestType> { |
| 588 | public: |
Gabriel Charette | 75110a5 | 2019-01-29 22:39:33 | [diff] [blame] | 589 | SequenceManagerPerfTest() = default; |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 590 | |
Sami Kyostila | cf339fd | 2019-01-28 13:55:07 | [diff] [blame] | 591 | void SetUp() override { delegate_ = CreateDelegate(); } |
| 592 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 593 | void TearDown() override { delegate_.reset(); } |
| 594 | |
| 595 | std::unique_ptr<PerfTestDelegate> CreateDelegate() { |
| 596 | switch (GetParam()) { |
| 597 | case PerfTestType::kUseSequenceManagerWithMessageLoop: |
| 598 | return std::make_unique< |
| 599 | SequenceManagerWithMessageLoopPerfTestDelegate<MessageLoop>>( |
| 600 | " SequenceManager with MessageLoop "); |
| 601 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 602 | case PerfTestType::kUseSequenceManagerWithUIMessageLoop: |
| 603 | return std::make_unique< |
| 604 | SequenceManagerWithMessageLoopPerfTestDelegate<MessageLoopForUI>>( |
| 605 | " SequenceManager with MessageLoopForUI "); |
| 606 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 607 | case PerfTestType::kUseSequenceManagerWithIOMessageLoop: |
| 608 | return std::make_unique< |
| 609 | SequenceManagerWithMessageLoopPerfTestDelegate<MessageLoopForIO>>( |
| 610 | " SequenceManager with MessageLoopForIO "); |
| 611 | |
Gabriel Charette | 75110a5 | 2019-01-29 22:39:33 | [diff] [blame] | 612 | case PerfTestType::kUseSequenceManagerWithMessagePump: |
| 613 | return std::make_unique<SequenceManagerWithMessagePumpPerfTestDelegate>( |
| 614 | " SequenceManager with MessagePumpDefault ", |
| 615 | MessageLoop::TYPE_DEFAULT); |
| 616 | |
| 617 | case PerfTestType::kUseSequenceManagerWithUIMessagePump: |
| 618 | return std::make_unique<SequenceManagerWithMessagePumpPerfTestDelegate>( |
| 619 | " SequenceManager with MessagePumpForUI ", MessageLoop::TYPE_UI); |
| 620 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 621 | case PerfTestType::kUseSequenceManagerWithIOMessagePump: |
| 622 | return std::make_unique<SequenceManagerWithMessagePumpPerfTestDelegate>( |
| 623 | " SequenceManager with MessagePumpForIO ", MessageLoop::TYPE_IO); |
| 624 | |
Gabriel Charette | 75110a5 | 2019-01-29 22:39:33 | [diff] [blame] | 625 | case PerfTestType::kUseSequenceManagerWithMessagePumpAndRandomSampling: |
| 626 | return std::make_unique<SequenceManagerWithMessagePumpPerfTestDelegate>( |
| 627 | " SequenceManager with MessagePumpDefault and random sampling ", |
| 628 | MessageLoop::TYPE_DEFAULT, true); |
| 629 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 630 | case PerfTestType::kUseMessageLoop: |
| 631 | return std::make_unique<MessageLoopPerfTestDelegate>( |
| 632 | " MessageLoop ", std::make_unique<MessageLoop>()); |
| 633 | |
| 634 | case PerfTestType::kUseUIMessageLoop: |
| 635 | return std::make_unique<MessageLoopPerfTestDelegate>( |
| 636 | " MessageLoopForUI ", std::make_unique<MessageLoopForUI>()); |
| 637 | |
| 638 | case PerfTestType::kUseIOMessageLoop: |
| 639 | return std::make_unique<MessageLoopPerfTestDelegate>( |
| 640 | " MessageLoopForIO ", std::make_unique<MessageLoopForIO>()); |
| 641 | |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 642 | case PerfTestType::kUseSingleThreadInThreadPool: |
| 643 | return std::make_unique<SingleThreadInThreadPoolPerfTestDelegate>(); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 644 | |
| 645 | default: |
| 646 | NOTREACHED(); |
| 647 | return nullptr; |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 651 | bool ShouldMeasureQueueScaling() const { |
| 652 | // To limit test run time, we only measure multiple queues specific sequence |
| 653 | // manager configurations. |
| 654 | return delegate_->MultipleQueuesSupported() && |
| 655 | GetParam() == PerfTestType::kUseSequenceManagerWithUIMessagePump; |
| 656 | } |
| 657 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 658 | std::vector<scoped_refptr<TaskRunner>> CreateTaskRunners(int num) { |
| 659 | std::vector<scoped_refptr<TaskRunner>> task_runners; |
| 660 | for (int i = 0; i < num; i++) { |
| 661 | task_runners.push_back(delegate_->CreateTaskRunner()); |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 662 | } |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 663 | return task_runners; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 664 | } |
| 665 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 666 | void Benchmark(const std::string& trace, TestCase* TestCase) { |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 667 | TimeTicks start = TimeTicks::Now(); |
| 668 | TimeTicks now; |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 669 | TestCase->Start(); |
| 670 | delegate_->WaitUntilDone(); |
| 671 | now = TimeTicks::Now(); |
Greg Kraynov | 3634ea3 | 2018-08-07 08:43:38 | [diff] [blame] | 672 | |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 673 | perf_test::PrintResult( |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 674 | "task", "", trace + delegate_->GetName(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 675 | (now - start).InMicroseconds() / static_cast<double>(kNumTasks), |
| 676 | "us/task", true); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 677 | LOG(ERROR) << "task " << trace << delegate_->GetName() |
| 678 | << ((now - start).InMicroseconds() / |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 679 | static_cast<double>(kNumTasks)) |
| 680 | << " us/task"; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 681 | } |
| 682 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 683 | std::unique_ptr<PerfTestDelegate> delegate_; |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 684 | }; |
| 685 | |
Victor Costan | 7266ee8a | 2019-02-13 05:08:23 | [diff] [blame] | 686 | INSTANTIATE_TEST_SUITE_P( |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 687 | , |
| 688 | SequenceManagerPerfTest, |
Karolina Soltys | 1dccaeb | 2018-12-05 15:20:01 | [diff] [blame] | 689 | testing::Values( |
| 690 | PerfTestType::kUseSequenceManagerWithMessageLoop, |
| 691 | PerfTestType::kUseSequenceManagerWithMessagePump, |
| 692 | PerfTestType::kUseSequenceManagerWithUIMessageLoop, |
| 693 | PerfTestType::kUseSequenceManagerWithUIMessagePump, |
| 694 | PerfTestType::kUseSequenceManagerWithIOMessageLoop, |
| 695 | PerfTestType::kUseSequenceManagerWithIOMessagePump, |
| 696 | PerfTestType::kUseMessageLoop, |
| 697 | PerfTestType::kUseUIMessageLoop, |
| 698 | PerfTestType::kUseIOMessageLoop, |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 699 | PerfTestType::kUseSingleThreadInThreadPool, |
Karolina Soltys | 1dccaeb | 2018-12-05 15:20:01 | [diff] [blame] | 700 | PerfTestType::kUseSequenceManagerWithMessagePumpAndRandomSampling)); |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 701 | TEST_P(SequenceManagerPerfTest, PostDelayedTasks_OneQueue) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 702 | if (!delegate_->VirtualTimeIsSupported()) { |
| 703 | LOG(INFO) << "Unsupported"; |
| 704 | return; |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 705 | } |
| 706 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 707 | SingleThreadDelayedTestCase task_source(delegate_.get(), |
| 708 | CreateTaskRunners(1)); |
| 709 | Benchmark("post delayed tasks with one queue", &task_source); |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 710 | } |
| 711 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 712 | TEST_P(SequenceManagerPerfTest, PostDelayedTasks_FourQueues) { |
| 713 | if (!delegate_->VirtualTimeIsSupported() || !ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 714 | LOG(INFO) << "Unsupported"; |
| 715 | return; |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 716 | } |
| 717 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 718 | SingleThreadDelayedTestCase task_source(delegate_.get(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 719 | CreateTaskRunners(4)); |
| 720 | Benchmark("post delayed tasks with four queues", &task_source); |
alexclarke | 976cb66 | 2016-01-20 14:17:43 | [diff] [blame] | 721 | } |
| 722 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 723 | TEST_P(SequenceManagerPerfTest, PostDelayedTasks_EightQueues) { |
| 724 | if (!delegate_->VirtualTimeIsSupported() || !ShouldMeasureQueueScaling()) { |
| 725 | LOG(INFO) << "Unsupported"; |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 726 | return; |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 727 | } |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 728 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 729 | SingleThreadDelayedTestCase task_source(delegate_.get(), |
| 730 | CreateTaskRunners(8)); |
| 731 | Benchmark("post delayed tasks with eight queues", &task_source); |
| 732 | } |
| 733 | |
| 734 | TEST_P(SequenceManagerPerfTest, PostDelayedTasks_ThirtyTwoQueues) { |
| 735 | if (!delegate_->VirtualTimeIsSupported() || !ShouldMeasureQueueScaling()) { |
| 736 | LOG(INFO) << "Unsupported"; |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | SingleThreadDelayedTestCase task_source(delegate_.get(), |
| 741 | CreateTaskRunners(32)); |
| 742 | Benchmark("post delayed tasks with thirty two queues", &task_source); |
| 743 | } |
| 744 | |
| 745 | TEST_P(SequenceManagerPerfTest, PostImmediateTasks_OneQueue) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 746 | SingleThreadImmediateTestCase task_source(delegate_.get(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 747 | CreateTaskRunners(1)); |
| 748 | Benchmark("post immediate tasks with one queue", &task_source); |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 749 | } |
| 750 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 751 | TEST_P(SequenceManagerPerfTest, PostImmediateTasks_FourQueues) { |
| 752 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 753 | LOG(INFO) << "Unsupported"; |
| 754 | return; |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 755 | } |
| 756 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 757 | SingleThreadImmediateTestCase task_source(delegate_.get(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 758 | CreateTaskRunners(4)); |
| 759 | Benchmark("post immediate tasks with four queues", &task_source); |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 760 | } |
| 761 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 762 | TEST_P(SequenceManagerPerfTest, PostImmediateTasks_EightQueues) { |
| 763 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 764 | LOG(INFO) << "Unsupported"; |
| 765 | return; |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 766 | } |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 767 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 768 | SingleThreadImmediateTestCase task_source(delegate_.get(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 769 | CreateTaskRunners(8)); |
| 770 | Benchmark("post immediate tasks with eight queues", &task_source); |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 771 | } |
| 772 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 773 | TEST_P(SequenceManagerPerfTest, PostImmediateTasks_ThirtyTwoQueues) { |
| 774 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 775 | LOG(INFO) << "Unsupported"; |
| 776 | return; |
Alex Clarke | d4b78b8 | 2018-10-19 15:48:53 | [diff] [blame] | 777 | } |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 778 | |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 779 | SingleThreadImmediateTestCase task_source(delegate_.get(), |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 780 | CreateTaskRunners(32)); |
| 781 | Benchmark("post immediate tasks with thirty two queues", &task_source); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 782 | } |
| 783 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 784 | TEST_P(SequenceManagerPerfTest, PostImmediateTasksFromTwoThreads_OneQueue) { |
| 785 | TwoThreadTestCase task_source(delegate_.get(), CreateTaskRunners(1)); |
Sami Kyostila | afb3ed20c | 2018-11-28 10:26:12 | [diff] [blame] | 786 | Benchmark("post immediate tasks with one queue from two threads", |
| 787 | &task_source); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 788 | } |
| 789 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 790 | TEST_P(SequenceManagerPerfTest, PostImmediateTasksFromTwoThreads_FourQueues) { |
| 791 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 792 | LOG(INFO) << "Unsupported"; |
| 793 | return; |
| 794 | } |
| 795 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 796 | TwoThreadTestCase task_source(delegate_.get(), CreateTaskRunners(4)); |
Sami Kyostila | afb3ed20c | 2018-11-28 10:26:12 | [diff] [blame] | 797 | Benchmark("post immediate tasks with four queues from two threads", |
| 798 | &task_source); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 799 | } |
| 800 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 801 | TEST_P(SequenceManagerPerfTest, PostImmediateTasksFromTwoThreads_EightQueues) { |
| 802 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 803 | LOG(INFO) << "Unsupported"; |
| 804 | return; |
| 805 | } |
| 806 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 807 | TwoThreadTestCase task_source(delegate_.get(), CreateTaskRunners(8)); |
Sami Kyostila | afb3ed20c | 2018-11-28 10:26:12 | [diff] [blame] | 808 | Benchmark("post immediate tasks with eight queues from two threads", |
| 809 | &task_source); |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | TEST_P(SequenceManagerPerfTest, |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 813 | PostImmediateTasksFromTwoThreads_ThirtyTwoQueues) { |
| 814 | if (!ShouldMeasureQueueScaling()) { |
Alex Clarke | 3cf9994 | 2018-10-31 11:57:03 | [diff] [blame] | 815 | LOG(INFO) << "Unsupported"; |
| 816 | return; |
| 817 | } |
| 818 | |
Sami Kyostila | 2bde9143 | 2018-11-07 01:43:46 | [diff] [blame] | 819 | TwoThreadTestCase task_source(delegate_.get(), CreateTaskRunners(32)); |
Sami Kyostila | afb3ed20c | 2018-11-28 10:26:12 | [diff] [blame] | 820 | Benchmark("post immediate tasks with thirty two queues from two threads", |
| 821 | &task_source); |
Alex Clarke | a77f1e0 | 2018-05-21 09:20:18 | [diff] [blame] | 822 | } |
| 823 | |
alexclarke | f02cff06d | 2015-03-06 12:11:50 | [diff] [blame] | 824 | // TODO(alexclarke): Add additional tests with different mixes of non-delayed vs |
| 825 | // delayed tasks. |
| 826 | |
Greg Kraynov | 8f478446 | 2018-05-14 10:08:21 | [diff] [blame] | 827 | } // namespace sequence_manager |
| 828 | } // namespace base |