1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const {helper, assert} = require('./helper');
|
17 |
|
18 | class Tracing {
|
19 | |
20 |
|
21 |
|
22 | constructor(client) {
|
23 | this._client = client;
|
24 | this._recording = false;
|
25 | this._path = '';
|
26 | }
|
27 |
|
28 | |
29 |
|
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 |
|
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 |
|
72 | module.exports = Tracing;
|