2022-06-10 15:38:46 -03:00
|
|
|
import { PerformanceObserver } from "perf_hooks";
|
|
|
|
|
2023-01-09 22:42:46 -03:00
|
|
|
import logger from "../logger";
|
|
|
|
|
2022-06-10 15:38:46 -03:00
|
|
|
declare global {
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var perfObserver: PerformanceObserver | undefined;
|
|
|
|
}
|
|
|
|
export const perfObserver =
|
|
|
|
globalThis.perfObserver ||
|
2022-06-14 12:39:50 -03:00
|
|
|
new PerformanceObserver((items) => {
|
2022-06-10 15:38:46 -03:00
|
|
|
items.getEntries().forEach((entry) => {
|
2022-06-27 21:38:01 -03:00
|
|
|
// Log entry duration in seconds with four decimal places.
|
2023-01-09 22:42:46 -03:00
|
|
|
logger.debug(entry.name.replace("$1", `${(entry.duration / 1000.0).toFixed(4)}s`));
|
2022-06-10 15:38:46 -03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-14 12:39:50 -03:00
|
|
|
perfObserver.observe({ entryTypes: ["measure"] });
|
|
|
|
|
2022-06-10 15:38:46 -03:00
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
|
|
globalThis.perfObserver = perfObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default perfObserver;
|
2022-06-14 12:32:37 -03:00
|
|
|
|
|
|
|
export { performance } from "perf_hooks";
|