UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createProfiler = exports.createNoopProfiler = void 0;
4function createNoopProfiler() {
5 return {
6 run(fn) {
7 return Promise.resolve().then(() => fn());
8 },
9 collect() {
10 return [];
11 },
12 };
13}
14exports.createNoopProfiler = createNoopProfiler;
15function 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 // Trace Event Format documentation:
31 // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
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}
47exports.createProfiler = createProfiler;
48function hrtimeToMicroseconds(hrtime) {
49 return (hrtime[0] * 1e9 + hrtime[1]) / 1000;
50}