1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/prediction/prediction_metrics_handler.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/prediction/input_predictor_unittest_helpers.h"
#include "ui/base/prediction/prediction_unittest_helpers.h"
using base::Bucket;
using testing::ElementsAre;
namespace ui {
namespace test {
namespace {
base::TimeTicks MillisecondsToTestTimeTicks(int64_t ms) {
return PredictionUnittestHelpers::GetStaticTimeStampForTests() +
base::Milliseconds(ms);
}
} // namespace
class PredictionMetricsHandlerTest : public testing::Test {
public:
explicit PredictionMetricsHandlerTest() {}
PredictionMetricsHandlerTest(const PredictionMetricsHandlerTest&) = delete;
PredictionMetricsHandlerTest& operator=(const PredictionMetricsHandlerTest&) =
delete;
void SetUp() override {
metrics_handler_ = std::make_unique<PredictionMetricsHandler>(
"Event.InputEventPrediction.Scroll");
histogram_tester_ = std::make_unique<base::HistogramTester>();
}
int GetInterpolatedEventForPredictedEvent(const base::TimeTicks& timestamp,
gfx::PointF* interpolated) {
return metrics_handler_->GetInterpolatedEventForPredictedEvent(
timestamp, interpolated);
}
::testing::AssertionResult HistogramSizeEq(const char* histogram_name,
int size) {
uint64_t histogram_size =
histogram_tester_->GetAllSamples(histogram_name).size();
if (static_cast<uint64_t>(size) == histogram_size) {
return ::testing::AssertionSuccess();
} else {
return ::testing::AssertionFailure()
<< histogram_name << " expected " << size << " entries, but had "
<< histogram_size;
}
}
bool HasPredictionHistograms() {
uint64_t histogram_size =
histogram_tester_
->GetAllSamples("Event.InputEventPrediction.Scroll.VisualJitter")
.size();
return histogram_size > 0u;
}
void Reset() {
histogram_tester_ = std::make_unique<base::HistogramTester>();
metrics_handler_->Reset();
}
const base::HistogramTester& histogram_tester() { return *histogram_tester_; }
protected:
std::unique_ptr<PredictionMetricsHandler> metrics_handler_;
std::unique_ptr<base::HistogramTester> histogram_tester_;
};
TEST_F(PredictionMetricsHandlerTest, CanComputeMetricsTest) {
base::TimeTicks start_time =
PredictionUnittestHelpers::GetStaticTimeStampForTests();
base::TimeDelta dt = base::Milliseconds(8);
// Need at least 2 real events to start comput metrics.
{
metrics_handler_->AddRealEvent(gfx::PointF(0, 0), start_time + 3 * dt,
start_time);
metrics_handler_->AddPredictedEvent(gfx::PointF(0, 0), start_time + 3 * dt,
start_time);
metrics_handler_->EvaluatePrediction();
EXPECT_FALSE(HasPredictionHistograms());
}
// Need at least a real event strictly after the second predicted event.
Reset();
{
metrics_handler_->AddRealEvent(gfx::PointF(0, 0), start_time, start_time);
metrics_handler_->AddRealEvent(gfx::PointF(0, 0), start_time + dt,
start_time);
metrics_handler_->AddPredictedEvent(gfx::PointF(0, 0), start_time + 2 * dt,
start_time);
metrics_handler_->AddPredictedEvent(gfx::PointF(0, 0), start_time + 3 * dt,
start_time);
metrics_handler_->EvaluatePrediction();
EXPECT_FALSE(HasPredictionHistograms());
metrics_handler_->AddRealEvent(gfx::PointF(0, 0), start_time + 3 * dt,
start_time);
metrics_handler_->EvaluatePrediction();
EXPECT_FALSE(HasPredictionHistograms());
metrics_handler_->AddRealEvent(gfx::PointF(0, 0), start_time + 3.1 * dt,
start_time);
metrics_handler_->EvaluatePrediction();
EXPECT_TRUE(HasPredictionHistograms());
}
}
TEST_F(PredictionMetricsHandlerTest, InterpolationTest) {
base::TimeTicks start_time =
PredictionUnittestHelpers::GetStaticTimeStampForTests();
base::TimeDelta dt = base::Milliseconds(8);
gfx::PointF interpolated;
metrics_handler_->AddRealEvent(gfx::PointF(2, 2), start_time + 1 * dt,
start_time);
metrics_handler_->AddRealEvent(gfx::PointF(3, 3), start_time + 2 * dt,
start_time);
metrics_handler_->AddRealEvent(gfx::PointF(5, 5), start_time + 3 * dt,
start_time);
metrics_handler_->AddRealEvent(gfx::PointF(8, 8), start_time + 4 * dt,
start_time);
EXPECT_EQ(0, GetInterpolatedEventForPredictedEvent(start_time + 1.5 * dt,
&interpolated));
EXPECT_EQ(interpolated, gfx::PointF(2.5, 2.5));
EXPECT_EQ(2, GetInterpolatedEventForPredictedEvent(start_time + 3.5 * dt,
&interpolated));
EXPECT_EQ(interpolated, gfx::PointF(6.5, 6.5));
}
// For test purpose and simplify, we are predicted in the middle of 2 real
// events, which is also the frame time (i.e. a prediction of 4 ms)
void AddEvents(PredictionMetricsHandler* metrics_handler) {
metrics_handler->AddRealEvent(gfx::PointF(1, 1),
MillisecondsToTestTimeTicks(8),
MillisecondsToTestTimeTicks(12)); // R0
metrics_handler->AddRealEvent(gfx::PointF(2, 2),
MillisecondsToTestTimeTicks(16),
MillisecondsToTestTimeTicks(20)); // R1
metrics_handler->AddRealEvent(gfx::PointF(4, 4),
MillisecondsToTestTimeTicks(24),
MillisecondsToTestTimeTicks(28)); // R2
metrics_handler->AddRealEvent(gfx::PointF(7, 7),
MillisecondsToTestTimeTicks(32),
MillisecondsToTestTimeTicks(36)); // R3
metrics_handler->AddRealEvent(gfx::PointF(5, 5),
MillisecondsToTestTimeTicks(40),
MillisecondsToTestTimeTicks(44)); // R4
metrics_handler->AddRealEvent(gfx::PointF(3, 3),
MillisecondsToTestTimeTicks(48),
MillisecondsToTestTimeTicks(54)); // R5
// P0 | Interpolation from R0-R1 is (1.25,1.25)
// P0 | Frame Interpolation from R0-R1 is (1.5,1.5)
// UnderPrediction
metrics_handler->AddPredictedEvent(gfx::PointF(1, 1),
MillisecondsToTestTimeTicks(10),
MillisecondsToTestTimeTicks(12));
// P1 | Interpolation from R1-R2 is (2.5,2.5)
// P1 | Frame Interpolation from R1-R2 is (3,3)
// OverPrediction
metrics_handler->AddPredictedEvent(gfx::PointF(3.5, 3.5),
MillisecondsToTestTimeTicks(18),
MillisecondsToTestTimeTicks(20));
// P2 | Interpolation from R2-R3 is (4.75,4.75)
// P2 | Frame Interpolation from R2-R3 is (5.5,5.5)
// UnderPrediction
metrics_handler->AddPredictedEvent(gfx::PointF(5, 5),
MillisecondsToTestTimeTicks(26),
MillisecondsToTestTimeTicks(28));
// P3 | Interpolation from R3-R4 is (6.5,6.5)
// P3 | Frame Interpolation from R3-R4 is (6,6)
// UnderPrediction
metrics_handler->AddPredictedEvent(gfx::PointF(7, 7),
MillisecondsToTestTimeTicks(34),
MillisecondsToTestTimeTicks(36));
// P4 | Interpolation from R4-R5 is (4.5,4.5)
// P4 | Frame Interpolation from R4-R5 is (4,4)
// OverPrediction
metrics_handler->AddPredictedEvent(gfx::PointF(3, 3),
MillisecondsToTestTimeTicks(42),
MillisecondsToTestTimeTicks(44));
}
TEST_F(PredictionMetricsHandlerTest, PredictionMetricTest) {
AddEvents(metrics_handler_.get());
metrics_handler_->EvaluatePrediction();
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.OverPrediction"),
ElementsAre(Bucket(0, 1), Bucket(1, 1), Bucket(2, 1)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.UnderPrediction"),
ElementsAre(Bucket(0, 2)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.PredictionScore"),
ElementsAre(Bucket(0, 3), Bucket(1, 1), Bucket(2, 1)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.FrameOverPrediction"),
ElementsAre(Bucket(0, 1), Bucket(1, 1)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.FrameUnderPrediction"),
ElementsAre(Bucket(0, 2), Bucket(1, 1)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.FramePredictionScore"),
ElementsAre(Bucket(0, 3), Bucket(1, 2)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.PredictionJitter"),
ElementsAre(Bucket(0, 1), Bucket(1, 2), Bucket(2, 1)));
EXPECT_THAT(histogram_tester().GetAllSamples(
"Event.InputEventPrediction.Scroll.VisualJitter"),
ElementsAre(Bucket(1, 2), Bucket(2, 2)));
}
// Test that it doesn't crash when predicted event is prior to first real event.
TEST_F(PredictionMetricsHandlerTest, PredictedTimePriorToReal) {
metrics_handler_->AddRealEvent(gfx::PointF(1, 1),
MillisecondsToTestTimeTicks(8),
MillisecondsToTestTimeTicks(12));
metrics_handler_->AddRealEvent(gfx::PointF(2, 2),
MillisecondsToTestTimeTicks(10),
MillisecondsToTestTimeTicks(12));
metrics_handler_->AddPredictedEvent(gfx::PointF(0, 0),
MillisecondsToTestTimeTicks(7),
MillisecondsToTestTimeTicks(12));
metrics_handler_->EvaluatePrediction();
// No prediction metrics result.
EXPECT_TRUE(
HistogramSizeEq("Event.InputEventPrediction.Scroll.PredictionJitter", 0));
}
} // namespace test
} // namespace ui
|