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.
|
|
|
|
*/
|
2018-07-30 23:41:39 +00:00
|
|
|
const {helper, assert} = require('./helper');
|
2017-08-02 17:45:11 +00:00
|
|
|
|
|
|
|
class Tracing {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-08-02 17:45:11 +00:00
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
this._client = client;
|
|
|
|
this._recording = false;
|
2017-08-02 22:41:05 +00:00
|
|
|
this._path = '';
|
2017-08-02 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-10 00:15:33 +00:00
|
|
|
* @param {!{path?: string, screenshots?: boolean, categories?: !Array<string>}} options
|
2017-08-02 17:45:11 +00:00
|
|
|
*/
|
2019-05-10 00:15:33 +00:00
|
|
|
async start(options = {}) {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!this._recording, '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 = [
|
2017-08-02 17:45:11 +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',
|
2018-06-14 17:33:05 +00:00
|
|
|
'disabled-by-default-v8.cpu_profiler', 'disabled-by-default-v8.cpu_profiler.hires'
|
2017-08-02 17:45:11 +00:00
|
|
|
];
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
|
|
|
path = null,
|
|
|
|
screenshots = false,
|
|
|
|
categories = defaultCategories,
|
|
|
|
} = options;
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2018-11-12 20:59:21 +00:00
|
|
|
if (screenshots)
|
|
|
|
categories.push('disabled-by-default-devtools.screenshot');
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2018-11-12 20:59:21 +00:00
|
|
|
this._path = path;
|
2017-08-02 17:45:11 +00:00
|
|
|
this._recording = true;
|
|
|
|
await this._client.send('Tracing.start', {
|
|
|
|
transferMode: 'ReturnAsStream',
|
2018-11-12 20:59:21 +00:00
|
|
|
categories: categories.join(',')
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-08 06:48:43 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Buffer>}
|
|
|
|
*/
|
2017-08-02 22:41:05 +00:00
|
|
|
async stop() {
|
2017-08-02 17:45:11 +00:00
|
|
|
let fulfill;
|
2017-08-21 23:39:04 +00:00
|
|
|
const contentPromise = new Promise(x => fulfill = x);
|
2017-08-02 17:45:11 +00:00
|
|
|
this._client.once('Tracing.tracingComplete', event => {
|
2019-06-15 05:36:06 +00:00
|
|
|
helper.readProtocolStream(this._client, event.stream, this._path).then(fulfill);
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
|
|
|
await this._client.send('Tracing.end');
|
|
|
|
this._recording = false;
|
|
|
|
return contentPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Tracing;
|