Log a warning if task queues exceed deadline by 20ms

Fixes #4292
This commit is contained in:
Daniel Imms
2022-12-06 14:04:10 -08:00
parent ad1f2ac2a8
commit dec3791f4c
+10 -1
View File
@@ -66,6 +66,8 @@ abstract class TaskQueue implements ITaskQueue {
this._idleCallback = undefined;
let taskDuration = 0;
let longestTask = 0;
let lastDeadlineRemaining = deadline.timeRemaining();
let deadlineRemaining = 0;
while (this._i < this._tasks.length) {
taskDuration = performance.now();
this._tasks[this._i++]();
@@ -73,10 +75,17 @@ abstract class TaskQueue implements ITaskQueue {
longestTask = Math.max(taskDuration, longestTask);
// Guess the following task will take a similar time to the longest task in this batch, allow
// additional room to try avoid exceeding the deadline
if (longestTask * 1.5 > deadline.timeRemaining()) {
deadlineRemaining = deadline.timeRemaining();
if (longestTask * 1.5 > deadlineRemaining) {
// Warn when the time exceeding the deadline is over 20ms, if this happens in practice the
// task should be split into sub-tasks to ensure the UI remains responsive.
if (lastDeadlineRemaining - taskDuration < -5) {
console.warn(`task queue exceeded allotted deadline by ${Math.abs(Math.round(lastDeadlineRemaining - taskDuration))}ms`, { stacktrace: new Error().stack });
}
this._start();
return;
}
lastDeadlineRemaining = deadlineRemaining;
}
this.clear();
}