| 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/output/begin_frame_args.h" |
| 9 #include "components/scheduler/base/test_time_source.h" |
| 10 |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace scheduler { |
| 15 |
| 16 class IdleTimeEstimatorForTest : public IdleTimeEstimator { |
| 17 public: |
| 18 IdleTimeEstimatorForTest(base::SimpleTestTickClock* clock, |
| 19 int sample_count, |
| 20 double estimation_percentile) |
| 21 : IdleTimeEstimator(sample_count, estimation_percentile) { |
| 22 SetTimeSourceForTesting(make_scoped_ptr(new TestTimeSource(clock))); |
| 23 } |
| 24 }; |
| 25 |
| 26 class IdleTimeEstimatorTest : public testing::Test { |
| 27 public: |
| 28 IdleTimeEstimatorTest() {} |
| 29 ~IdleTimeEstimatorTest() override {} |
| 30 |
| 31 void SetUp() override { |
| 32 estimator_.reset(new IdleTimeEstimatorForTest(&clock_, 10, 50)); |
| 33 } |
| 34 |
| 35 base::SimpleTestTickClock clock_; |
| 36 |
| 37 void Frame(int compositor_time, int idle_time) { |
| 38 base::PendingTask task(FROM_HERE, base::Closure()); |
| 39 estimator_->WillProcessTask(task); |
| 40 clock_.Advance(base::TimeDelta::FromMilliseconds(compositor_time)); |
| 41 estimator_->DidCommitFrameToCompositor(); |
| 42 estimator_->DidProcessTask(task); |
| 43 clock_.Advance(base::TimeDelta::FromMilliseconds(idle_time)); |
| 44 } |
| 45 |
| 46 void FrameWithTwoCompositorTasks(int compositor_time1, |
| 47 int compositor_time2, |
| 48 int idle_time) { |
| 49 base::PendingTask task(FROM_HERE, base::Closure()); |
| 50 estimator_->WillProcessTask(task); |
| 51 clock_.Advance(base::TimeDelta::FromMilliseconds(compositor_time1)); |
| 52 estimator_->DidProcessTask(task); |
| 53 |
| 54 estimator_->WillProcessTask(task); |
| 55 clock_.Advance(base::TimeDelta::FromMilliseconds(compositor_time2)); |
| 56 estimator_->DidCommitFrameToCompositor(); |
| 57 estimator_->DidProcessTask(task); |
| 58 clock_.Advance(base::TimeDelta::FromMilliseconds(idle_time)); |
| 59 } |
| 60 |
| 61 scoped_ptr<IdleTimeEstimatorForTest> estimator_; |
| 62 }; |
| 63 |
| 64 TEST_F(IdleTimeEstimatorTest, InitialTimeEstimateWithNoData) { |
| 65 EXPECT_EQ(cc::BeginFrameArgs::DefaultInterval(), |
| 66 estimator_->GetExpectedIdleDuration( |
| 67 cc::BeginFrameArgs::DefaultInterval() * 2)); |
| 68 } |
| 69 |
| 70 TEST_F(IdleTimeEstimatorTest, BasicEstimation_SteadyState) { |
| 71 Frame(5, 11); |
| 72 Frame(5, 11); |
| 73 |
| 74 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 75 estimator_->GetExpectedIdleDuration( |
| 76 cc::BeginFrameArgs::DefaultInterval())); |
| 77 } |
| 78 |
| 79 TEST_F(IdleTimeEstimatorTest, BasicEstimation_Variable) { |
| 80 Frame(5, 11); |
| 81 Frame(6, 10); |
| 82 Frame(7, 9); |
| 83 Frame(7, 9); |
| 84 Frame(7, 9); |
| 85 Frame(8, 8); |
| 86 |
| 87 // We expect it to return the median. |
| 88 EXPECT_EQ(base::TimeDelta::FromMilliseconds(9), |
| 89 estimator_->GetExpectedIdleDuration( |
| 90 base::TimeDelta::FromMilliseconds(16))); |
| 91 } |
| 92 |
| 93 TEST_F(IdleTimeEstimatorTest, Clear) { |
| 94 Frame(5, 11); |
| 95 Frame(5, 11); |
| 96 |
| 97 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 98 estimator_->GetExpectedIdleDuration( |
| 99 cc::BeginFrameArgs::DefaultInterval())); |
| 100 estimator_->Clear(); |
| 101 |
| 102 EXPECT_EQ(cc::BeginFrameArgs::DefaultInterval(), |
| 103 estimator_->GetExpectedIdleDuration( |
| 104 cc::BeginFrameArgs::DefaultInterval())); |
| 105 } |
| 106 |
| 107 TEST_F(IdleTimeEstimatorTest, Estimation_MultipleTasks) { |
| 108 FrameWithTwoCompositorTasks(1, 4, 11); |
| 109 FrameWithTwoCompositorTasks(1, 4, 11); |
| 110 |
| 111 EXPECT_EQ(base::TimeDelta::FromMilliseconds(11), |
| 112 estimator_->GetExpectedIdleDuration( |
| 113 cc::BeginFrameArgs::DefaultInterval())); |
| 114 } |
| 115 |
| 116 TEST_F(IdleTimeEstimatorTest, IdlePeriodClamping) { |
| 117 Frame(5, 100); |
| 118 Frame(5, 100); |
| 119 Frame(5, 100); |
| 120 |
| 121 EXPECT_EQ(base::TimeDelta::FromMilliseconds(32 - 5), |
| 122 estimator_->GetExpectedIdleDuration( |
| 123 base::TimeDelta::FromMilliseconds(32))); |
| 124 } |
| 125 |
| 126 } // namespace scheduler |
| OLD | NEW |