| 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 "components/scheduler/base/queueing_time_estimator.h" | |
| 6 #include "components/scheduler/base/test_time_source.h" | |
| 7 #include "testing/gmock/include/gmock/gmock.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace scheduler { | |
| 11 | |
| 12 using QueueingTimeEstimatorTest = testing::Test; | |
| 13 | |
| 14 class TestQueueingTimeEstimatorClient | |
| 15 : public QueueingTimeEstimator::Client { | |
| 16 public: | |
| 17 void OnQueueingTimeForWindowEstimated( | |
| 18 base::TimeDelta queueing_time) override { | |
| 19 expected_queueing_times_.push_back(queueing_time); | |
| 20 } | |
| 21 const std::vector<base::TimeDelta>& expected_queueing_times() { | |
| 22 return expected_queueing_times_; | |
| 23 } | |
| 24 | |
| 25 private: | |
| 26 std::vector<base::TimeDelta> expected_queueing_times_; | |
| 27 }; | |
| 28 | |
| 29 class QueueingTimeEstimatorForTest : public QueueingTimeEstimator { | |
| 30 public: | |
| 31 QueueingTimeEstimatorForTest(TestQueueingTimeEstimatorClient* client, | |
| 32 base::TimeDelta window_duration) | |
| 33 : QueueingTimeEstimator(client, window_duration) {} | |
| 34 }; | |
| 35 | |
| 36 // Three tasks of one second each, all within a 5 second window. Expected | |
| 37 // queueing time is the probability of falling into one of these tasks (3/5), | |
| 38 // multiplied by the expected queueing time within a task (0.5 seconds). Thus we | |
| 39 // expect a queueing time of 0.3 seconds. | |
| 40 TEST_F(QueueingTimeEstimatorTest, AllTasksWithinWindow) { | |
| 41 base::TimeTicks time; | |
| 42 TestQueueingTimeEstimatorClient client; | |
| 43 QueueingTimeEstimatorForTest estimator(&client, | |
| 44 base::TimeDelta::FromSeconds(5)); | |
| 45 for (int i = 0; i < 3; ++i) { | |
| 46 estimator.OnToplevelTaskCompleted( | |
| 47 time, time + base::TimeDelta::FromMilliseconds(1000)); | |
| 48 time += base::TimeDelta::FromMilliseconds(1500); | |
| 49 } | |
| 50 | |
| 51 // Flush the data by adding a task in the next window. | |
| 52 time += base::TimeDelta::FromMilliseconds(5000); | |
| 53 estimator.OnToplevelTaskCompleted( | |
| 54 time, time + base::TimeDelta::FromMilliseconds(500)); | |
| 55 | |
| 56 EXPECT_THAT(client.expected_queueing_times(), | |
| 57 testing::ElementsAre(base::TimeDelta::FromMilliseconds(300))); | |
| 58 } | |
| 59 | |
| 60 // One 20 second long task, starting 3 seconds into the first window. | |
| 61 // Window 1: Probability of being within task = 2/5. Expected delay within task: | |
| 62 // avg(20, 18). Total expected queueing time = 7.6s. | |
| 63 // Window 2: Probability of being within task = 1. Expected delay within task: | |
| 64 // avg(18, 13). Total expected queueing time = 15.5s. | |
| 65 // Window 5: Probability of being within task = 3/5. Expected delay within task: | |
| 66 // avg(3, 0). Total expected queueing time = 0.9s. | |
| 67 TEST_F(QueueingTimeEstimatorTest, MultiWindowTask) { | |
| 68 TestQueueingTimeEstimatorClient client; | |
| 69 QueueingTimeEstimatorForTest estimator(&client, | |
| 70 base::TimeDelta::FromSeconds(5)); | |
| 71 base::TimeTicks time; | |
| 72 time += base::TimeDelta::FromMilliseconds(5000); | |
| 73 estimator.OnToplevelTaskCompleted(time, time); | |
| 74 | |
| 75 time += base::TimeDelta::FromMilliseconds(3000); | |
| 76 | |
| 77 estimator.OnToplevelTaskCompleted( | |
| 78 time, time + base::TimeDelta::FromMilliseconds(20000)); | |
| 79 | |
| 80 // Flush the data by adding a task in the next window. | |
| 81 time += base::TimeDelta::FromMilliseconds(25000); | |
| 82 | |
| 83 estimator.OnToplevelTaskCompleted( | |
| 84 time, time + base::TimeDelta::FromMilliseconds(500)); | |
| 85 | |
| 86 EXPECT_THAT(client.expected_queueing_times(), | |
| 87 testing::ElementsAre(base::TimeDelta::FromMilliseconds(7600), | |
| 88 base::TimeDelta::FromMilliseconds(15500), | |
| 89 base::TimeDelta::FromMilliseconds(10500), | |
| 90 base::TimeDelta::FromMilliseconds(5500), | |
| 91 base::TimeDelta::FromMilliseconds(900))); | |
| 92 } | |
| 93 | |
| 94 } // namespace scheduler | |
| OLD | NEW |