Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/DevtoolsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Mutex} from './Mutex.js';
import {DevTools} from './third_party/index.js';
import type {
Browser,
CDPSession,
ConsoleMessage,
Page,
Protocol,
Expand Down Expand Up @@ -49,6 +50,8 @@ export interface TargetUniverse {
/** The DevTools target corresponding to the puppeteer Page */
target: DevTools.Target;
universe: DevTools.Foundation.Universe.Universe;
/** The secondary session created for this page */
session: CDPSession;
}
export type TargetUniverseFactoryFn = (page: Page) => Promise<TargetUniverse>;

Expand Down Expand Up @@ -158,7 +161,7 @@ const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
undefined,
connection,
);
return {target, universe};
return {target, universe, session};
};

// We don't want to pause any DevTools universe session ever on the MCP side.
Expand Down
13 changes: 12 additions & 1 deletion src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,22 @@ export class McpContext implements Context {
newSettings.networkConditions = options.networkConditions;
}

const secondarySession = this.getDevToolsUniverse(mcpPage)?.session;
if (!options.cpuThrottlingRate) {
await page.emulateCPUThrottling(1);
if (secondarySession) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised this is needed because CPUthrottling is global IIRC. Are we sure DevTools code does not reset CPU throttling upon instantiation?

await secondarySession.send('Emulation.setCPUThrottlingRate', {
rate: 1,
});
}
delete newSettings.cpuThrottlingRate;
} else {
await page.emulateCPUThrottling(options.cpuThrottlingRate);
if (secondarySession) {
await secondarySession.send('Emulation.setCPUThrottlingRate', {
rate: options.cpuThrottlingRate,
});
}
newSettings.cpuThrottlingRate = options.cpuThrottlingRate;
}

Expand Down Expand Up @@ -481,7 +492,7 @@ export class McpContext implements Context {
page.networkConditions,
);
page.pptrPage.setDefaultNavigationTimeout(
NAVIGATION_TIMEOUT * networkMultiplier,
NAVIGATION_TIMEOUT * networkMultiplier * cpuMultiplier,
);
}

Expand Down
32 changes: 31 additions & 1 deletion tests/tools/emulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import assert from 'node:assert';
import type {IncomingHttpHeaders} from 'node:http';
import {beforeEach, describe, it} from 'node:test';
import {beforeEach, describe, it, mock} from 'node:test';

import {emulate} from '../../src/tools/emulation.js';
import {
Expand Down Expand Up @@ -209,6 +209,36 @@ describe('emulation', () => {
});
});

it('applies cpu throttling to secondary session', async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test instead that throttling apply correctly to performance trace results?

await withMcpContext(async (response, context) => {
const mcpPage = context.getSelectedMcpPage();
const universe = context.getDevToolsUniverse(mcpPage);
assert.ok(universe);

const sendSpy = mock.method(universe.session, 'send');

await emulate.handler(
{
params: {
cpuThrottlingRate: 4,
},
page: mcpPage,
},
response,
context,
);

assert.ok(sendSpy.mock.calls.length > 0);
const cpuCall = sendSpy.mock.calls.find(
call => call.arguments[0] === 'Emulation.setCPUThrottlingRate',
);
assert.ok(cpuCall);
assert.deepStrictEqual(cpuCall.arguments[1], {rate: 4});

sendSpy.mock.restore();
});
});

it('disables cpu throttling', async () => {
await withMcpContext(async (response, context) => {
await context.emulate({
Expand Down
Loading