summaryrefslogtreecommitdiff
path: root/chromium/components/page_load_metrics/browser/responsiveness_metrics_normalization_unittest.cc
blob: 87736715930f209007aa2a51c75f7a44eba804d7 (plain)
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/page_load_metrics/browser/responsiveness_metrics_normalization.h"
#include "base/test/scoped_feature_list.h"
#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

using UserInteractionLatenciesPtr =
    page_load_metrics::mojom::UserInteractionLatenciesPtr;
using UserInteractionLatencies =
    page_load_metrics::mojom::UserInteractionLatencies;
using UserInteractionLatency = page_load_metrics::mojom::UserInteractionLatency;
using UserInteractionType = page_load_metrics::mojom::UserInteractionType;

class ResponsivenessMetricsNormalizationTest : public testing::Test {
 public:
  ResponsivenessMetricsNormalizationTest() = default;

  void AddNewUserInteractions(uint64_t num_new_interactions,
                              UserInteractionLatencies& max_event_durations,
                              UserInteractionLatencies& total_event_durations) {
    responsiveness_metrics_normalization_.AddNewUserInteractionLatencies(
        num_new_interactions, max_event_durations, total_event_durations);
  }

  const page_load_metrics::NormalizedResponsivenessMetrics&
  normalized_responsiveness_metrics() const {
    return responsiveness_metrics_normalization_
        .GetNormalizedResponsivenessMetrics();
  }

 private:
  page_load_metrics::ResponsivenessMetricsNormalization
      responsiveness_metrics_normalization_;
};

TEST_F(ResponsivenessMetricsNormalizationTest, OnlySendWorstInteractions) {
  UserInteractionLatenciesPtr max_event_duration1 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(120));
  UserInteractionLatenciesPtr total_event_durations1 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(140));
  AddNewUserInteractions(1, *max_event_duration1, *total_event_durations1);

  UserInteractionLatenciesPtr max_event_duration2 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(200));
  UserInteractionLatenciesPtr total_event_durations2 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(250));
  AddNewUserInteractions(2, *max_event_duration2, *total_event_durations2);

  UserInteractionLatenciesPtr max_event_duration3 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(70));
  UserInteractionLatenciesPtr total_event_durations3 =
      UserInteractionLatencies::NewWorstInteractionLatency(
          base::Milliseconds(70));
  AddNewUserInteractions(3, *max_event_duration3, *total_event_durations3);

  EXPECT_EQ(normalized_responsiveness_metrics().num_user_interactions, 6u);
  // When the flag is disabled, only worst_latency has a meaningful value and
  // other metrics should have default values.
  auto& normalized_max_event_durations =
      normalized_responsiveness_metrics().normalized_max_event_durations;
  EXPECT_EQ(normalized_max_event_durations.worst_latency,
            base::Milliseconds(200));
  EXPECT_EQ(normalized_max_event_durations.worst_latency_over_budget,
            base::Milliseconds(0));
  EXPECT_EQ(normalized_max_event_durations.sum_of_latency_over_budget,
            base::Milliseconds(0));
  EXPECT_EQ(
      normalized_max_event_durations.pseudo_second_worst_latency_over_budget,
      base::Milliseconds(0));

  auto& normalized_total_event_durations =
      normalized_responsiveness_metrics().normalized_total_event_durations;
  EXPECT_EQ(normalized_total_event_durations.worst_latency,
            base::Milliseconds(250));
  EXPECT_EQ(normalized_total_event_durations.worst_latency_over_budget,
            base::Milliseconds(0));
  EXPECT_EQ(normalized_total_event_durations.sum_of_latency_over_budget,
            base::Milliseconds(0));
  EXPECT_EQ(
      normalized_total_event_durations.pseudo_second_worst_latency_over_budget,
      base::Milliseconds(0));
}

TEST_F(ResponsivenessMetricsNormalizationTest, SendAllInteractions) {
  // Flip the flag to send all user interaction latencies to browser.
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(
      blink::features::kSendAllUserInteractionLatencies);

  UserInteractionLatenciesPtr max_event_durations =
      UserInteractionLatencies::NewUserInteractionLatencies({});
  auto& user_interaction_latencies1 =
      max_event_durations->get_user_interaction_latencies();
  user_interaction_latencies1.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(50), UserInteractionType::kKeyboard));
  user_interaction_latencies1.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(100), UserInteractionType::kTapOrClick));
  user_interaction_latencies1.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(150), UserInteractionType::kDrag));

  UserInteractionLatenciesPtr total_event_durations =
      UserInteractionLatencies::NewUserInteractionLatencies({});
  auto& user_interaction_latencies2 =
      total_event_durations->get_user_interaction_latencies();
  user_interaction_latencies2.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(55), UserInteractionType::kKeyboard));
  user_interaction_latencies2.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(105), UserInteractionType::kTapOrClick));
  user_interaction_latencies2.emplace_back(UserInteractionLatency::New(
      base::Milliseconds(155), UserInteractionType::kDrag));

  AddNewUserInteractions(3, *max_event_durations, *total_event_durations);
  auto worst_ten_max_event_durations =
      normalized_responsiveness_metrics()
          .normalized_max_event_durations.worst_ten_latencies_over_budget;
  EXPECT_EQ(worst_ten_max_event_durations.size(), 3u);
  EXPECT_EQ(worst_ten_max_event_durations.top(), base::Milliseconds(0));
  worst_ten_max_event_durations.pop();
  EXPECT_EQ(worst_ten_max_event_durations.top(), base::Milliseconds(0));
  worst_ten_max_event_durations.pop();
  EXPECT_EQ(worst_ten_max_event_durations.top(), base::Milliseconds(50));

  auto worst_ten_total_event_durations =
      normalized_responsiveness_metrics()
          .normalized_total_event_durations.worst_ten_latencies_over_budget;
  EXPECT_EQ(worst_ten_total_event_durations.size(), 3u);
  EXPECT_EQ(worst_ten_total_event_durations.top(), base::Milliseconds(5));
  worst_ten_total_event_durations.pop();
  EXPECT_EQ(worst_ten_total_event_durations.top(), base::Milliseconds(5));
  worst_ten_total_event_durations.pop();
  EXPECT_EQ(worst_ten_total_event_durations.top(), base::Milliseconds(55));
  EXPECT_EQ(normalized_responsiveness_metrics().num_user_interactions, 3u);

  auto& normalized_max_event_durations =
      normalized_responsiveness_metrics().normalized_max_event_durations;
  EXPECT_EQ(normalized_max_event_durations.worst_latency,
            base::Milliseconds(150));
  EXPECT_EQ(normalized_max_event_durations.worst_latency_over_budget,
            base::Milliseconds(50));
  EXPECT_EQ(normalized_max_event_durations.sum_of_latency_over_budget,
            base::Milliseconds(50));
  EXPECT_EQ(
      normalized_max_event_durations.pseudo_second_worst_latency_over_budget,
      base::Milliseconds(0));

  auto& normalized_total_event_durations =
      normalized_responsiveness_metrics().normalized_total_event_durations;
  EXPECT_EQ(normalized_total_event_durations.worst_latency,
            base::Milliseconds(155));
  EXPECT_EQ(normalized_total_event_durations.worst_latency_over_budget,
            base::Milliseconds(55));
  EXPECT_EQ(normalized_total_event_durations.sum_of_latency_over_budget,
            base::Milliseconds(65));
  EXPECT_EQ(
      normalized_total_event_durations.pseudo_second_worst_latency_over_budget,
      base::Milliseconds(5));
}