cables_dev/cables/src/core/core_profiler.js
import { now } from "./timer.js";
class Profiler
{
constructor(patch)
{
this.startFrame = patch.getFrameNum();
this.items = {};
this.currentId = null;
this.currentStart = 0;
this._patch = patch;
}
getItems()
{
return this.items;
}
clear()
{
if (this.paused) return;
this.items = {};
}
togglePause()
{
this.paused = !this.paused;
if (!this.paused)
{
this.items = {};
this.currentStart = performance.now();
}
}
add(type, object)
{
if (this.paused) return;
if (this.currentId !== null)
{
if (!object || object.id != this.currentId)
{
if (this.items[this.currentId])
{
this.items[this.currentId].timeUsed += performance.now() - this.currentStart;
if (!this.items[this.currentId].peakTime || now() - this.items[this.currentId].peakTime > 5000)
{
this.items[this.currentId].peak = 0;
this.items[this.currentId].peakTime = now();
}
this.items[this.currentId].peak = Math.max(this.items[this.currentId].peak, performance.now() - this.currentStart);
}
}
}
if (object !== null)
{
if (!this.items[object.id])
{
this.items[object.id] = {
"numTriggers": 0,
"timeUsed": 0,
};
}
if (this.items[object.id].lastFrame != this._patch.getFrameNum()) this.items[object.id].numTriggers = 0;
this.items[object.id].lastFrame = this._patch.getFrameNum();
this.items[object.id].numTriggers++;
this.items[object.id].opid = object.op.id;
this.items[object.id].title = object.op.name + "." + object.name;
this.items[object.id].subPatch = object.op.uiAttribs.subPatch;
this.currentId = object.id;
this.currentStart = performance.now();
}
else
{
this.currentId = null;
}
}
print()
{
console.log("--------");
for (const i in this.items)
{
console.log(this.items[i].title + ": " + this.items[i].numTriggers + " / " + this.items[i].timeUsed);
}
}
}
export { Profiler };