OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "base/memory/scoped_ptr.h" |
| 6 #include "base/test/simple_test_tick_clock.h" |
| 7 #include "components/scheduler/base/test_time_source.h" |
| 8 #include "components/scheduler/renderer/queueing_time_estimator.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 // TODO(tdresser): fix test. |
| 13 |
| 14 namespace scheduler { |
| 15 |
| 16 class QueueingTimeEstimatorTest : public testing::Test { |
| 17 public: |
| 18 QueueingTimeEstimatorTest() {} |
| 19 ~QueueingTimeEstimatorTest() override {} |
| 20 |
| 21 void SetUp() override { |
| 22 test_time_source_.reset(new TestTimeSource(&clock_)); |
| 23 } |
| 24 |
| 25 base::SimpleTestTickClock clock_; |
| 26 scoped_ptr<TestTimeSource> test_time_source_; |
| 27 }; |
| 28 |
| 29 class TestQueueingTimeEstimatorClient |
| 30 : public QueueingTimeEstimator::Client { |
| 31 public: |
| 32 void OnQueueingTimeForWindowEstimated( |
| 33 base::TimeDelta queueing_time) override { |
| 34 expected_queueing_times_.push_back(queueing_time); |
| 35 } |
| 36 const std::vector<base::TimeDelta>& expected_queueing_times() { |
| 37 return expected_queueing_times_; |
| 38 } |
| 39 |
| 40 private: |
| 41 std::vector<base::TimeDelta> expected_queueing_times_; |
| 42 }; |
| 43 |
| 44 class QueueingTimeEstimatorForTest : public QueueingTimeEstimator { |
| 45 public: |
| 46 QueueingTimeEstimatorForTest(TestQueueingTimeEstimatorClient* client, |
| 47 TestTimeSource* test_time_source, |
| 48 base::TimeDelta window_duration) |
| 49 : QueueingTimeEstimator(client, test_time_source, window_duration) {} |
| 50 }; |
| 51 |
| 52 // Three tasks of one second each, all within a 5 second window. Expected |
| 53 // queueing time is the probability of falling into one of these tasks (3/5), |
| 54 // multiplied by the expected queueing time within a task (0.5 seconds). Thus we |
| 55 // expect a queueing time of 0.3 seconds. |
| 56 TEST_F(QueueingTimeEstimatorTest, AllTasksWithinWindow) { |
| 57 TestQueueingTimeEstimatorClient client; |
| 58 QueueingTimeEstimatorForTest estimator(&client, test_time_source_.get(), |
| 59 base::TimeDelta::FromSeconds(5)); |
| 60 base::PendingTask task(FROM_HERE, base::Closure()); |
| 61 |
| 62 for (int i = 0; i < 3; ++i) { |
| 63 estimator.WillProcessTask(task); |
| 64 clock_.Advance(base::TimeDelta::FromMilliseconds(1000)); |
| 65 estimator.DidProcessTask(task); |
| 66 |
| 67 clock_.Advance(base::TimeDelta::FromMilliseconds(500)); |
| 68 } |
| 69 |
| 70 // Flush the data by adding a task in the next window. |
| 71 clock_.Advance(base::TimeDelta::FromMilliseconds(5000)); |
| 72 estimator.WillProcessTask(task); |
| 73 clock_.Advance(base::TimeDelta::FromMilliseconds(500)); |
| 74 estimator.DidProcessTask(task); |
| 75 |
| 76 EXPECT_THAT(client.expected_queueing_times(), |
| 77 testing::ElementsAre(base::TimeDelta::FromMilliseconds(300))); |
| 78 } |
| 79 |
| 80 // One 20 second long task, starting 3 seconds into the first window. |
| 81 // Window 1: Probability of being within task = 2/5. Expected delay within task: |
| 82 // avg(20, 18). Total expected queueing time = 7.6s. |
| 83 // Window 2: Probability of being within task = 1. Expected delay within task: |
| 84 // avg(18, 13). Total expected queueing time = 15.5s. |
| 85 // Window 5: Probability of being within task = 3/5. Expected delay within task: |
| 86 // avg(3, 0). Total expected queueing time = 0.9s. |
| 87 TEST_F(QueueingTimeEstimatorTest, MultiWindowTask) { |
| 88 TestQueueingTimeEstimatorClient client; |
| 89 QueueingTimeEstimatorForTest estimator(&client, test_time_source_.get(), |
| 90 base::TimeDelta::FromSeconds(5)); |
| 91 base::PendingTask task(FROM_HERE, base::Closure()); |
| 92 |
| 93 estimator.WillProcessTask(task); |
| 94 clock_.Advance(base::TimeDelta::FromMilliseconds(0)); |
| 95 estimator.DidProcessTask(task); |
| 96 |
| 97 clock_.Advance(base::TimeDelta::FromMilliseconds(3000)); |
| 98 |
| 99 estimator.WillProcessTask(task); |
| 100 clock_.Advance(base::TimeDelta::FromMilliseconds(20000)); |
| 101 estimator.DidProcessTask(task); |
| 102 |
| 103 // Flush the data by adding a task in the next window. |
| 104 clock_.Advance(base::TimeDelta::FromMilliseconds(5000)); |
| 105 estimator.WillProcessTask(task); |
| 106 clock_.Advance(base::TimeDelta::FromMilliseconds(500)); |
| 107 estimator.DidProcessTask(task); |
| 108 |
| 109 EXPECT_THAT(client.expected_queueing_times(), |
| 110 testing::ElementsAre(base::TimeDelta::FromMilliseconds(7600), |
| 111 base::TimeDelta::FromMilliseconds(15500), |
| 112 base::TimeDelta::FromMilliseconds(10500), |
| 113 base::TimeDelta::FromMilliseconds(5500), |
| 114 base::TimeDelta::FromMilliseconds(900))); |
| 115 } |
| 116 |
| 117 // A single task with a nested message loop. The top level task lasts 4 seconds. |
| 118 // Probability of landing within this task = 4/5, expected queueing time within |
| 119 // this task = 2s. Total expected queueing time = 1.6. |
| 120 TEST_F(QueueingTimeEstimatorTest, NestedRunLoop) { |
| 121 TestQueueingTimeEstimatorClient client; |
| 122 QueueingTimeEstimatorForTest estimator(&client, test_time_source_.get(), |
| 123 base::TimeDelta::FromSeconds(5)); |
| 124 base::PendingTask task(FROM_HERE, base::Closure()); |
| 125 |
| 126 // Make sure we ignore the tasks inside the nested run loop. |
| 127 estimator.WillProcessTask(task); |
| 128 estimator.WillProcessTask(task); |
| 129 clock_.Advance(base::TimeDelta::FromMilliseconds(2000)); |
| 130 estimator.DidProcessTask(task); |
| 131 clock_.Advance(base::TimeDelta::FromMilliseconds(2000)); |
| 132 estimator.DidProcessTask(task); |
| 133 |
| 134 // Flush the data by adding a task in the next window. |
| 135 clock_.Advance(base::TimeDelta::FromMilliseconds(5000)); |
| 136 estimator.WillProcessTask(task); |
| 137 clock_.Advance(base::TimeDelta::FromMilliseconds(500)); |
| 138 estimator.DidProcessTask(task); |
| 139 |
| 140 EXPECT_THAT(client.expected_queueing_times(), |
| 141 testing::ElementsAre(base::TimeDelta::FromMilliseconds(1600))); |
| 142 } |
| 143 |
| 144 } // namespace scheduler |
OLD | NEW |