Skip to content

Instantly share code, notes, and snippets.

@paulirish
Last active April 26, 2020 01:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulirish/7fbadc9556c9dc97e9ceac6cf078a5cb to your computer and use it in GitHub Desktop.
Save paulirish/7fbadc9556c9dc97e9ceac6cf078a5cb to your computer and use it in GitHub Desktop.
Expected Queueing Time metric
// Expected Queueing Time
// https://docs.google.com/document/d/1Vgu7-R84Ym3lbfTRi98vpdspRr1UwORB4UV-p9K1FF0/edit
// Initial impl by Nicolás Peña (npm), Tim Dresser (tdresser)
// Usage:
// var eqt = EQT.begin();
// // ...
// const {expectedQueueingTime} = EQT.end();
class EQT {
constructor() {
this.longTaskObserver;
this.beginTime;
this.endTime;
this.taskDurations = [];
}
static begin() {
const eqt = new EQT();
eqt.beginTime = performance.now();
eqt.longTaskObserver = new PerformanceObserver(entryList => {
for (const entry of entryList.getEntries()) eqt.taskDurations.push(entry.duration);
});
eqt.longTaskObserver.observe({entryTypes: ['longtask']});
return eqt;
}
end() {
this.longTaskObserver.disconnect();
this.endTime = performance.now();
const totalDuration = this.endTime - this.beginTime;
const expectedQueueingTime = this.taskDurations.reduce(
(sum, duration) => (sum += (duration ** 2) / (2 * totalDuration)),
0
);
return {expectedQueueingTime};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment