2017-08-02 17:45:11 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2020-07-13 09:22:26 +00:00
|
|
|
import { assert } from './assert.js';
|
2022-06-14 11:16:21 +00:00
|
|
|
import {
|
|
|
|
getReadableAsBuffer,
|
|
|
|
getReadableFromProtocolStream,
|
|
|
|
isErrorLike,
|
|
|
|
} from './util.js';
|
2020-07-13 09:22:26 +00:00
|
|
|
import { CDPSession } from './Connection.js';
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2020-07-02 11:15:39 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TracingOptions {
|
2020-04-23 11:51:48 +00:00
|
|
|
path?: string;
|
|
|
|
screenshots?: boolean;
|
|
|
|
categories?: string[];
|
|
|
|
}
|
|
|
|
|
2020-06-24 12:57:59 +00:00
|
|
|
/**
|
|
|
|
* The Tracing class exposes the tracing audit interface.
|
|
|
|
* @remarks
|
|
|
|
* You can use `tracing.start` and `tracing.stop` to create a trace file
|
|
|
|
* which can be opened in Chrome DevTools or {@link https://chromedevtools.github.io/timeline-viewer/ | timeline viewer}.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* await page.tracing.start({path: 'trace.json'});
|
|
|
|
* await page.goto('https://www.google.com');
|
|
|
|
* await page.tracing.stop();
|
|
|
|
* ```
|
2020-07-02 11:15:39 +00:00
|
|
|
*
|
|
|
|
* @public
|
2020-06-24 12:57:59 +00:00
|
|
|
*/
|
2020-04-23 11:51:48 +00:00
|
|
|
export class Tracing {
|
2022-06-13 09:16:25 +00:00
|
|
|
#client: CDPSession;
|
|
|
|
#recording = false;
|
|
|
|
#path?: string;
|
2020-04-23 11:51:48 +00:00
|
|
|
|
2020-06-24 12:57:59 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-23 11:51:48 +00:00
|
|
|
constructor(client: CDPSession) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
2017-08-02 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 12:57:59 +00:00
|
|
|
/**
|
|
|
|
* Starts a trace for the current page.
|
|
|
|
* @remarks
|
|
|
|
* Only one trace can be active at a time per browser.
|
2022-06-13 09:16:25 +00:00
|
|
|
*
|
2020-06-24 12:57:59 +00:00
|
|
|
* @param options - Optional `TracingOptions`.
|
|
|
|
*/
|
2020-04-23 11:51:48 +00:00
|
|
|
async start(options: TracingOptions = {}): Promise<void> {
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2022-06-13 09:16:25 +00:00
|
|
|
!this.#recording,
|
2020-05-07 10:54:55 +00:00
|
|
|
'Cannot start recording trace while already recording trace.'
|
|
|
|
);
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2017-12-04 02:36:34 +00:00
|
|
|
const defaultCategories = [
|
2020-05-07 10:54:55 +00:00
|
|
|
'-*',
|
|
|
|
'devtools.timeline',
|
|
|
|
'v8.execute',
|
|
|
|
'disabled-by-default-devtools.timeline',
|
|
|
|
'disabled-by-default-devtools.timeline.frame',
|
|
|
|
'toplevel',
|
|
|
|
'blink.console',
|
|
|
|
'blink.user_timing',
|
|
|
|
'latencyInfo',
|
|
|
|
'disabled-by-default-devtools.timeline.stack',
|
|
|
|
'disabled-by-default-v8.cpu_profiler',
|
2017-08-02 17:45:11 +00:00
|
|
|
];
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
2022-05-31 14:34:16 +00:00
|
|
|
path,
|
2018-11-12 20:59:21 +00:00
|
|
|
screenshots = false,
|
|
|
|
categories = defaultCategories,
|
|
|
|
} = options;
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (screenshots) {
|
|
|
|
categories.push('disabled-by-default-devtools.screenshot');
|
|
|
|
}
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2021-09-10 20:23:35 +00:00
|
|
|
const excludedCategories = categories
|
2022-06-15 10:42:21 +00:00
|
|
|
.filter((cat) => {
|
|
|
|
return cat.startsWith('-');
|
|
|
|
})
|
|
|
|
.map((cat) => {
|
|
|
|
return cat.slice(1);
|
|
|
|
});
|
|
|
|
const includedCategories = categories.filter((cat) => {
|
|
|
|
return !cat.startsWith('-');
|
|
|
|
});
|
2021-09-10 20:23:35 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#path = path;
|
|
|
|
this.#recording = true;
|
|
|
|
await this.#client.send('Tracing.start', {
|
2017-08-02 17:45:11 +00:00
|
|
|
transferMode: 'ReturnAsStream',
|
2021-09-10 20:23:35 +00:00
|
|
|
traceConfig: {
|
|
|
|
excludedCategories,
|
|
|
|
includedCategories,
|
|
|
|
},
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-24 12:57:59 +00:00
|
|
|
/**
|
|
|
|
* Stops a trace started with the `start` method.
|
|
|
|
* @returns Promise which resolves to buffer with trace data.
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
async stop(): Promise<Buffer | undefined> {
|
|
|
|
let resolve: (value: Buffer | undefined) => void;
|
2020-07-23 14:21:15 +00:00
|
|
|
let reject: (err: Error) => void;
|
2022-05-31 14:34:16 +00:00
|
|
|
const contentPromise = new Promise<Buffer | undefined>((x, y) => {
|
|
|
|
resolve = x;
|
2020-07-23 14:21:15 +00:00
|
|
|
reject = y;
|
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client.once('Tracing.tracingComplete', async (event) => {
|
2021-06-23 12:51:38 +00:00
|
|
|
try {
|
2022-06-14 11:16:21 +00:00
|
|
|
const readable = await getReadableFromProtocolStream(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client,
|
2021-06-23 12:51:38 +00:00
|
|
|
event.stream
|
|
|
|
);
|
2022-06-14 11:16:21 +00:00
|
|
|
const buffer = await getReadableAsBuffer(readable, this.#path);
|
2022-05-31 14:34:16 +00:00
|
|
|
resolve(buffer ?? undefined);
|
2021-06-23 12:51:38 +00:00
|
|
|
} catch (error) {
|
2022-06-10 13:27:42 +00:00
|
|
|
if (isErrorLike(error)) {
|
2022-05-31 14:34:16 +00:00
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
reject(new Error(`Unknown error: ${error}`));
|
|
|
|
}
|
2021-06-23 12:51:38 +00:00
|
|
|
}
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Tracing.end');
|
|
|
|
this.#recording = false;
|
2017-08-02 17:45:11 +00:00
|
|
|
return contentPromise;
|
|
|
|
}
|
|
|
|
}
|