Files
protoloon/js/Timeline.js
2026-04-02 17:39:02 -06:00

110 lines
2.2 KiB
JavaScript

/*
Copyright (c) 2023-forever Douglas Malnati. All rights reserved.
See the /faq/tos page for details.
(If this generated header is stamped on a file which is a 3rd party file or under a different license or copyright, then ignore this copyright statement and use that file's terms.)
*/
import * as utl from '/js/Utl.js';
export class Timeline
{
static global = new Timeline();
constructor()
{
this.logOnEvent = false;
this.ccGlobal = false;
this.noCc = false;
this.Reset();
}
Global()
{
return Timeline.global;
}
SetCcGlobal(tf)
{
this.ccGlobal = tf;
}
SetLogOnEvent(tf)
{
this.logOnEvent = tf;
}
Reset()
{
this.eventList = [];
this.longestStr = 0;
this.noCc = true;
this.Event(`Timeline::Reset`);
this.noCc = false;
}
Event(name)
{
if (this.ccGlobal && this != Timeline.global && !this.noCc)
{
this.Global().Event(name);
}
let time = performance.now();
this.eventList.push({
name: name,
time: time,
});
if (name.length > this.longestStr)
{
this.longestStr = name.length;
}
if (this.logOnEvent)
{
console.log(name);
}
return time;
}
Report(msg)
{
if (msg)
{
console.log(`Timeline report (${msg}):`);
}
else
{
console.log("Timeline report:");
}
// build table to output
let objList = [];
let totalMs = 0;
for (let i = 1; i < this.eventList.length; ++i)
{
totalMs += this.eventList[i - 0].time - this.eventList[i - 1].time;
objList.push({
from: this.eventList[i - 1].name,
to : this.eventList[i - 0].name,
diffMs: utl.Commas(Math.round(this.eventList[i - 0].time - this.eventList[i - 1].time)),
fromStartMs: utl.Commas(Math.round(totalMs)),
});
}
totalMs = utl.Commas(Math.round(totalMs));
console.table(objList);
console.log(`total ms: ${totalMs}`);
}
}