-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ts
38 lines (35 loc) · 1.03 KB
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createHook } from "node:async_hooks";
import { format } from "node:util";
import { writeFileSync } from "node:fs";
const asyncHook = createHook({ init, destroy });
function debug(...args) {
writeFileSync(1, `${format(...args)}\n`, { flag: "a" });
}
const TIMED_TYPES = ["Timeout", "Immediate", "PROMISE", "Microtask"];
const timers = new Map();
const resources = new Map();
function init(id, type, _, resource) {
if (!TIMED_TYPES.includes(type)) {
return;
}
// debug(`Init ${type} ${id}`);
if (!timers.has(id)) {
timers.set(id, process.hrtime());
resources.set(id, resource);
}
}
function destroy(id) {
if (timers.has(id)) {
const start = timers.get(id);
const resource = resources.get(id);
const elapsedTimes = process.hrtime(start);
const elapsedTime =
(elapsedTimes[0] * 1000000000 + elapsedTimes[1]) / 1000000;
if (elapsedTime > 1000) {
//debug(resource);
//debug(`*** ${id} (${String(resource)}) took ${elapsedTime} ms`);
}
timers.delete(id);
}
}
asyncHook.enable();