Chrome DevTools for coding agents
chore: add a new profiler scenario for get_console_messages. (#2491)
yulunz71d897b27e79
diff --git a/scripts/profile/profile_mcp.ts b/scripts/profile/profile_mcp.tsindex 132340d7..7654e564 100644--- a/scripts/profile/profile_mcp.ts+++ b/scripts/profile/profile_mcp.ts@@ -19,7 +19,11 @@ import {} from './inspector.ts';import {startSourceMapTestServer} from './source_map_server.ts';import type {ProfileScenario, ScenarioArgs} from './types.ts';-import {formatBytes, isScenarioModule, parsePositiveInteger} from './utils.ts';+import {+ formatBytes,+ isScenarioModule,+ parseNonNegativeInteger,+} from './utils.ts';interface ProfileOptions {iterations: number;@@ -244,20 +248,24 @@ async function main(): Promise<void> {const {values} = parseArgs({options: {'output-dir': {type: 'string'},- iterations: {type: 'string', default: '10'},- 'warmup-iterations': {type: 'string', default: '10'},+ iterations: {type: 'string'},+ 'warmup-iterations': {type: 'string'},scenario: {type: 'string'},url: {type: 'string'},},});- const options: ProfileOptions = {- iterations: parsePositiveInteger(values.iterations, '--iterations'),- warmupIterations: parsePositiveInteger(- values['warmup-iterations'],- '--warmup-iterations',- ),- };+ const cliIterations =+ values.iterations !== undefined+ ? parseNonNegativeInteger(values.iterations, '--iterations')+ : undefined;+ const cliWarmupIterations =+ values['warmup-iterations'] !== undefined+ ? parseNonNegativeInteger(+ values['warmup-iterations'],+ '--warmup-iterations',+ )+ : undefined;const rootDir = path.resolve(import.meta.dirname, '../..');const scenariosDir = path.join(import.meta.dirname, 'scenarios');@@ -283,6 +291,23 @@ async function main(): Promise<void> {const results: ScenarioResult[] = [];for (const scenarioEntry of scenarios) {+ const scenarioDefaults =+ scenarioEntry.scenario.getNumIterations?.() ?? {};+ const iterations = cliIterations ?? scenarioDefaults.iterations ?? 10;+ const warmupIterations =+ cliWarmupIterations ?? scenarioDefaults.warmupIterations ?? 10;++ if (iterations === 0 && warmupIterations === 0) {+ throw new Error(+ `--iterations and --warmup-iterations cannot both be 0 for scenario "${scenarioEntry.name}"`,+ );+ }++ const options: ProfileOptions = {+ iterations,+ warmupIterations,+ };+const scenarioOutputDir =scenarios.length === 1 && values['output-dir'] !== undefined? baseOutputDirdiff --git a/scripts/profile/scenarios/get_console_messages.ts b/scripts/profile/scenarios/get_console_messages.tsnew file mode 100644index 00000000..e5936084--- /dev/null+++ b/scripts/profile/scenarios/get_console_messages.ts@@ -0,0 +1,36 @@+/**+ * @license+ * Copyright 2026 Google LLC+ * SPDX-License-Identifier: Apache-2.0+ */++import type {ScenarioArgs, ScenarioIterations, ToolCall} from '../types.ts';++export function getNumIterations(): ScenarioIterations {+ return {+ iterations: 1,+ warmupIterations: 0,+ };+}++export function get(args: ScenarioArgs): ToolCall[] {+ const toolCalls: ToolCall[] = [+ {+ name: 'navigate_page',+ arguments: {type: 'url', url: args.targetUrl},+ },+ {+ name: 'list_console_messages',+ arguments: {},+ },+ ];++ for (let msgid = 1; msgid <= 25; msgid++) {+ toolCalls.push({+ name: 'get_console_message',+ arguments: {msgid},+ });+ }++ return toolCalls;+}diff --git a/scripts/profile/source_map_server.ts b/scripts/profile/source_map_server.tsindex 5302b57f..c86efe35 100644--- a/scripts/profile/source_map_server.ts+++ b/scripts/profile/source_map_server.ts@@ -11,22 +11,26 @@ export interface SourceMapTestServer {url: string;}-export function sourceMapFor(index: number): string {+export function sourceMapFor(index: number, sizeBytes = 1_000_000): string {+ const padding = '// Large source map payload padding line\n'.repeat(+ Math.ceil(sizeBytes / 42),+ );return JSON.stringify({version: 3,file: `script-${index}.js`,sources: [`source-${index}.ts`],sourcesContent: [- `export function profileFunction${index}(value: number): number {\n return value + ${index};\n}\n`,+ `export function profileFunction${index}(value: number): number {\n console.error(new Error('Profile error from script ${index}'));\n return value + ${index};\n}\n${padding}`,],names: [],- mappings: 'AAAA;AACA;AACA',+ mappings: 'AAAA;AACA;AACA;AACA',});}export function scriptFor(index: number): string {return [`function profileFunction${index}(value) {`,+ ` console.error(new Error('Profile error from script ' + ${index}));`,` return value + ${index};`,'}','globalThis.profileScriptResults ??= [];',diff --git a/scripts/profile/types.ts b/scripts/profile/types.tsindex 83b8541b..3ef344b8 100644--- a/scripts/profile/types.ts+++ b/scripts/profile/types.ts@@ -25,8 +25,14 @@ export interface ToolCall {arguments?: ToolArguments;}+export interface ScenarioIterations {+ iterations?: number;+ warmupIterations?: number;+}+export interface ProfileScenario {name?: string;description?: string;+ getNumIterations?: () => ScenarioIterations;get: (args: ScenarioArgs) => ToolCall[] | Promise<ToolCall[]>;}diff --git a/scripts/profile/utils.ts b/scripts/profile/utils.tsindex 1ca33e32..ba90d464 100644--- a/scripts/profile/utils.ts+++ b/scripts/profile/utils.ts@@ -14,16 +14,16 @@ export function formatBytes(bytes: number): string {return `${(bytes / 1024 / 1024).toFixed(2)} MiB`;}-export function parsePositiveInteger(+export function parseNonNegativeInteger(value: string | undefined,name: string,): number {if (value === undefined || !/^\d+$/.test(value)) {- throw new Error(`${name} must be a positive integer`);+ throw new Error(`${name} must be a non-negative integer`);}const parsed = Number(value);- if (!Number.isSafeInteger(parsed) || parsed < 1) {- throw new Error(`${name} must be a positive integer`);+ if (!Number.isSafeInteger(parsed) || parsed < 0) {+ throw new Error(`${name} must be a non-negative integer`);}return parsed;}