1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-07-25 12:05:19 +02:00
This commit is contained in:
syuilo
2026-06-28 10:16:57 +09:00
parent c5951175ef
commit 5856784288
2 changed files with 31 additions and 23 deletions

View File

@@ -189,6 +189,19 @@ async function launchChrome(label: string): Promise<ChromeHandle> {
throw new Error('Timed out waiting for Chrome DevTools Protocol');
}
async function closeChrome(handle: ChromeHandle) {
if (handle.process.exitCode == null) {
handle.process.kill();
}
await waitForProcessExit(handle.process);
await rm(handle.userDataDir, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 200,
});
}
type CdpResponse<T = any> = {
id?: number;
method?: string;
@@ -302,14 +315,19 @@ export class Chrome {
static async create(label: string, options: ChromeOptions): Promise<Chrome> {
const chromeHandle = await launchChrome(label);
const url = await fetchJson<{ webSocketDebuggerUrl: string }>(
`http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`,
{ method: 'PUT' },
).catch(async () => await fetchJson<{ webSocketDebuggerUrl: string }>(
`http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`,
));
const cdpClient = await CdpClient.connect(url.webSocketDebuggerUrl);
return new Chrome(chromeHandle, cdpClient, options);
try {
const url = await fetchJson<{ webSocketDebuggerUrl: string }>(
`http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`,
{ method: 'PUT' },
).catch(async () => await fetchJson<{ webSocketDebuggerUrl: string }>(
`http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`,
));
const cdpClient = await CdpClient.connect(url.webSocketDebuggerUrl);
return new Chrome(chromeHandle, cdpClient, options);
} catch (err) {
await closeChrome(chromeHandle);
throw err;
}
}
public async enableNetworkTracking() {
@@ -513,16 +531,7 @@ export class Chrome {
public async close() {
this.cdp.close();
if (this.handle.process.exitCode == null) {
this.handle.process.kill();
}
await waitForProcessExit(this.handle.process);
await rm(this.handle.userDataDir, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 200,
});
await closeChrome(this.handle);
}
}

View File

@@ -414,12 +414,11 @@ function summarizeSamples(label: 'base' | 'head', samples: BrowserMeasurementSam
}
async function measureSample(label: 'base' | 'head', round: number, heapSnapshotSavePath?: string) {
let chrome: Chrome | null = null;
await prepareInstance();
const chrome = await Chrome.create(label, { scenarioTimeoutMs });
try {
await prepareInstance();
chrome = await Chrome.create(label, { scenarioTimeoutMs });
await chrome.enableNetworkTracking();
const startedAt = Date.now();
@@ -442,7 +441,7 @@ async function measureSample(label: 'base' | 'head', round: number, heapSnapshot
return measurement;
} finally {
if (chrome != null) await chrome.close();
await chrome.close();
}
}