File: waap_ui_metrics_service.cc

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,786,824 kB
  • sloc: cpp: 35,783,839; ansic: 7,477,365; javascript: 3,962,116; python: 1,480,521; xml: 764,832; asm: 710,816; pascal: 188,028; sh: 88,717; perl: 88,692; objc: 79,984; sql: 57,625; cs: 42,265; fortran: 24,101; makefile: 22,509; tcl: 15,277; php: 14,018; yacc: 9,043; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 7,628 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
196
197
198
199
200
201
202
203
204
// Copyright 2025 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/waap/waap_ui_metrics_service.h"

#include <string>
#include <string_view>

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_recorder.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_service_factory.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "components/startup_metric_utils/common/startup_metric_utils.h"

namespace {

std::string_view ReloadButtonModeToString(
    WaapUIMetricsRecorder::ReloadButtonMode mode) {
  switch (mode) {
    case WaapUIMetricsRecorder::ReloadButtonMode::kReload:
      return "Reload";
    case WaapUIMetricsRecorder::ReloadButtonMode::kStop:
      return "Stop";
  }
  NOTREACHED();
}

std::string_view ReloadButtonInputTypeToString(
    WaapUIMetricsRecorder::ReloadButtonInputType type) {
  switch (type) {
    case WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease:
      return ".MouseRelease";
    case WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress:
      return ".KeyPress";
  }
  NOTREACHED();
}

// Helper to construct the full histogram name for ReloadButton metrics
std::string BuildReloadButtonHistogramName(std::string_view base,
                                           std::string_view slice = "") {
  return base::StrCat({"InitialWebUI.ReloadButton.", base, slice});
}

// Emits a WaaP trace event asynchronously onto a perfetto::Track and records a
// UMA histogram with the same event name.
void EmitHistogramWithTraceEvent(const char* event_name,
                                 base::TimeTicks start_ticks,
                                 base::TimeTicks end_ticks) {
  TRACE_EVENT_BEGIN("waap", perfetto::StaticString(event_name),
                    perfetto::Track(reinterpret_cast<uintptr_t>(event_name)),
                    start_ticks);
  TRACE_EVENT_END("waap",
                  perfetto::Track(reinterpret_cast<uintptr_t>(event_name)),
                  end_ticks);

  const base::TimeDelta delta = end_ticks - start_ticks;
  base::UmaHistogramLongTimes100(event_name, delta);
}

// Emits a WaaP trace event and records a UMA histogram with the given event
// name and duration.
void EmitReloadButtonHistogramWithTraceEvent(const char* event_name,
                                             base::TimeTicks start_ticks,
                                             base::TimeTicks end_ticks) {
  const base::TimeDelta duration = end_ticks - start_ticks;
  TRACE_EVENT_BEGIN("waap", perfetto::StaticString(event_name),
                    perfetto::Track(reinterpret_cast<uintptr_t>(event_name)),
                    start_ticks);
  TRACE_EVENT_END("waap",
                  perfetto::Track(reinterpret_cast<uintptr_t>(event_name)),
                  end_ticks);
  base::UmaHistogramCustomTimes(event_name, duration, base::Milliseconds(1),
                                base::Minutes(3), 100);
}

void RecordStartupPaintMetric(const char* paint_metric_name,
                              base::TimeTicks paint_time) {
  if (!startup_metric_utils::GetBrowser().ShouldLogStartupHistogram()) {
    return;
  }

  base::TimeTicks time_origin =
      startup_metric_utils::GetBrowser().GetApplicationStartTicksForStartup();
  if (time_origin.is_null()) {
    return;
  }

  // For early experiment, this is ReloadButton only.
  // TODO(crbug.com/448794588): Switch to general name after initial phase.
  std::string histogram_name =
      base::StrCat({"InitialWebUI.Startup.ReloadButton.", paint_metric_name});
  switch (startup_metric_utils::GetBrowser().GetStartupTemperature()) {
    case startup_metric_utils::COLD_STARTUP_TEMPERATURE:
      histogram_name = base::StrCat({histogram_name, ".ColdStartup"});
      break;
    case startup_metric_utils::WARM_STARTUP_TEMPERATURE:
      histogram_name = base::StrCat({histogram_name, ".WarmStartup"});
      break;
    case startup_metric_utils::LUKEWARM_STARTUP_TEMPERATURE:
      break;
    case startup_metric_utils::UNDETERMINED_STARTUP_TEMPERATURE:
      break;
    case startup_metric_utils::STARTUP_TEMPERATURE_COUNT:
      NOTREACHED();
  }

  EmitHistogramWithTraceEvent(histogram_name.c_str(), time_origin, paint_time);
}

}  // namespace

WaapUIMetricsService::WaapUIMetricsService(
    base::PassKey<WaapUIMetricsServiceFactory>) {}

WaapUIMetricsService::~WaapUIMetricsService() = default;

// static
WaapUIMetricsService* WaapUIMetricsService::Get(Profile* profile) {
  return WaapUIMetricsServiceFactory::GetForProfile(profile);
}

void WaapUIMetricsService::OnFirstPaint(base::TimeTicks time) {
  static bool is_first_call = true;
  CHECK(!time.is_null());
  if (!is_first_call) {
    return;
  }
  is_first_call = false;

  RecordStartupPaintMetric("FirstPaint", time);
}

void WaapUIMetricsService::OnFirstContentfulPaint(base::TimeTicks time) {
  static bool is_first_call = true;
  CHECK(!time.is_null());
  if (!is_first_call) {
    return;
  }
  is_first_call = false;

  RecordStartupPaintMetric("FirstContentfulPaint", time);
}

void WaapUIMetricsService::OnReloadButtonMousePressToNextPaint(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks) {
  auto name = BuildReloadButtonHistogramName("MousePressToNextPaint");
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}

void WaapUIMetricsService::OnReloadButtonMouseHoverToNextPaint(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks) {
  auto name = BuildReloadButtonHistogramName("MouseHoverToNextPaint");
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}

void WaapUIMetricsService::OnReloadButtonInput(
    WaapUIMetricsRecorder::ReloadButtonInputType input_type) {
  auto name = BuildReloadButtonHistogramName("InputCount");
  base::UmaHistogramEnumeration(name, input_type);
}

void WaapUIMetricsService::OnReloadButtonInputToReload(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks,
    WaapUIMetricsRecorder::ReloadButtonInputType input_type) {
  auto name = BuildReloadButtonHistogramName(
      "InputToReload", ReloadButtonInputTypeToString(input_type));
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}

void WaapUIMetricsService::OnReloadButtonInputToStop(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks,
    WaapUIMetricsRecorder::ReloadButtonInputType input_type) {
  auto name = BuildReloadButtonHistogramName(
      "InputToStop", ReloadButtonInputTypeToString(input_type));
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}

void WaapUIMetricsService::OnReloadButtonInputToNextPaint(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks,
    WaapUIMetricsRecorder::ReloadButtonInputType input_type) {
  auto name = BuildReloadButtonHistogramName(
      "InputToNextPaint", ReloadButtonInputTypeToString(input_type));
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}

void WaapUIMetricsService::OnReloadButtonChangeVisibleModeToNextPaint(
    base::TimeTicks start_ticks,
    base::TimeTicks end_ticks,
    WaapUIMetricsRecorder::ReloadButtonMode new_mode) {
  auto name = BuildReloadButtonHistogramName(
      "ChangeVisibleModeToNextPaintIn", ReloadButtonModeToString(new_mode));
  EmitReloadButtonHistogramWithTraceEvent(name.c_str(), start_ticks, end_ticks);
}