Tracing.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. */
  16. const {helper, assert} = require('./helper');
  17. class Tracing {
  18. /**
  19. * @param {!Puppeteer.CDPSession} client
  20. */
  21. constructor(client) {
  22. this._client = client;
  23. this._recording = false;
  24. this._path = '';
  25. }
  26. /**
  27. * @param {!{path?: string, screenshots?: boolean, categories?: !Array<string>}} options
  28. */
  29. async start(options = {}) {
  30. assert(!this._recording, 'Cannot start recording trace while already recording trace.');
  31. const defaultCategories = [
  32. '-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
  33. 'disabled-by-default-devtools.timeline.frame', 'toplevel',
  34. 'blink.console', 'blink.user_timing', 'latencyInfo', 'disabled-by-default-devtools.timeline.stack',
  35. 'disabled-by-default-v8.cpu_profiler', 'disabled-by-default-v8.cpu_profiler.hires'
  36. ];
  37. const {
  38. path = null,
  39. screenshots = false,
  40. categories = defaultCategories,
  41. } = options;
  42. if (screenshots)
  43. categories.push('disabled-by-default-devtools.screenshot');
  44. this._path = path;
  45. this._recording = true;
  46. await this._client.send('Tracing.start', {
  47. transferMode: 'ReturnAsStream',
  48. categories: categories.join(',')
  49. });
  50. }
  51. /**
  52. * @return {!Promise<!Buffer>}
  53. */
  54. async stop() {
  55. let fulfill;
  56. const contentPromise = new Promise(x => fulfill = x);
  57. this._client.once('Tracing.tracingComplete', event => {
  58. helper.readProtocolStream(this._client, event.stream, this._path).then(fulfill);
  59. });
  60. await this._client.send('Tracing.end');
  61. this._recording = false;
  62. return contentPromise;
  63. }
  64. }
  65. module.exports = Tracing;