Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1224)

Unified Diff: third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper_unittest.cc

Issue 2258133002: [scheduler] Implement time-based cpu throttling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper_unittest.cc
diff --git a/third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper_unittest.cc b/third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper_unittest.cc
deleted file mode 100644
index b9a80cd0d85207567247a8fdca52a4076b1a1645..0000000000000000000000000000000000000000
--- a/third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper_unittest.cc
+++ /dev/null
@@ -1,494 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "platform/scheduler/renderer/throttling_helper.h"
-
-#include <stddef.h>
-
-#include <memory>
-
-#include "base/callback.h"
-#include "base/macros.h"
-#include "base/memory/ptr_util.h"
-#include "base/test/simple_test_tick_clock.h"
-#include "cc/test/ordered_simple_task_runner.h"
-#include "platform/scheduler/base/test_time_source.h"
-#include "platform/scheduler/base/real_time_domain.h"
-#include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h"
-#include "platform/scheduler/renderer/auto_advancing_virtual_time_domain.h"
-#include "platform/scheduler/renderer/renderer_scheduler_impl.h"
-#include "platform/scheduler/renderer/web_frame_scheduler_impl.h"
-#include "platform/scheduler/renderer/web_view_scheduler_impl.h"
-#include "testing/gmock/include/gmock/gmock.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-using testing::ElementsAre;
-
-namespace blink {
-namespace scheduler {
-
-namespace {
-void RunTenTimesTask(size_t* count, scoped_refptr<TaskQueue> timer_queue) {
- if (++(*count) < 10) {
- timer_queue->PostTask(FROM_HERE,
- base::Bind(&RunTenTimesTask, count, timer_queue));
- }
-}
-}
-
-class ThrottlingHelperTest : public testing::Test {
- public:
- ThrottlingHelperTest() {}
- ~ThrottlingHelperTest() override {}
-
- void SetUp() override {
- clock_.reset(new base::SimpleTestTickClock());
- clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
- mock_task_runner_ =
- make_scoped_refptr(new cc::OrderedSimpleTaskRunner(clock_.get(), true));
- delegate_ = SchedulerTqmDelegateForTest::Create(
- mock_task_runner_, base::MakeUnique<TestTimeSource>(clock_.get()));
- scheduler_.reset(new RendererSchedulerImpl(delegate_));
- throttling_helper_ = scheduler_->throttling_helper();
- timer_queue_ = scheduler_->NewTimerTaskRunner("test_queue");
- }
-
- void TearDown() override {
- scheduler_->Shutdown();
- scheduler_.reset();
- }
-
- void ExpectThrottled(scoped_refptr<TaskQueue> timer_queue) {
- size_t count = 0;
- timer_queue->PostTask(FROM_HERE,
- base::Bind(&RunTenTimesTask, &count, timer_queue));
-
- mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1));
- EXPECT_LE(count, 1u);
-
- // Make sure the rest of the tasks run or we risk a UAF on |count|.
- mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(10));
- EXPECT_EQ(10u, count);
- }
-
- void ExpectUnthrottled(scoped_refptr<TaskQueue> timer_queue) {
- size_t count = 0;
- timer_queue->PostTask(FROM_HERE,
- base::Bind(&RunTenTimesTask, &count, timer_queue));
-
- mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1));
- EXPECT_EQ(10u, count);
- mock_task_runner_->RunUntilIdle();
- }
-
- protected:
- std::unique_ptr<base::SimpleTestTickClock> clock_;
- scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
- scoped_refptr<SchedulerTqmDelegate> delegate_;
- std::unique_ptr<RendererSchedulerImpl> scheduler_;
- scoped_refptr<TaskQueue> timer_queue_;
- ThrottlingHelper* throttling_helper_; // NOT OWNED
-
- DISALLOW_COPY_AND_ASSIGN(ThrottlingHelperTest);
-};
-
-TEST_F(ThrottlingHelperTest, ThrottledRunTime) {
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.0)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.1)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.2)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.5)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.8)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(0.9)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(2.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(2.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(1.1)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(9.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(8.0)));
-
- EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromSecondsD(9.0),
- ThrottlingHelper::ThrottledRunTime(
- base::TimeTicks() + base::TimeDelta::FromSecondsD(8.1)));
-}
-
-namespace {
-void TestTask(std::vector<base::TimeTicks>* run_times,
- base::SimpleTestTickClock* clock) {
- run_times->push_back(clock->NowTicks());
-}
-} // namespace
-
-TEST_F(ThrottlingHelperTest, TimerAlignment) {
- std::vector<base::TimeTicks> run_times;
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(200.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(800.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(1200.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(8300.0));
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- mock_task_runner_->RunUntilIdle();
-
- // Times are aligned to a multipple of 1000 milliseconds.
- EXPECT_THAT(
- run_times,
- ElementsAre(
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(1000.0),
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(1000.0),
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(2000.0),
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(9000.0)));
-}
-
-TEST_F(ThrottlingHelperTest, TimerAlignment_Unthrottled) {
- std::vector<base::TimeTicks> run_times;
- base::TimeTicks start_time = clock_->NowTicks();
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(200.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(800.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(1200.0));
-
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(8300.0));
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
-
- mock_task_runner_->RunUntilIdle();
-
- // Times are not aligned.
- EXPECT_THAT(
- run_times,
- ElementsAre(start_time + base::TimeDelta::FromMilliseconds(200.0),
- start_time + base::TimeDelta::FromMilliseconds(800.0),
- start_time + base::TimeDelta::FromMilliseconds(1200.0),
- start_time + base::TimeDelta::FromMilliseconds(8300.0)));
-}
-
-TEST_F(ThrottlingHelperTest, Refcount) {
- ExpectUnthrottled(timer_queue_.get());
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- ExpectThrottled(timer_queue_);
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- ExpectThrottled(timer_queue_);
-
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- ExpectThrottled(timer_queue_);
-
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- ExpectUnthrottled(timer_queue_);
-
- // Should be a NOP.
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- ExpectUnthrottled(timer_queue_);
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- ExpectThrottled(timer_queue_);
-}
-
-TEST_F(ThrottlingHelperTest,
- ThrotlingAnEmptyQueueDoesNotPostPumpThrottledTasksLocked) {
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- EXPECT_TRUE(throttling_helper_->task_runner()->IsEmpty());
-}
-
-TEST_F(ThrottlingHelperTest, WakeUpForNonDelayedTask) {
- std::vector<base::TimeTicks> run_times;
-
- // Nothing is posted on timer_queue_ so PumpThrottledTasks will not tick.
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- // Posting a task should trigger the pump.
- timer_queue_->PostTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()));
-
- mock_task_runner_->RunUntilIdle();
- EXPECT_THAT(run_times,
- ElementsAre(base::TimeTicks() +
- base::TimeDelta::FromMilliseconds(1000.0)));
-}
-
-TEST_F(ThrottlingHelperTest, WakeUpForDelayedTask) {
- std::vector<base::TimeTicks> run_times;
-
- // Nothing is posted on timer_queue_ so PumpThrottledTasks will not tick.
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- // Posting a task should trigger the pump.
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(1200.0));
-
- mock_task_runner_->RunUntilIdle();
- EXPECT_THAT(run_times,
- ElementsAre(base::TimeTicks() +
- base::TimeDelta::FromMilliseconds(2000.0)));
-}
-
-namespace {
-bool MessageLoopTaskCounter(size_t* count) {
- *count = *count + 1;
- return true;
-}
-
-void NopTask() {}
-
-void AddOneTask(size_t* count) {
- (*count)++;
-}
-
-} // namespace
-
-TEST_F(ThrottlingHelperTest,
- SingleThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) {
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10));
- timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay);
-
- size_t task_count = 0;
- mock_task_runner_->RunTasksWhile(
- base::Bind(&MessageLoopTaskCounter, &task_count));
-
- // Run the task.
- EXPECT_EQ(1u, task_count);
-}
-
-TEST_F(ThrottlingHelperTest,
- SingleFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) {
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5));
- timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay);
-
- size_t task_count = 0;
- mock_task_runner_->RunTasksWhile(
- base::Bind(&MessageLoopTaskCounter, &task_count));
-
- // Run the delayed task.
- EXPECT_EQ(1u, task_count);
-}
-
-TEST_F(ThrottlingHelperTest,
- TwoFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) {
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- std::vector<base::TimeTicks> run_times;
-
- base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5));
- timer_queue_->PostDelayedTask(
- FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay);
-
- base::TimeDelta delay2(base::TimeDelta::FromSecondsD(5.5));
- timer_queue_->PostDelayedTask(
- FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay2);
-
- size_t task_count = 0;
- mock_task_runner_->RunTasksWhile(
- base::Bind(&MessageLoopTaskCounter, &task_count));
-
- // Run both delayed tasks.
- EXPECT_EQ(2u, task_count);
-
- EXPECT_THAT(
- run_times,
- ElementsAre(base::TimeTicks() + base::TimeDelta::FromSeconds(6),
- base::TimeTicks() + base::TimeDelta::FromSeconds(16)));
-}
-
-TEST_F(ThrottlingHelperTest, TaskDelayIsBasedOnRealTime) {
- std::vector<base::TimeTicks> run_times;
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
-
- // Post an initial task that should run at the first aligned time period.
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(900.0));
-
- mock_task_runner_->RunUntilIdle();
-
- // Advance realtime.
- clock_->Advance(base::TimeDelta::FromMilliseconds(250));
-
- // Post a task that due to real time + delay must run in the third aligned
- // time period.
- timer_queue_->PostDelayedTask(FROM_HERE,
- base::Bind(&TestTask, &run_times, clock_.get()),
- base::TimeDelta::FromMilliseconds(900.0));
-
- mock_task_runner_->RunUntilIdle();
-
- EXPECT_THAT(
- run_times,
- ElementsAre(
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(1000.0),
- base::TimeTicks() + base::TimeDelta::FromMilliseconds(3000.0)));
-}
-
-TEST_F(ThrottlingHelperTest, ThrottledTasksReportRealTime) {
- EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks());
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks());
-
- clock_->Advance(base::TimeDelta::FromMilliseconds(250));
- // Make sure the throttled time domain's Now() reports the same as the
- // underlying clock.
- EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks());
-}
-
-TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump) {
- size_t count = 0;
- timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count));
-
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- mock_task_runner_->RunUntilIdle(); // Wait until the pump.
- EXPECT_EQ(1u, count); // The task got run
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyEnabled) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- timer_queue_->SetQueueEnabled(true); // NOP
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyDisabled) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- timer_queue_->SetQueueEnabled(false);
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, SetQueueEnabled_Unthrottled) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- throttling_helper_->SetQueueEnabled(timer_queue_.get(), false);
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- throttling_helper_->SetQueueEnabled(timer_queue_.get(), true);
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, SetQueueEnabled_DisabledWhileThrottled) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- throttling_helper_->SetQueueEnabled(timer_queue_.get(), false);
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump_ThenManuallyDisabled) {
- size_t count = 0;
- timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count));
-
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- mock_task_runner_->RunUntilIdle(); // Wait until the pump.
- EXPECT_EQ(1u, count); // Task ran
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
-
- throttling_helper_->SetQueueEnabled(timer_queue_.get(), false);
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, DoubleIncrementDoubleDecrement) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get());
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
-}
-
-TEST_F(ThrottlingHelperTest, EnableVirtualTimeThenIncrement) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- scheduler_->EnableVirtualTime();
- EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain());
-
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain());
-}
-
-TEST_F(ThrottlingHelperTest, IncrementThenEnableVirtualTime) {
- timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask));
-
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get());
- EXPECT_FALSE(timer_queue_->IsQueueEnabled());
-
- scheduler_->EnableVirtualTime();
- EXPECT_TRUE(timer_queue_->IsQueueEnabled());
- EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain());
-}
-
-} // namespace scheduler
-} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698