-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.html
More file actions
273 lines (249 loc) · 8.28 KB
/
benchmark.html
File metadata and controls
273 lines (249 loc) · 8.28 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<meta name="viewport" content="width=device-width">
<html>
<head>
<title>Rendering Micro Benchmark</title>
</head>
<body>
<div id="benchmarkContainer">
<style>
body {
font-family: sans-serif;
}
#container {
display: flex;
}
#input, #preview {
width: 50%;
padding: 10px;
border: 1px solid #ccc;
}
#input {
white-space: pre;
overflow-x: auto;
}
#preview {
background-color: #f5f5f5;
}
#preview h4 {
margin-bottom: 5px;
}
#preview iframe {
border: none;
width: 100%;
height: 100px;
}
button {
margin: 10px 0;
padding: 8px 16px;
}
.warning {
padding: 10px;
background: orange;
color: white;
margin-bottom: 15px;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
}
</style>
<h1>Rendering Micro Benchmark</h1>
<p>Enter HTML snippets below to benchmark their performance.</p>
<div id="container">
<textarea id="input" rows="10"></textarea>
<div id="preview"></div>
</div>
<button title="Copy a persistent URL" id="copyLink">Copy Link</button>
<button id="run">Run Benchmark</button>
<div id="results"></div>
</div>
<script type="module">
import { benchmark, Snippet, TimeStats } from './src/runner.js';
import { compress, decompress } from './src/gzip.js';
onload = async () => {
input.addEventListener('input', () => {
updatePreview();
updateSnippetWarnings();
});
run.addEventListener('click', () => {
runBenchmark();
});
// Generate a persistent url with the current snippets as a url param.
copyLink.addEventListener('click', async () => {
const copyUrl = new URL(window.location.href);
const compressed = await compress(input.value);
copyUrl.searchParams.set('snippets', compressed);
navigator.clipboard.writeText(copyUrl.toString());
});
// If set, use the `snippets` url parameter for snippets. Otherwise, use a
// set of predefined examples.
const currentURL = new URL(window.location.href);
const currentSnippets = currentURL.searchParams.get('snippets');
if (currentSnippets) {
input.value = await decompress(currentSnippets);
} else {
input.value = examples.trim();
}
updatePreview();
updateSnippetWarnings();
checkCrossOriginIsolated();
}
function updatePreview() {
preview.innerHTML = '<h2>Preview</h2>';
for (const snippet of getSnippets()) {
let previewTitle = document.createElement('h4');
previewTitle.textContent = snippet.name;
preview.appendChild(previewTitle);
// Modify the snippet html to simulate how the benchmark runner uses
// unique ids to prevent caching (see: `generateUnique()`).
let uniqueName = snippet.name + '_' + 123456789;
let snippetHtml = snippet.html.replaceAll(snippet.name, uniqueName);
// Use an iframe to isolate each snippet preview. For example, this
// prevents a style tag in one snippet from applying to other snippets.
let container = document.createElement('iframe');
container.srcdoc = `<!DOCTYPE html>${snippetHtml}`;
preview.appendChild(container);
}
}
async function runBenchmark() {
const snippets = getSnippets();
let benchmarkContainer = document.getElementById('benchmarkContainer');
benchmarkContainer.remove();
let snippetStats = await benchmark(snippets, document.body);
document.body.replaceChildren(benchmarkContainer);
results.innerHTML = formatResults(snippetStats);
}
// Return an array of `Snippet`s from #input.
function getSnippets() {
let snippets = [];
let scratch = document.createElement('div');
scratch.innerHTML = input.value;
for (const div of scratch.querySelectorAll('snippet'))
snippets.push(new Snippet(div.getAttribute('name'), div.innerHTML));
return snippets;
}
// Shows or hides a warning with `id`.
function updateWarning(show, id, message) {
let element = document.getElementById(id);
if (show) {
if (!element) {
element = document.createElement('div');
element.id = id;
element.classList.add('warning');
benchmarkContainer.prepend(element);
}
element.innerHTML = `<b>Warning:</b> ${message}`;
} else if (element) {
element.remove();
}
}
// Show warnings for snippet issues.
function updateSnippetWarnings() {
let showMissingName = false;
let showMissingIncludeName = false;
for (const snippet of getSnippets()) {
if (!snippet.name) {
showMissingName = true;
} else if (!snippet.html.includes(snippet.name)) {
showMissingIncludeName = true;
}
}
updateWarning(showMissingName, 'missing-name-warning', `Missing
<code>name</code> attribute on <snippet>.`);
updateWarning(showMissingIncludeName, 'missing-include-name-warning', `To
prevent caching, <snippet> contents must contain the snippet name.`);
}
// If the page is not loaded with cross-origin isolation headers, timer
// precision (performance.now(), etc.) is much worse. Below is a comparison
// with vs without isolation:
// Chromium: 5us vs 100us
// WebKit: 20us vs 1000us
// Gecko: 100us vs 1000us
function checkCrossOriginIsolated() {
updateWarning(!self.crossOriginIsolated, 'isolated-warning', `Timer
precision is reduced because this page is not
<a href="https://mdn.io/crossOriginIsolated">cross-origin isolated</a>.
Fix by using <code>python3 server.py</code>.`);
}
function formatResults(snippetStats) {
function formatNumber(num) { return parseFloat(num.toFixed(2)); }
let resultsHtml = `<h2>Results</h2>
<table>
<tr>
<th rowspan="2">Snippet Name</th>
<th colspan="2">Total Time (ms)</th>
<th colspan="2">Parse Time (ms)</th>
<th colspan="2">Style Time (ms)</th>
<th colspan="2">Layout Time (ms)</th>
<th colspan="2">Paint Time (ms)</th>
</tr>
<tr>
<th>Avg</th>
<th>Std Dev</th>
<th>Avg</th>
<th>Std Dev</th>
<th>Avg</th>
<th>Std Dev</th>
<th>Avg</th>
<th>Std Dev</th>
<th>Avg</th>
<th>Std Dev</th>
</tr>`;
for (const stats of snippetStats) {
resultsHtml += `<tr>
<td>${stats.name}</td>
<td>${formatNumber(stats.totalAvg)}</td>
<td>${formatNumber(stats.totalStdDev)}</td>
<td>${formatNumber(stats.parseAvg)}</td>
<td>${formatNumber(stats.parseStdDev)}</td>
<td>${formatNumber(stats.styleAvg)}</td>
<td>${formatNumber(stats.styleStdDev)}</td>
<td>${formatNumber(stats.layoutAvg)}</td>
<td>${formatNumber(stats.layoutStdDev)}</td>
<td>${formatNumber(stats.paintAvg)}</td>
<td>${formatNumber(stats.paintStdDev)}</td>
</tr>`;
}
resultsHtml += `</table>`;
return resultsHtml;
}
const examples = `
<snippet name="svg-image">
<style>
.svg-image {
width: 24px;
height: 24px;
background-image: url('data:image/svg+xml,<svg uinqueid="svg-image" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path></svg>');
}
</style>
<div class="svg-image"></div>
</snippet>
<snippet name="inline-svg">
<style>
.inline-svg {
width: 24px;
height: 24px;
}
</style>
<svg class="inline-svg" viewBox="0 0 24 24">
<path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path>
</svg>
</snippet>
<snippet name="css-clip-path">
<style>
.css-clip-path {
clip-path: path('M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z');
width: 24px;
height: 24px;
background-color: black;
}
</style>
<div class="css-clip-path"></div>
</snippet>`;
</script>
</body>
</html>