3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 00:05:01 +00:00
zeppelin/backend/src/utils/easyProfiler.ts
Dragory 45e3fe2ef0
chore: esm imports
This will make merging this into 'next' much easier.
2024-08-11 21:58:52 +03:00

52 lines
1.1 KiB
TypeScript

import type { Knub } from "knub";
import { performance } from "perf_hooks";
import { noop, SECONDS } from "../utils.js";
type Profiler = Knub["profiler"];
let _profilingEnabled = false;
export const profilingEnabled = () => {
return _profilingEnabled;
};
export const enableProfiling = () => {
_profilingEnabled = true;
};
export const disableProfiling = () => {
_profilingEnabled = false;
};
export const startProfiling = (profiler: Profiler, key: string) => {
if (!profilingEnabled()) {
return noop;
}
const startTime = performance.now();
return () => {
profiler.addDataPoint(key, performance.now() - startTime);
};
};
export const calculateBlocking = (coarseness = 10) => {
if (!profilingEnabled()) {
return () => 0;
}
let last = performance.now();
let result = 0;
const interval = setInterval(() => {
const now = performance.now();
const blockedTime = Math.max(0, now - last - coarseness);
result += blockedTime;
last = now;
}, coarseness);
setTimeout(() => clearInterval(interval), 10 * SECONDS);
return () => {
clearInterval(interval);
return result;
};
};