1 | "use strict";
|
2 |
|
3 | const MEASURE_TESTIM_CLI_PERFORMANCE = process.env.MEASURE_TESTIM_CLI_PERFORMANCE;
|
4 | let epoch = Date.now();
|
5 | let last = Date.now();
|
6 |
|
7 | module.exports = {
|
8 | log(...args) {
|
9 | if (!MEASURE_TESTIM_CLI_PERFORMANCE) {
|
10 | return;
|
11 | }
|
12 | let time = Date.now();
|
13 | console.log(`${time - epoch}\t\t\t${time - last}\t\t\t`, ...args);
|
14 | last = time;
|
15 | },
|
16 | measure(fn) {
|
17 | if (!MEASURE_TESTIM_CLI_PERFORMANCE) {
|
18 | return;
|
19 | }
|
20 | const start = Date.now();
|
21 | try {
|
22 | fn();
|
23 | } finally {
|
24 | console.log(fn.name, 'took', Date.now() - start);
|
25 | }
|
26 | },
|
27 | patchPromisePrototype() {
|
28 | Promise.prototype.log = function log (message) {
|
29 | if (!MEASURE_TESTIM_CLI_PERFORMANCE) {
|
30 | return this;
|
31 | }
|
32 | return this.then((v) => {
|
33 | module.exports.log(message);
|
34 | return v;
|
35 | });
|
36 | };
|
37 |
|
38 | require('bluebird').prototype.log = function log(message) {
|
39 | if (!MEASURE_TESTIM_CLI_PERFORMANCE) {
|
40 | return this;
|
41 | }
|
42 | return this.tap(() => module.exports.log(message));
|
43 | }
|
44 | },
|
45 | measureRequireTimes() {
|
46 | if (!MEASURE_TESTIM_CLI_PERFORMANCE) {
|
47 | return;
|
48 | }
|
49 | const {
|
50 | performance,
|
51 | PerformanceObserver
|
52 | } = require('perf_hooks');
|
53 | const mod = require('module');
|
54 |
|
55 |
|
56 | mod.Module.prototype.require = performance.timerify(mod.Module.prototype.require);
|
57 | require = performance.timerify(require);
|
58 |
|
59 |
|
60 | const obs = new PerformanceObserver((list) => {
|
61 | const entries = list.getEntries();
|
62 | entries.sort((a, b) => b.duration - a.duration).filter(x => x.duration > 1).forEach((entry) => {
|
63 | console.log(`require('${entry[0]}')`, entry.duration);
|
64 | });
|
65 | obs.disconnect();
|
66 | });
|
67 | obs.observe({ entryTypes: ['function'], buffered: true });
|
68 | }
|
69 | }
|
70 |
|
71 |
|
72 | module.exports.patchPromisePrototype();
|