UNPKG

1.8 kBJavaScriptView Raw
1'use strict';
2
3function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
4
5// node.js builtin library
6const inspector = require('inspector');
7
8class Profiler {
9 constructor() {
10 this.session = undefined;
11 }
12
13 hasSession() {
14 return this.session != null;
15 }
16
17 startProfiling() {
18 var _this = this;
19
20 return _asyncToGenerator(function* () {
21 _this.session = new inspector.Session();
22 _this.session.connect();
23
24 return Promise.all([_this.sendCommand('Profiler.setSamplingInterval', {
25 interval: 100
26 }), _this.sendCommand('Profiler.enable'), _this.sendCommand('Profiler.start')]);
27 })();
28 }
29
30 sendCommand(method, params) {
31 var _this2 = this;
32
33 return _asyncToGenerator(function* () {
34 if (_this2.hasSession()) {
35 return new Promise(function (resolve, reject) {
36 _this2.session.post(method, params, function (err, params) {
37 if (err == null) {
38 resolve(params);
39 } else {
40 reject(err);
41 }
42 });
43 });
44 }
45 })();
46 }
47
48 destroy() {
49 if (this.hasSession()) {
50 this.session.disconnect();
51 }
52 }
53
54 stopProfiling() {
55 var _this3 = this;
56
57 return _asyncToGenerator(function* () {
58 const res = yield _this3.sendCommand('Profiler.stop');
59 _this3.destroy();
60 return res.profile;
61 })();
62 }
63}
64
65module.exports = Profiler;
\No newline at end of file