Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 5 | #include "cc/metrics/average_lag_tracker.h" |
| 6 | |
| 7 | #include <memory> |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 8 | |
| 9 | #include "base/test/metrics/histogram_tester.h" |
| 10 | #include "testing/gmock/include/gmock/gmock.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | using base::Bucket; |
| 14 | using testing::ElementsAre; |
| 15 | |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 16 | namespace cc { |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | class AverageLagTrackerTest : public testing::Test { |
| 20 | public: |
| 21 | AverageLagTrackerTest() { ResetHistograms(); } |
| 22 | |
| 23 | void ResetHistograms() { |
Peter Boström | f238104 | 2019-08-27 00:25:03 | [diff] [blame] | 24 | histogram_tester_ = std::make_unique<base::HistogramTester>(); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | const base::HistogramTester& histogram_tester() { return *histogram_tester_; } |
| 28 | |
| 29 | void SetUp() override { |
Joao Victor Almeida | ae356d4 | 2021-07-06 16:24:34 | [diff] [blame^] | 30 | average_lag_tracker_ = std::make_unique<AverageLagTracker>(); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 31 | } |
| 32 | |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 33 | void SyntheticTouchScrollBegin(base::TimeTicks event_time, |
| 34 | base::TimeTicks frame_time, |
| 35 | float delta, |
| 36 | float predicted_delta = 0) { |
| 37 | AverageLagTracker::EventInfo event_info( |
| 38 | 0, delta, predicted_delta != 0 ? predicted_delta : delta, event_time, |
| 39 | AverageLagTracker::EventType::ScrollBegin); |
| 40 | event_info.finish_timestamp = frame_time; |
Joao Victor Almeida | ae356d4 | 2021-07-06 16:24:34 | [diff] [blame^] | 41 | average_lag_tracker_->AddScrollEventInFrame(event_info); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 42 | } |
| 43 | |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 44 | void SyntheticTouchScrollUpdate(base::TimeTicks event_time, |
| 45 | base::TimeTicks frame_time, |
| 46 | float delta, |
| 47 | float predicted_delta = 0) { |
| 48 | AverageLagTracker::EventInfo event_info( |
| 49 | 0, delta, predicted_delta != 0 ? predicted_delta : delta, event_time, |
| 50 | AverageLagTracker::EventType::ScrollUpdate); |
| 51 | event_info.finish_timestamp = frame_time; |
Joao Victor Almeida | ae356d4 | 2021-07-06 16:24:34 | [diff] [blame^] | 52 | average_lag_tracker_->AddScrollEventInFrame(event_info); |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CheckScrollBeginHistograms(int bucket_value, int count) { |
| 56 | EXPECT_THAT(histogram_tester().GetAllSamples( |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 57 | "Event.Latency.ScrollBegin.Touch.AverageLagPresentation"), |
| 58 | ElementsAre(Bucket(bucket_value, count))); |
| 59 | } |
| 60 | |
| 61 | void CheckScrollUpdateHistograms(int bucket_value, int count) { |
| 62 | EXPECT_THAT(histogram_tester().GetAllSamples( |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 63 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation"), |
| 64 | ElementsAre(Bucket(bucket_value, count))); |
| 65 | } |
| 66 | |
| 67 | void CheckPredictionPositiveHistograms(int bucket_value, int count) { |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 68 | EXPECT_THAT(histogram_tester().GetAllSamples( |
| 69 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation." |
| 70 | "PredictionPositive"), |
| 71 | ElementsAre(Bucket(bucket_value, count))); |
| 72 | } |
| 73 | |
| 74 | void CheckPredictionNegativeHistograms(int bucket_value, int count) { |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 75 | EXPECT_THAT(histogram_tester().GetAllSamples( |
| 76 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation." |
| 77 | "PredictionNegative"), |
| 78 | ElementsAre(Bucket(bucket_value, count))); |
| 79 | } |
| 80 | |
| 81 | void CheckScrollUpdateHistogramsTotalCount(int count) { |
| 82 | histogram_tester().ExpectTotalCount( |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 83 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation", count); |
| 84 | } |
| 85 | |
| 86 | void CheckPredictionPositiveHistogramsTotalCount(int count) { |
| 87 | histogram_tester().ExpectTotalCount( |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 88 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation." |
| 89 | "PredictionPositive", |
| 90 | count); |
| 91 | } |
| 92 | |
| 93 | void CheckPredictionNegativeHistogramsTotalCount(int count) { |
| 94 | histogram_tester().ExpectTotalCount( |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 95 | "Event.Latency.ScrollUpdate.Touch.AverageLagPresentation." |
| 96 | "PredictionNegative", |
| 97 | count); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | protected: |
Joao Victor Almeida | ae356d4 | 2021-07-06 16:24:34 | [diff] [blame^] | 101 | std::unique_ptr<AverageLagTracker> average_lag_tracker_; |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 102 | |
| 103 | std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 104 | }; |
| 105 | |
| 106 | base::TimeTicks MillisecondsToTimeTicks(float t_ms) { |
| 107 | return base::TimeTicks() + base::TimeDelta::FromMilliseconds(t_ms); |
| 108 | } |
| 109 | |
| 110 | // Simulate a simple situation that events at every 10ms and start at t=15ms, |
| 111 | // frame swaps at every 10ms too and start at t=20ms and test we record one |
| 112 | // UMA for ScrollUpdate in one second. |
| 113 | TEST_F(AverageLagTrackerTest, OneSecondInterval) { |
| 114 | base::TimeTicks event_time = |
| 115 | base::TimeTicks() + base::TimeDelta::FromMilliseconds(5); |
| 116 | base::TimeTicks frame_time = |
| 117 | base::TimeTicks() + base::TimeDelta::FromMilliseconds(10); |
| 118 | float scroll_delta = 10; |
| 119 | |
| 120 | // ScrollBegin |
| 121 | event_time += base::TimeDelta::FromMilliseconds(10); // 15ms |
| 122 | frame_time += base::TimeDelta::FromMilliseconds(10); // 20ms |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 123 | SyntheticTouchScrollBegin(event_time, frame_time, scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 124 | |
| 125 | // Send 101 ScrollUpdate events to verify that there is 1 AverageLag record |
| 126 | // per 1 second. |
| 127 | const int kUpdates = 101; |
| 128 | for (int i = 0; i < kUpdates; i++) { |
| 129 | event_time += base::TimeDelta::FromMilliseconds(10); |
| 130 | frame_time += base::TimeDelta::FromMilliseconds(10); |
| 131 | // First 50 has positive delta, others negetive delta. |
| 132 | const int sign = (i < kUpdates / 2) ? 1 : -1; |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 133 | SyntheticTouchScrollUpdate(event_time, frame_time, sign * scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // ScrollBegin report_time is at 20ms, so the next ScrollUpdate report_time is |
| 137 | // at 1020ms. The last event_time that finish this report should be later than |
| 138 | // 1020ms. |
| 139 | EXPECT_EQ(event_time, |
| 140 | base::TimeTicks() + base::TimeDelta::FromMilliseconds(1025)); |
| 141 | EXPECT_EQ(frame_time, |
| 142 | base::TimeTicks() + base::TimeDelta::FromMilliseconds(1030)); |
| 143 | |
| 144 | // ScrollBegin AverageLag are the area between the event original component |
| 145 | // (time=15ms, delta=10px) to the frame swap time (time=20ms, expect finger |
| 146 | // position at delta=15px). The AverageLag scaled to 1 second is |
| 147 | // (0.5*(10px+15px)*5ms)/5ms = 12.5px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 148 | CheckScrollBeginHistograms(12, 1); |
| 149 | |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 150 | // This ScrollUpdate AverageLag are calculated as the finger uniformly scroll |
| 151 | // 10px each frame. For scroll up/down frame, the Lag at the last frame swap |
| 152 | // is 5px, and Lag at this frame swap is 15px. For the one changing direction, |
| 153 | // the Lag is from 5 to 10 and down to 5 again. So total LagArea is 99 * 100, |
| 154 | // plus 75. the AverageLag in 1 second is 9.975px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 155 | CheckScrollUpdateHistograms(9, 1); |
| 156 | CheckPredictionPositiveHistograms(0, 1); |
| 157 | CheckPredictionNegativeHistogramsTotalCount(0); |
| 158 | |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 159 | ResetHistograms(); |
| 160 | |
| 161 | // Send another ScrollBegin to end the unfinished ScrollUpdate report. |
| 162 | event_time += base::TimeDelta::FromMilliseconds(10); |
| 163 | frame_time += base::TimeDelta::FromMilliseconds(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 164 | SyntheticTouchScrollBegin(event_time, frame_time, scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 165 | |
| 166 | // The last ScrollUpdate's lag is 8.75px and truncated to 8. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 167 | CheckScrollUpdateHistograms(8, 1); |
| 168 | CheckPredictionPositiveHistograms(0, 1); |
| 169 | CheckPredictionNegativeHistogramsTotalCount(0); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Test the case that event's frame swap time is later than next event's |
| 173 | // creation time. (i.e, event at t=10ms will be dispatch at t=30ms, while next |
| 174 | // event is at t=20ms). |
| 175 | TEST_F(AverageLagTrackerTest, LargerLatency) { |
| 176 | base::TimeTicks event_time = MillisecondsToTimeTicks(10); |
| 177 | base::TimeTicks frame_time = |
| 178 | event_time + base::TimeDelta::FromMilliseconds(20); |
| 179 | float scroll_delta = 10; |
| 180 | |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 181 | SyntheticTouchScrollBegin(event_time, frame_time, scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 182 | |
| 183 | // Send 2 ScrollUpdate. The second one will record AverageLag.ScrollBegin as |
| 184 | // it's event_time is larger or equal to ScrollBegin's frame_time. |
| 185 | for (int i = 0; i < 2; i++) { |
| 186 | event_time += base::TimeDelta::FromMilliseconds(10); |
| 187 | frame_time = event_time + base::TimeDelta::FromMilliseconds(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 188 | SyntheticTouchScrollUpdate(event_time, frame_time, scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // ScrollBegin AveragLag are from t=10ms to t=30ms, with absolute scroll |
| 192 | // position from 10 to 30. The AverageLag should be: |
| 193 | // (0.5*(10px + 30px)*20ms/20ms) = 20px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 194 | CheckScrollBeginHistograms(20, 1); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 195 | |
| 196 | // Another ScrollBegin to flush unfinished frames. |
| 197 | // event_time doesn't matter here because the previous frames' lag are |
| 198 | // compute from their frame_time. |
| 199 | event_time = MillisecondsToTimeTicks(1000); |
| 200 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 201 | SyntheticTouchScrollBegin(event_time, frame_time, scroll_delta); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 202 | // The to unfinished frames' lag are (finger_positon-rendered_position)*time, |
| 203 | // AverageLag is ((30px-10px)*10ms+(30px-20px)*10ms)/20ms = 15px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 204 | CheckScrollUpdateHistograms(14, 1); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Test that multiple latency being flush in the same frame swap. |
| 208 | TEST_F(AverageLagTrackerTest, TwoLatencyInfoInSameFrame) { |
| 209 | // ScrollBegin |
| 210 | base::TimeTicks event_time = MillisecondsToTimeTicks(10); |
| 211 | base::TimeTicks frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 212 | SyntheticTouchScrollBegin(event_time, frame_time, -10 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 213 | |
| 214 | // ScrollUpdate with event_time >= ScrollBegin frame_time will generate |
| 215 | // a histogram for AverageLag.ScrollBegin. |
| 216 | event_time = MillisecondsToTimeTicks(20); |
| 217 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 218 | SyntheticTouchScrollUpdate(event_time, frame_time, -10 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 219 | |
| 220 | // Absolute position from -10 to -20. The AverageLag should be: |
| 221 | // (0.5*(10px + 20px)*10ms/10ms) = 15px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 222 | CheckScrollBeginHistograms(14, 1); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 223 | |
| 224 | event_time = MillisecondsToTimeTicks(25); |
| 225 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 226 | SyntheticTouchScrollUpdate(event_time, frame_time, 5 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 227 | |
| 228 | // Another ScrollBegin to flush unfinished frames. |
| 229 | event_time = MillisecondsToTimeTicks(1000); |
| 230 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 231 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 232 | |
| 233 | // The ScrollUpdates are at t=20ms, finger_pos=-20px, rendered_pos=-10px, |
| 234 | // at t=25ms, finger_pos=-15px, rendered_pos=-10px; |
| 235 | // To t=30ms both events get flush. |
| 236 | // AverageLag is (0.5*(10px+5px)*5ms + 5px*5ms)/10ms = 6.25px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 237 | CheckScrollUpdateHistograms(6, 1); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Test the case that switching direction causes lag at current frame |
| 241 | // time and previous frame time are in different direction. |
| 242 | TEST_F(AverageLagTrackerTest, ChangeDirectionInFrame) { |
| 243 | // ScrollBegin |
| 244 | base::TimeTicks event_time = MillisecondsToTimeTicks(10); |
| 245 | base::TimeTicks frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 246 | SyntheticTouchScrollBegin(event_time, frame_time, 10 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 247 | |
| 248 | // At t=20, lag = 10px. |
| 249 | event_time = MillisecondsToTimeTicks(20); |
| 250 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 251 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 252 | |
| 253 | // At t=30, lag = -10px. |
| 254 | event_time = MillisecondsToTimeTicks(30); |
| 255 | frame_time = MillisecondsToTimeTicks(40); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 256 | SyntheticTouchScrollUpdate(event_time, frame_time, -20 /* scroll_delta */); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 257 | |
| 258 | // Another ScrollBegin to flush unfinished frames. |
| 259 | event_time = MillisecondsToTimeTicks(1000); |
| 260 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 261 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 262 | |
| 263 | // From t=20 to t=30, lag_area=2*(0.5*10px*5ms)=50px*ms. |
| 264 | // From t=30 to t=40, lag_area=20px*10ms=200px*ms |
| 265 | // AverageLag = (50+200)/20 = 12.5px. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 266 | CheckScrollUpdateHistograms(12, 1); |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 267 | } |
| 268 | |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 269 | // A simple case without scroll prediction to compare with the two with |
| 270 | // prediction cases below. |
| 271 | TEST_F(AverageLagTrackerTest, NoScrollPrediction) { |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 272 | // ScrollBegin, at t=5, finger_pos=5px. |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 273 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 274 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 275 | SyntheticTouchScrollBegin(event_time, frame_time, 5 /* scroll_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 276 | |
| 277 | // ScrollUpdate, at t=15, finger_pos=15px. |
| 278 | event_time = MillisecondsToTimeTicks(15); |
| 279 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 280 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 281 | |
| 282 | // ScrollUpdate, at t=25, finger_pos=25px. |
| 283 | event_time = MillisecondsToTimeTicks(25); |
| 284 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 285 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 286 | |
| 287 | // Another ScrollBegin to flush unfinished frames. |
| 288 | event_time = MillisecondsToTimeTicks(1000); |
| 289 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 290 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 291 | |
| 292 | // Prediction hasn't take affect on ScrollBegin so it'll stay the same. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 293 | CheckScrollBeginHistograms(7, 1); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 294 | // At t=10, finger_pos = 10px, rendered_pos = 5px. |
| 295 | // At t=20, finger_pos = 20px, rendered_pos = 15px. |
| 296 | // At t=30, finger_pos = 25px, rendered_pos = 25px. |
| 297 | // AverageLag = ((5px+15px)*10ms/2 + (5px+10px)*5ms/2 + 10px*5ms)/20ms |
| 298 | // = 9.375 |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 299 | CheckScrollUpdateHistograms(9, 1); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Test AverageLag with perfect scroll prediction. |
| 303 | TEST_F(AverageLagTrackerTest, ScrollPrediction) { |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 304 | // ScrollBegin, at t=5, finger_pos=5px. |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 305 | // Predict frame_time=10, predicted_pos = 10px. |
| 306 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 307 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 308 | SyntheticTouchScrollBegin(event_time, frame_time, 5 /* scroll_delta */, |
| 309 | 10 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 310 | |
| 311 | // ScrollUpdate, at t=15, finger_pos=15px. |
| 312 | // Predict frame_time=20, predicted_pos = 20px. |
| 313 | event_time = MillisecondsToTimeTicks(15); |
| 314 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 315 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 316 | 10 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 317 | |
| 318 | // ScrollUpdate, at t=25, finger_pos=25px. |
| 319 | // Predict frame_time=30, predicted_pos = 30px. |
| 320 | event_time = MillisecondsToTimeTicks(25); |
| 321 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 322 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 323 | 10 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 324 | |
| 325 | // Another ScrollBegin to flush unfinished frames. |
| 326 | event_time = MillisecondsToTimeTicks(1000); |
| 327 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 328 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 329 | |
| 330 | // Prediction hasn't take affect on ScrollBegin so it'll stay the same. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 331 | CheckScrollBeginHistograms(7, 1); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 332 | // At t=10, finger_pos = 10px, rendered_pos = 10px. |
| 333 | // At t=20, finger_pos = 20px, rendered_pos = 20px. |
| 334 | // At t=30, finger_pos = 25px, rendered_pos = 30px. |
| 335 | // AverageLag = ((0px+10px)*10ms/2 + (0px+5px)*10ms/2 + 5px*5ms)/20ms |
| 336 | // = 4.375px |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 337 | // AverageLag (w/o prediction) |
| 338 | // ((5px+15px)*10ms/2 + (5px+10px)*5ms/2 + 10px*5ms)/20ms |
| 339 | // = 9.375px |
| 340 | // Positive effect of prediction = 5px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 341 | CheckScrollUpdateHistograms(4, 1); |
| 342 | CheckPredictionPositiveHistograms(5, 1); |
| 343 | CheckPredictionNegativeHistogramsTotalCount(0); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Test AverageLag with imperfect scroll prediction. |
| 347 | TEST_F(AverageLagTrackerTest, ImperfectScrollPrediction) { |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 348 | // ScrollBegin, at t=5, finger_pos=5px. |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 349 | // Predict frame_time=10, predicted_pos(over) = 12px. |
| 350 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 351 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 352 | SyntheticTouchScrollBegin(event_time, frame_time, 5 /* scroll_delta */, |
| 353 | 12 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 354 | |
| 355 | // ScrollUpdate, at t=15, finger_pos=15px. |
| 356 | // Predict frame_time=20, predicted_pos(under) = 17px. |
| 357 | event_time = MillisecondsToTimeTicks(15); |
| 358 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 359 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 360 | 5 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 361 | |
| 362 | // ScrollUpdate, at t=25, finger_pos=25px. |
| 363 | // Predict frame_time=30, predicted_pos(over) = 31px. |
| 364 | event_time = MillisecondsToTimeTicks(25); |
| 365 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 366 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 367 | 14 /* predicted_delta */); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 368 | |
| 369 | // Another ScrollBegin to flush unfinished frames. |
| 370 | event_time = MillisecondsToTimeTicks(1000); |
| 371 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 372 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 373 | |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 374 | CheckScrollBeginHistograms(7, 1); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 375 | // AverageLag = ((2px*2ms/2+8px*8ms/2)+ ((3px+8px)*5ms/2+8px*5ms))/20ms |
| 376 | // = 5.075px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 377 | CheckScrollUpdateHistograms(5, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 378 | // AverageLag (w/o prediction = |
| 379 | // ((5px+15px)*10ms/2 + (5px+10px)*5ms/2 + 10px*5ms)/20ms |
| 380 | // = 9.375px |
| 381 | // Positive effect of prediction = 4.3px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 382 | CheckPredictionPositiveHistograms(4, 1); |
| 383 | CheckPredictionNegativeHistogramsTotalCount(0); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | TEST_F(AverageLagTrackerTest, NegativePredictionEffect) { |
| 387 | // ScrollBegin, at t=5, finger_pos=5px. |
| 388 | // Predict frame_time=10, predicted_pos(over) = 20px. |
| 389 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 390 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 391 | SyntheticTouchScrollBegin(event_time, frame_time, 5 /* scroll_delta */, |
| 392 | 20 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 393 | |
| 394 | // ScrollUpdate, at t=15, finger_pos=15px. |
| 395 | // Predict frame_time=20, predicted_pos(over) = 60px. |
| 396 | event_time = MillisecondsToTimeTicks(15); |
| 397 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 398 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 399 | 40 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 400 | |
| 401 | // ScrollUpdate, at t=25, finger_pos=25px. |
| 402 | // Predict frame_time=30, predicted_pos(over) = 60px. |
| 403 | event_time = MillisecondsToTimeTicks(25); |
| 404 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 405 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 406 | 0 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 407 | |
| 408 | // Another ScrollBegin to flush unfinished frames. |
| 409 | event_time = MillisecondsToTimeTicks(1000); |
| 410 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 411 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 412 | |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 413 | CheckScrollBeginHistograms(7, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 414 | // AverageLag = ((10px+0px)*10ms/2)+ ((40px+35px)*5ms/2+35px*5ms))/20ms |
| 415 | // = 20.625px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 416 | CheckScrollUpdateHistograms(20, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 417 | // AverageLag (w/o prediction = |
| 418 | // ((5px+15px)*10ms/2 + (5px+10px)*5ms/2 + 10px*5ms)/20ms |
| 419 | // = 9.375px |
| 420 | // Negative effect of prediction = 11.25 |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 421 | CheckPredictionPositiveHistogramsTotalCount(0); |
| 422 | CheckPredictionNegativeHistograms(11, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | TEST_F(AverageLagTrackerTest, NoPredictionEffect) { |
| 426 | // ScrollBegin, at t=5, finger_pos=5px. |
| 427 | // Predict frame_time=10, predicted_pos(over) = 25px. |
| 428 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 429 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 430 | SyntheticTouchScrollBegin(event_time, frame_time, 5 /* scroll_delta */, |
| 431 | 25 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 432 | |
| 433 | // ScrollUpdate, at t=15, finger_pos=15px. |
| 434 | // Predict frame_time=20, predicted_pos(over) = 32px. |
| 435 | event_time = MillisecondsToTimeTicks(15); |
| 436 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 437 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 438 | 7 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 439 | |
| 440 | // ScrollUpdate, at t=25, finger_pos=25px. |
| 441 | // Predict frame_time=30, predicted_pos(over) = 37px. |
| 442 | event_time = MillisecondsToTimeTicks(25); |
| 443 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 444 | SyntheticTouchScrollUpdate(event_time, frame_time, 10 /* scroll_delta */, |
| 445 | 5 /* predicted_delta */); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 446 | |
| 447 | // Another ScrollBegin to flush unfinished frames. |
| 448 | event_time = MillisecondsToTimeTicks(1000); |
| 449 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 450 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 451 | |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 452 | CheckScrollBeginHistograms(7, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 453 | // AverageLag = ((15px+5px)*10ms/2 + (12px+7px)*5ms/2 + 7px*5ms)/20ms |
| 454 | // = 9.125px |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 455 | CheckScrollUpdateHistograms(9, 1); |
Daniel Libby | f7cf7738 | 2019-05-06 19:23:44 | [diff] [blame] | 456 | // AverageLag (w/o prediction) = |
| 457 | // ((5px+15px)*10ms/2 + (5px+10px)*5ms/2 + 10px*5ms)/20ms |
| 458 | // = 9.375px |
| 459 | // Prediction slightly positive, we should see a 0 bucket in |
| 460 | // PredictionPositive UMA |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 461 | CheckPredictionPositiveHistograms(0, 1); |
| 462 | CheckPredictionNegativeHistogramsTotalCount(0); |
Ella Ge | 7d1d3be20 | 2019-04-08 16:47:59 | [diff] [blame] | 463 | } |
| 464 | |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 465 | // Tests that when an event arrives out-of-order, the average lag tracker |
| 466 | // properly ignores it. |
| 467 | TEST_F(AverageLagTrackerTest, EventOutOfOrder) { |
| 468 | base::TimeTicks event_time = MillisecondsToTimeTicks(5); |
| 469 | base::TimeTicks frame_time = MillisecondsToTimeTicks(10); |
| 470 | float scroll_delta = 5.f; |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 471 | SyntheticTouchScrollBegin(event_time, frame_time, scroll_delta); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 472 | |
| 473 | event_time = MillisecondsToTimeTicks(15); |
| 474 | frame_time = MillisecondsToTimeTicks(20); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 475 | SyntheticTouchScrollUpdate(event_time, frame_time, scroll_delta); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 476 | |
| 477 | event_time = MillisecondsToTimeTicks(25); |
| 478 | frame_time = MillisecondsToTimeTicks(30); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 479 | SyntheticTouchScrollUpdate(event_time, frame_time, scroll_delta); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 480 | |
| 481 | // A ScrollBegin to flush unfinished frames. |
| 482 | event_time = MillisecondsToTimeTicks(1000); |
| 483 | frame_time = MillisecondsToTimeTicks(1000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 484 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 485 | |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 486 | CheckScrollUpdateHistogramsTotalCount(1); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 487 | |
| 488 | // Send an event whose timestamp is earlier than the most recent event, |
| 489 | // representing an event that gets process out of order. |
| 490 | base::TimeTicks earlier_event_time = MillisecondsToTimeTicks(15); |
| 491 | frame_time = MillisecondsToTimeTicks(1010); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 492 | SyntheticTouchScrollUpdate(earlier_event_time, frame_time, scroll_delta); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 493 | |
| 494 | // Another ScrollBegin to flush unfinished frames. |
| 495 | event_time = MillisecondsToTimeTicks(2000); |
| 496 | frame_time = MillisecondsToTimeTicks(2000); |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 497 | SyntheticTouchScrollBegin(event_time, frame_time, 0); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 498 | |
| 499 | // Ensure that the event was ignored. |
Joao Victor Almeida | 0b3c5a9 | 2020-07-07 21:04:43 | [diff] [blame] | 500 | CheckScrollUpdateHistogramsTotalCount(1); |
Matt Amert | 9da83c3 | 2019-11-06 06:04:31 | [diff] [blame] | 501 | } |
| 502 | |
Ella Ge | 792e4f1 | 2019-04-05 17:32:15 | [diff] [blame] | 503 | } // namespace |
Joao Victor Almeida | 917ce17 | 2020-07-07 20:56:12 | [diff] [blame] | 504 | } // namespace cc |