-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mjs
188 lines (149 loc) · 5.06 KB
/
main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { FpsTracker } from './FpsTracker.mjs';
import { CanvasFps } from './CanvasFps.mjs';
import { ThreadLocalRAFIterator, SendPostMessageRAF } from './AnimationFrameIterator.mjs';
// Set up OffscreenCanvas WebWorker
const canvas_wrkr_raf_main = document.querySelector('#canvas_wrkr_raf_main').transferControlToOffscreen();
const canvas_wrkr_raf_wrkr = document.querySelector('#canvas_wrkr_raf_wrkr').transferControlToOffscreen();
const worker = new Worker('./CanvasWorker.mjs', { type: 'module' });
worker.postMessage({
msg: 'start',
canvas_wrkr_raf_main,
canvas_wrkr_raf_wrkr
}, [ canvas_wrkr_raf_main, canvas_wrkr_raf_wrkr ]);
// Start tracking frames on main, reporting frame times to worker
(async () => {
const fpsTracker = new FpsTracker(5000);
const canvas_main_raf_main = document.querySelector('#canvas_main_raf_main');
const c = new CanvasFps(canvas_main_raf_main.getContext('2d'), fpsTracker);
c.startDrawing();
for await (let frameTime of ThreadLocalRAFIterator()) {
let fps = fpsTracker.reportNewFrame(frameTime);
SendPostMessageRAF(worker, frameTime);
}
})();
// Start adding Long Tasks on main
(async function() {
function blog(text) {
const el = document.getElementById('long_task_tracker');
el.textContent = text;
}
function block(block_ms) {
blog(`blocked for ${block_ms.toFixed(0)}ms`);
let now = performance.now();
let end = now + block_ms;
while (now < end) {
now = performance.now();
}
}
// Will delay at least one single animation frame
async function block_with_delay(block_ms, delay_ms = 0) {
const then = performance.now() + delay_ms;
for await( let time of ThreadLocalRAFIterator() ) {
// blog(`will block for ${block_ms.toFixed(0)}ms, in ${(then-time).toFixed(0)}ms`);
if (time >= then) break;
}
block(block_ms);
}
let interval;
function toggleLongTasks() {
if (interval) {
interval = clearInterval(interval);
} else {
// setInterval default (min) interval is 4ms
interval = setInterval(() => {
//block((1000/90)/2);
block(100);
// block_with_delay((1000/90)/2);
});
}
}
// await block_with_delay(5000, 5000);
toggleLongTasks();
document.getElementById('t').addEventListener('keydown', (evt) => {
block(100);
});
})();
// https://dbaron.org/log/20100309-faster-timeouts
// Only add setZeroTimeout to the window object, and hide everything
// else in a closure.
(function() {
var timeouts = [];
var messageName = "zero-timeout-message";
// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeout(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
}
function handleMessage(event) {
if (event.source == window && event.data == messageName) {
event.stopPropagation();
if (timeouts.length > 0) {
var fn = timeouts.shift();
fn();
}
}
}
window.addEventListener("message", handleMessage, true);
// Add the one thing we want added to the window object.
window.setZeroTimeout = setZeroTimeout;
})();
export async function reportTimeToNextFrame() {
let start = performance.now();
let id = `MyFrame-${(Math.random() * 100000).toFixed(0)}`;
let p = document.createElement('div');
p.id = id;
p.innerText = '.';
p.setAttribute('elementtiming', id);
document.body.appendChild(p);
let viaElementTiming = new Promise(resolve => {
// TODO, wrap this in a promise
const observer = new PerformanceObserver(entryList => {
for (const entry of entryList.getEntries()) {
if (entry.identifier != id) continue;
let duration = entry.renderTime - start;
console.log('via Element Timing', duration, start, entry);
resolve(duration);
}
observer.disconnect();
});
observer.observe({type: 'element'});
});
let viaSingleRaf = new Promise(resolve => {
requestAnimationFrame((t1) => {
let duration = t1 - start;
console.log('via Single rAF', duration, t1);
resolve(duration);
})
});
let viaDoubleRaf = new Promise(resolve => {
requestAnimationFrame((t1) => {
requestAnimationFrame((t2) => {
let duration = t2 - start;
console.log('via Double rAF', duration, t1, t2);
resolve(duration);
})
})
});
let viaRafNTask = new Promise(resolve => {
requestAnimationFrame((t1) => {
// TODO: try out different types of task scheduling methods to get higher priority task
// See: https://github.com/WICG/scheduling-apis/blob/main/explainers/prioritized-post-task.md
// setZeroTimeout(() => {
setTimeout(() => {
let t2 = performance.now();
let duration = t2 - start;
console.log('via rAF n Task', duration, t1, t2);
resolve(duration);
}, 0);
})
});
return Promise.all([
viaElementTiming,
viaSingleRaf,
viaDoubleRaf,
viaRafNTask,
]);
}
window.reportTimeToNextFrame = reportTimeToNextFrame;