UNPKG

2.27 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16const {helper, assert} = require('./helper');
17
18class Tracing {
19 /**
20 * @param {!Puppeteer.CDPSession} client
21 */
22 constructor(client) {
23 this._client = client;
24 this._recording = false;
25 this._path = '';
26 }
27
28 /**
29 * @param {!{path?: string, screenshots?: boolean, categories?: !Array<string>}} options
30 */
31 async start(options = {}) {
32 assert(!this._recording, 'Cannot start recording trace while already recording trace.');
33
34 const defaultCategories = [
35 '-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
36 'disabled-by-default-devtools.timeline.frame', 'toplevel',
37 'blink.console', 'blink.user_timing', 'latencyInfo', 'disabled-by-default-devtools.timeline.stack',
38 'disabled-by-default-v8.cpu_profiler', 'disabled-by-default-v8.cpu_profiler.hires'
39 ];
40 const {
41 path = null,
42 screenshots = false,
43 categories = defaultCategories,
44 } = options;
45
46 if (screenshots)
47 categories.push('disabled-by-default-devtools.screenshot');
48
49 this._path = path;
50 this._recording = true;
51 await this._client.send('Tracing.start', {
52 transferMode: 'ReturnAsStream',
53 categories: categories.join(',')
54 });
55 }
56
57 /**
58 * @return {!Promise<!Buffer>}
59 */
60 async stop() {
61 let fulfill;
62 const contentPromise = new Promise(x => fulfill = x);
63 this._client.once('Tracing.tracingComplete', event => {
64 helper.readProtocolStream(this._client, event.stream, this._path).then(fulfill);
65 });
66 await this._client.send('Tracing.end');
67 this._recording = false;
68 return contentPromise;
69 }
70}
71
72module.exports = Tracing;