| 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/renderer/idle_time_estimator.h" |
| 6 |
| 7 #include "base/test/simple_test_tick_clock.h" |
| 8 #include "cc/test/ordered_simple_task_runner.h" |
| 9 #include "components/scheduler/base/task_queue_manager.h" |
| 10 #include "components/scheduler/base/test_time_source.h" |
| 11 #include "components/scheduler/child/scheduler_task_runner_delegate_for_test.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace scheduler { |
| 16 |
| 17 class IdleTimeEstimatorForTest : public IdleTimeEstimator { |
| 18 public: |
| 19 IdleTimeEstimatorForTest( |
| 20 const scoped_refptr<TaskQueue>& compositor_task_runner, |
| 21 base::SimpleTestTickClock* clock, |
| 22 int sample_count, |
| 23 double estimation_percentile) |
| 24 : IdleTimeEstimator(compositor_task_runner, |
| 25 sample_count, |
| 26 estimation_percentile) { |
| 27 SetTimeSourceForTesting(make_scoped_ptr(new TestTimeSource(clock))); |
| 28 } |
| 29 }; |
| 30 |
| 31 class IdleTimeEstimatorTest : public testing::Test { |
| 32 public: |
| 33 IdleTimeEstimatorTest() |
| 34 : frame_length_(base::TimeDelta::FromMilliseconds(16)) {} |
| 35 |
| 36 ~IdleTimeEstimatorTest() override {} |
| 37 |
| 38 void SetUp() override { |
| 39 clock_.reset(new base::SimpleTestTickClock()); |
| 40 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
| 41 mock_task_runner_ = make_scoped_refptr( |
| 42 new cc::OrderedSimpleTaskRunner(clock_.get(), false)); |
| 43 main_task_runner_ = |
| 44 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_); |
| 45 manager_ = make_scoped_ptr(new TaskQueueManager( |
| 46 main_task_runner_, "test.scheduler", "test.scheduler.debug")); |
| 47 compositor_task_runner_ = |
| 48 manager_->NewTaskQueue(TaskQueue::Spec("compositor_tq")); |
| 49 estimator_.reset(new IdleTimeEstimatorForTest(compositor_task_runner_, |
| 50 clock_.get(), 10, 50)); |
| 51 } |
| 52 |
| 53 void SimulateFrameWithOneCompositorTask(int compositor_time) { |
| 54 base::TimeDelta non_idle_time = |
| 55 base::TimeDelta::FromMilliseconds(compositor_time); |
| 56 base::PendingTask task(FROM_HERE, base::Closure()); |
| 57 estimator_->WillProcessTask(task); |
| 58 clock_->Advance(non_idle_time); |
| 59 estimator_->DidCommitFrameToCompositor(); |
| 60 estimator_->DidProcessTask(task); |
| 61 if (non_idle_time < frame_length_) |
| 62 clock_->Advance(frame_length_ - non_idle_time); |
| 63 } |
| 64 |
| 65 void SimulateFrameWithTwoCompositorTasks(int compositor_time1, |
| 66 int compositor_time2) { |
| 67 base::TimeDelta non_idle_time1 = |
| 68 base::TimeDelta::FromMilliseconds(compositor_time1); |
| 69 base::TimeDelta non_idle_time2 = |
| 70 base::TimeDelta::FromMilliseconds(compositor_time2); |
| 71 base::PendingTask task(FROM_HERE, base::Closure()); |
| 72 estimator_->WillProcessTask(task); |
| 73 clock_->Advance(non_idle_time1); |
| 74 estimator_->DidProcessTask(task); |
| 75 |
| 76 estimator_->WillProcessTask(task); |
| 77 clock_->Advance(non_idle_time2); |
| 78 estimator_->DidCommitFrameToCompositor(); |
| 79 estimator_->DidProcessTask(task); |
| 80 |
| 81 base::TimeDelta idle_time = frame_length_ - non_idle_time1 - non_idle_time2; |
| 82 clock_->Advance(idle_time); |
| 83 } |
| 84 |
| 85 scoped_ptr<base::SimpleTestTickClock> clock_; |
| 86 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 87 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner_; |
| 88 scoped_ptr<TaskQueueManager> manager_; |
| 89 scoped_refptr<TaskQueue> compositor_task_runner_; |
| 90 scoped_ptr<IdleTimeEstimatorForTest> estimator_; |
| 91 const base::TimeDelta frame_length_; |
| 92 }; |
| 93 |
| 94 TEST_F(IdleTimeEstimatorTest, InitialTimeEstimateWithNoData) { |
| 95 EXPECT_EQ(frame_length_, estimator_->GetExpectedIdleDuration(frame_length_)); |
| 96 } |
| 97 |
| 98 TEST_F(IdleTimeEstimatorTest, BasicEstimation_SteadyState) { |
| 99 SimulateFrameWithOneCompositorTask(5); |
| 100 SimulateFrameWithOneCompositorTask(5); |
| 101 |
| 102 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 103 estimator_->GetExpectedIdleDuration(frame_length_)); |
| 104 } |
| 105 |
| 106 TEST_F(IdleTimeEstimatorTest, BasicEstimation_Variable) { |
| 107 SimulateFrameWithOneCompositorTask(5); |
| 108 SimulateFrameWithOneCompositorTask(6); |
| 109 SimulateFrameWithOneCompositorTask(7); |
| 110 SimulateFrameWithOneCompositorTask(7); |
| 111 SimulateFrameWithOneCompositorTask(7); |
| 112 SimulateFrameWithOneCompositorTask(8); |
| 113 |
| 114 // We expect it to return the median. |
| 115 EXPECT_EQ(base::TimeDelta::FromMilliseconds(9), |
| 116 estimator_->GetExpectedIdleDuration(frame_length_)); |
| 117 } |
| 118 |
| 119 TEST_F(IdleTimeEstimatorTest, NoIdleTime) { |
| 120 SimulateFrameWithOneCompositorTask(100); |
| 121 SimulateFrameWithOneCompositorTask(100); |
| 122 |
| 123 EXPECT_EQ(base::TimeDelta::FromMilliseconds(0), |
| 124 estimator_->GetExpectedIdleDuration(frame_length_)); |
| 125 } |
| 126 |
| 127 TEST_F(IdleTimeEstimatorTest, Clear) { |
| 128 SimulateFrameWithOneCompositorTask(5); |
| 129 SimulateFrameWithOneCompositorTask(5); |
| 130 |
| 131 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 132 estimator_->GetExpectedIdleDuration(frame_length_)); |
| 133 estimator_->Clear(); |
| 134 |
| 135 EXPECT_EQ(frame_length_, estimator_->GetExpectedIdleDuration(frame_length_)); |
| 136 } |
| 137 |
| 138 TEST_F(IdleTimeEstimatorTest, Estimation_MultipleTasks) { |
| 139 SimulateFrameWithTwoCompositorTasks(1, 4); |
| 140 SimulateFrameWithTwoCompositorTasks(1, 4); |
| 141 |
| 142 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 143 estimator_->GetExpectedIdleDuration(frame_length_)); |
| 144 } |
| 145 |
| 146 } // namespace scheduler |
| OLD | NEW |