File: performance_controls_metrics_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (195 lines) | stat: -rw-r--r-- 8,198 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/performance_controls/performance_controls_metrics.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/performance_manager/public/user_tuning/performance_detection_manager.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_button_controller.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/user_tuning/prefs.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
using performance_manager::user_tuning::PerformanceDetectionManager;
}

class PerformanceControlsMetricsTest
    : public content::RenderViewHostTestHarness {
 public:
  PerformanceControlsMetricsTest()
      : RenderViewHostTestHarness(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

  std::unique_ptr<content::BrowserContext> CreateBrowserContext() override {
    return std::make_unique<TestingProfile>();
  }

  TestingPrefServiceSimple* prefs() {
    return scoped_testing_local_state_.Get();
  }

 private:
  ScopedTestingLocalState scoped_testing_local_state_{
      TestingBrowserProcess::GetGlobal()};
};

TEST_F(PerformanceControlsMetricsTest, DailyMetricsResets) {
  base::HistogramTester histogram_tester;

  const std::string message_count_histogram_name =
      "PerformanceControls.Intervention.BackgroundTab.Cpu.MessageShownCount";
  const std::string rate_limited_histogram_name =
      "PerformanceControls.Intervention.BackgroundTab.Cpu.RateLimitedCount";

  PrefService* const pref_service = prefs();
  std::unique_ptr<PerformanceInterventionMetricsReporter> daily_metrics =
      std::make_unique<PerformanceInterventionMetricsReporter>(pref_service);

  EXPECT_EQ(0, pref_service->GetInteger(
                   prefs::kPerformanceInterventionBackgroundCpuMessageCount));
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 1, 0);
  histogram_tester.ExpectBucketCount(rate_limited_histogram_name, 1, 0);

  RecordInterventionMessageCount(
      PerformanceDetectionManager::ResourceType::kCpu, pref_service);

  // Only the pref count should increment when we record a message count since
  // the histogram should record the value only after each day.
  EXPECT_EQ(1, pref_service->GetInteger(
                   prefs::kPerformanceInterventionBackgroundCpuMessageCount));
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 1, 0);

  RecordInterventionRateLimitedCount(
      PerformanceDetectionManager::ResourceType::kCpu, pref_service);
  EXPECT_EQ(1,
            pref_service->GetInteger(
                prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount));
  histogram_tester.ExpectBucketCount(rate_limited_histogram_name, 1, 0);

  // Message and rate limit count should be recorded to the histogram
  // and the prefs should be reset after a day
  task_environment()->FastForwardBy(base::Days(1));
  EXPECT_EQ(0, pref_service->GetInteger(
                   prefs::kPerformanceInterventionBackgroundCpuMessageCount));
  EXPECT_EQ(0,
            pref_service->GetInteger(
                prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount));
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 1, 1);
  histogram_tester.ExpectBucketCount(rate_limited_histogram_name, 1, 1);
}

TEST_F(PerformanceControlsMetricsTest, HealthStatusChange) {
  base::HistogramTester histogram_tester;
  const std::string message_count_histogram_name =
      "PerformanceControls.Intervention.BackgroundTab.Cpu.HealthStatusChange."
      "1Min";

  // Recording a health status that didn't change.
  RecordCpuHealthStatusChange(
      base::Minutes(1), PerformanceDetectionManager::HealthLevel::kHealthy,
      PerformanceDetectionManager::HealthLevel::kHealthy);
  histogram_tester.ExpectBucketCount(
      message_count_histogram_name,
      CpuInterventionHealthChange::kHealthyToHealthy, 1);

  // Recording a health status that improved by one level.
  RecordCpuHealthStatusChange(
      base::Minutes(1), PerformanceDetectionManager::HealthLevel::kUnhealthy,
      PerformanceDetectionManager::HealthLevel::kDegraded);
  histogram_tester.ExpectBucketCount(
      message_count_histogram_name,
      CpuInterventionHealthChange::kUnhealthyToDegraded, 1);

  // Recording a health status that improved by 2 levels.
  RecordCpuHealthStatusChange(
      base::Minutes(1), PerformanceDetectionManager::HealthLevel::kUnhealthy,
      PerformanceDetectionManager::HealthLevel::kHealthy);
  histogram_tester.ExpectBucketCount(
      message_count_histogram_name,
      CpuInterventionHealthChange::kUnhealthyToHealthy, 1);

  // Recording a health status that got worse.
  RecordCpuHealthStatusChange(
      base::Minutes(1), PerformanceDetectionManager::HealthLevel::kHealthy,
      PerformanceDetectionManager::HealthLevel::kUnhealthy);
  histogram_tester.ExpectBucketCount(
      message_count_histogram_name,
      CpuInterventionHealthChange::kHealthyToUnhealthy, 1);
}

class PerformanceControlsNotificationTest
    : public PerformanceControlsMetricsTest {
 public:
  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(
        performance_manager::features::
            kPerformanceInterventionNotificationImprovements);

    PerformanceControlsMetricsTest::SetUp();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(PerformanceControlsNotificationTest,
       RecordsNotificationAcceptPercentage) {
  base::HistogramTester histogram_tester;
  const std::string message_count_histogram_name =
      "PerformanceControls.Intervention.DailyAcceptancePercentage";
  std::unique_ptr<PerformanceInterventionButtonController> controller =
      std::make_unique<PerformanceInterventionButtonController>(nullptr,
                                                                nullptr);

  PrefService* const pref_service = prefs();
  std::unique_ptr<PerformanceInterventionMetricsReporter> daily_metrics =
      std::make_unique<PerformanceInterventionMetricsReporter>(pref_service);

  ASSERT_TRUE(
      pref_service
          ->GetList(performance_manager::user_tuning::prefs::
                        kPerformanceInterventionNotificationAcceptHistory)
          .empty());

  task_environment()->FastForwardBy(base::Days(1));
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 100, 0);

  pref_service->SetList(performance_manager::user_tuning::prefs::
                            kPerformanceInterventionNotificationAcceptHistory,
                        base::Value::List().Append(true));
  task_environment()->FastForwardBy(base::Days(1));
  EXPECT_EQ(100,
            PerformanceInterventionButtonController::GetAcceptancePercentage());
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 100, 1);

  base::Value::List updated_accept_history = base::Value::List();
  for (int i = 0;
       i < performance_manager::features::kAcceptanceRateWindowSize.Get() / 2;
       i++) {
    updated_accept_history.Append(true);
    updated_accept_history.Append(false);
  }
  pref_service->SetList(performance_manager::user_tuning::prefs::
                            kPerformanceInterventionNotificationAcceptHistory,
                        std::move(updated_accept_history));
  task_environment()->FastForwardBy(base::Days(1));
  EXPECT_EQ(50,
            PerformanceInterventionButtonController::GetAcceptancePercentage());
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 50, 1);
  histogram_tester.ExpectBucketCount(message_count_histogram_name, 100, 1);
}