-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathperformance.ts
More file actions
139 lines (130 loc) · 4.3 KB
/
performance.ts
File metadata and controls
139 lines (130 loc) · 4.3 KB
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import z from 'zod';
import {Context, defineTool, Response} from './ToolDefinition.js';
import {insightOutput, parseRawTraceBuffer} from '../trace-processing/parse.js';
import {logger} from '../logger.js';
import {Page} from 'puppeteer-core';
import {ToolCategories} from './categories.js';
export const startTrace = defineTool({
name: 'performance_start_trace',
description: 'Starts a performance trace recording',
annotations: {
category: ToolCategories.PERFORMANCE,
readOnlyHint: true,
},
schema: {
reload: z
.boolean()
.describe(
'Determines if, once tracing has started, the page should be automatically reloaded',
),
autoStop: z
.boolean()
.describe(
'Determines if the trace recording should be automatically stopped.',
),
},
handler: async (request, response, context) => {
if (context.isRunningPerformanceTrace()) {
response.appendResponseLine(
'Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.',
);
return;
}
context.setIsRunningPerformanceTrace(true);
const page = context.getSelectedPage();
const pageUrlForTracing = page.url();
if (request.params.reload) {
// Before starting the recording, navigate to about:blank to clear out any state.
await page.goto('about:blank', {
waitUntil: ['networkidle0'],
});
}
// This panel may be opened with trace data recorded in other tools.
// Keep in sync with the categories arrays in:
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/timeline/TimelineController.ts
// https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/gather/gatherers/trace.js
const categories = [
'-*',
'blink.console',
'blink.user_timing',
'devtools.timeline',
'disabled-by-default-devtools.screenshot',
'disabled-by-default-devtools.timeline',
'disabled-by-default-devtools.timeline.invalidationTracking',
'disabled-by-default-devtools.timeline.frame',
'disabled-by-default-devtools.timeline.stack',
'disabled-by-default-v8.cpu_profiler',
'disabled-by-default-v8.cpu_profiler.hires',
'latencyInfo',
'loading',
'disabled-by-default-lighthouse',
'v8.execute',
'v8',
];
await page.tracing.start({
categories,
});
if (request.params.reload) {
await page.goto(pageUrlForTracing, {
waitUntil: ['load'],
});
}
if (request.params.autoStop) {
await new Promise(resolve => setTimeout(resolve, 5_000));
await stopTracingAndAppendOutput(page, response, context);
} else {
response.appendResponseLine(
`The performance trace is being recorded. Use performance_stop_trace to stop it.`,
);
}
},
});
export const stopTrace = defineTool({
name: 'performance_stop_trace',
description: 'Stops the active performance trace recording',
annotations: {
category: ToolCategories.PERFORMANCE,
readOnlyHint: true,
},
schema: {},
handler: async (_request, response, context) => {
if (!context.isRunningPerformanceTrace) {
return;
}
const page = context.getSelectedPage();
await stopTracingAndAppendOutput(page, response, context);
},
});
async function stopTracingAndAppendOutput(
page: Page,
response: Response,
context: Context,
): Promise<void> {
try {
const traceEventsBuffer = await page.tracing.stop();
const result = await parseRawTraceBuffer(traceEventsBuffer);
response.appendResponseLine('The performance trace has been stopped.');
if (result) {
const insightText = insightOutput(result);
if (insightText) {
response.appendResponseLine('Insights with performance opportunities:');
response.appendResponseLine(insightText);
} else {
response.appendResponseLine(
'No insights has been found. The performance looks good!',
);
}
}
} catch (e) {
logger(
`Error stopping performance trace: ${e instanceof Error ? e.message : JSON.stringify(e)}`,
);
} finally {
context.setIsRunningPerformanceTrace(false);
}
}