| 180 | } |
| 181 | |
| 182 | async function saveTrace(session, file, operation) { |
| 183 | await session.send('Tracing.start', { |
| 184 | // toplevel adds task boundaries, so scheduling overhead shows as other work. |
| 185 | categories: |
| 186 | 'toplevel,devtools.timeline,blink.user_timing,disabled-by-default-devtools.timeline.frame', |
| 187 | transferMode: 'ReturnAsStream' |
| 188 | }); |
| 189 | await operation(); |
| 190 | let completed = new Promise(resolve => session.once('Tracing.tracingComplete', resolve)); |
| 191 | await session.send('Tracing.end'); |
| 192 | let {stream} = await completed; |
| 193 | let output = await fs.open(file, 'w'); |
| 194 | try { |
| 195 | let eof = false; |
| 196 | while (!eof) { |
| 197 | let chunk = await session.send('IO.read', {handle: stream}); |
| 198 | await output.write(chunk.base64Encoded ? Buffer.from(chunk.data, 'base64') : chunk.data); |
| 199 | eof = chunk.eof; |
| 200 | } |
| 201 | } finally { |
| 202 | await output.close(); |
| 203 | await session.send('IO.close', {handle: stream}); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Base and candidate pages stay open side by side, and their trials alternate in |
| 208 | // ABBA order so drift within a launch affects both arms alike. `onTrial` reports the |