1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createProfiler = exports.createNoopProfiler = void 0;
|
4 | function createNoopProfiler() {
|
5 | return {
|
6 | run(fn) {
|
7 | return Promise.resolve().then(() => fn());
|
8 | },
|
9 | collect() {
|
10 | return [];
|
11 | },
|
12 | };
|
13 | }
|
14 | exports.createNoopProfiler = createNoopProfiler;
|
15 | function createProfiler() {
|
16 | const events = [];
|
17 | return {
|
18 | collect() {
|
19 | return events;
|
20 | },
|
21 | run(fn, name, cat) {
|
22 | let startTime;
|
23 | return Promise.resolve()
|
24 | .then(() => {
|
25 | startTime = process.hrtime();
|
26 | })
|
27 | .then(() => fn())
|
28 | .then(value => {
|
29 | const duration = process.hrtime(startTime);
|
30 |
|
31 |
|
32 | const event = {
|
33 | name,
|
34 | cat,
|
35 | ph: 'X',
|
36 | ts: hrtimeToMicroseconds(startTime),
|
37 | pid: 1,
|
38 | tid: 0,
|
39 | dur: hrtimeToMicroseconds(duration),
|
40 | };
|
41 | events.push(event);
|
42 | return value;
|
43 | });
|
44 | },
|
45 | };
|
46 | }
|
47 | exports.createProfiler = createProfiler;
|
48 | function hrtimeToMicroseconds(hrtime) {
|
49 | return (hrtime[0] * 1e9 + hrtime[1]) / 1000;
|
50 | }
|