@stringsync/vexml
Version:
MusicXML to Vexflow
34 lines (33 loc) • 994 B
JavaScript
export class PerformanceMonitor {
log;
thresholdMs;
constructor(log, thresholdMs) {
this.log = log;
this.thresholdMs = thresholdMs;
}
check(elapsedMs, meta) {
if (elapsedMs >= this.thresholdMs) {
this.log.warn(`[SLOW WARNING] ${this.inferMethodName()} took ${this.getElapsedStr(elapsedMs)}`, meta);
}
}
inferMethodName() {
try {
return (new Error().stack
?.split('\n')
.at(3)
?.trimStart()
.replace('at ', '')
?.match(/(.+)\s/)
?.at(0)
?.trimEnd() ?? '<unknown>');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (e) {
// Fallback if stack parsing fails
}
return '<unknown>';
}
getElapsedStr(elapsedMs) {
return elapsedMs > 1 ? `${Math.round(elapsedMs)}ms` : `${elapsedMs.toFixed(3)}ms`;
}
}