-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
486 lines (431 loc) · 16.4 KB
/
index.html
File metadata and controls
486 lines (431 loc) · 16.4 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LocalStorage First Read Benchmark</title>
<script>
// URL-driven automation modes (runs before DOM is ready):
//
// ?populate=10000&valueSize=100
// Writes entries to localStorage using seeded PRNG values,
// sets document.title to "POPULATED:<count>" when done.
//
// ?auto&delay=5000
// Waits <delay> ms (default 5000), then reads localStorage.length
// to trigger the first access. Sets document.title to
// "RESULT:<duration_ms>:<entry_count>".
(function() {
var params = new URLSearchParams(location.search);
// Simple seeded PRNG (mulberry32). Same seed always produces the
// same sequence, so both backends get identical data.
function mulberry32(seed) {
return function() {
seed |= 0; seed = seed + 0x6D2B79F5 | 0;
var t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function seededValue(index, length) {
var rng = mulberry32(index * 2654435761);
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var result = "";
for (var j = 0; j < length; j++) {
result += chars.charAt(Math.floor(rng() * chars.length));
}
return result;
}
if (params.has("populate")) {
var n = parseInt(params.get("populate")) || 1000;
var v = parseInt(params.get("valueSize")) || 100;
try {
localStorage.clear();
for (var i = 0; i < n; i++) {
localStorage.setItem("k_" + i, seededValue(i, v));
}
document.title = "POPULATED:" + localStorage.length;
} catch (e) {
document.title = "POPULATED:" + localStorage.length;
}
return;
}
if (params.has("auto")) {
var delay = parseInt(params.get("delay")) || 5000;
setTimeout(function() {
var t0 = performance.now();
var len = localStorage.length;
var t1 = performance.now();
var dur = t1 - t0;
document.title = "RESULT:" + dur.toFixed(3) + ":" + len;
}, delay);
return;
}
})();
</script>
<style>
* { box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif;
max-width: 820px;
margin: 2rem auto;
padding: 0 1rem;
line-height: 1.5;
}
h1 { margin-bottom: 0.25rem; }
h2 { font-size: 1.1rem; margin-top: 0; }
.subtitle { color: #666; margin-top: 0; }
.workflow {
background: #e8f0fe;
border: 1px solid #aecbfa;
border-radius: 6px;
padding: 1rem 1.25rem;
margin-bottom: 1.5rem;
}
.workflow strong { display: block; margin-bottom: 0.25rem; }
.workflow ol { margin: 0; padding-left: 1.5rem; }
.workflow li { margin-bottom: 0.15rem; }
.phase {
border: 1px solid #ddd;
border-radius: 8px;
padding: 1.25rem;
margin-bottom: 1.25rem;
}
.phase.highlight { border-color: #1a73e8; border-width: 2px; }
label { display: inline-block; margin-right: 1.5rem; margin-bottom: 0.5rem; }
select, input[type="number"] {
padding: 4px 8px;
border: 1px solid #aaa;
border-radius: 3px;
}
.data-size-note { font-size: 0.85rem; color: #666; margin-top: 0.25rem; }
button {
padding: 8px 16px;
margin-right: 0.5rem;
margin-bottom: 0.5rem;
border: 1px solid #aaa;
border-radius: 4px;
background: #f5f5f5;
cursor: pointer;
font-size: 0.9rem;
}
button:hover:not(:disabled) { background: #e0e0e0; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: #1a73e8; color: #fff; border-color: #1a73e8; }
button.primary:hover:not(:disabled) { background: #1557b0; }
button.danger { background: #d93025; color: #fff; border-color: #d93025; }
button.danger:hover:not(:disabled) { background: #a50e0e; }
button.large { padding: 12px 28px; font-size: 1.1rem; }
.status-bar {
padding: 0.75rem;
margin: 0.75rem 0;
border-radius: 4px;
background: #f0f0f0;
min-height: 2.5rem;
}
.status-bar.error { background: #fce8e6; color: #d93025; }
.status-bar.success { background: #e6f4ea; color: #137333; }
.result-display {
font-size: 2.5rem;
font-weight: bold;
font-family: "SF Mono", "Fira Mono", monospace;
color: #1a73e8;
margin: 0.5rem 0;
}
.result-display .unit { font-size: 1rem; color: #666; font-weight: normal; }
.result-details { color: #555; margin-bottom: 0.75rem; }
.warning {
background: #fef7e0;
border: 1px solid #f9e3a0;
border-radius: 4px;
padding: 0.75rem;
margin: 0.75rem 0;
font-size: 0.9rem;
}
#summary {
background: #f8f9fa;
border: 1px solid #ddd;
border-radius: 4px;
padding: 1rem;
margin: 1rem 0;
display: none;
}
#summary table { border-collapse: collapse; width: 100%; }
#summary td { padding: 4px 12px 4px 0; }
#summary td:last-child { font-weight: bold; font-family: monospace; }
</style>
</head>
<body>
<h1>LocalStorage First Read Benchmark</h1>
<p class="subtitle">
Measures the first access time for the localStorage backend in a fresh
browser instance.
</p>
<div class="workflow">
<strong>Benchmark workflow:</strong>
<ol>
<li><strong>Populate</strong> — write test entries to localStorage</li>
<li><strong>Close the browser</strong> — ensures data is persisted to disk</li>
<li><strong>Reopen the browser</strong> and navigate back to this page</li>
<li><strong>Measure</strong> — time the first localStorage read</li>
</ol>
</div>
<!-- ============================================================ -->
<!-- Populate -->
<!-- ============================================================ -->
<div class="phase" id="phase-populate">
<h2>Populate localStorage</h2>
<label>
Number of entries:
<select id="num-entries">
<option value="100">100</option>
<option value="500">500</option>
<option value="1000" selected>1,000</option>
<option value="5000">5,000</option>
<option value="10000">10,000</option>
</select>
</label>
<label>
Value size:
<select id="value-size">
<option value="10">Small (~10 chars)</option>
<option value="100" selected>Medium (~100 chars)</option>
<option value="1000">Large (~1,000 chars)</option>
</select>
</label>
<div class="data-size-note" id="data-size-note"></div>
<div style="margin-top: 0.75rem;">
<button id="btn-populate" class="primary">Populate</button>
<button id="btn-clear" class="danger">Clear</button>
</div>
<div class="status-bar" id="populate-status">Ready.</div>
</div>
<!-- ============================================================ -->
<!-- Measure -->
<!-- ============================================================ -->
<div class="phase highlight" id="phase-benchmark">
<h2>Run First Access Benchmark</h2>
<label>
Delay before read:
<select id="read-delay">
<option value="0" selected>None</option>
<option value="3000">3 seconds</option>
<option value="5000">5 seconds</option>
<option value="10000">10 seconds</option>
<option value="15000">15 seconds</option>
</select>
</label>
<div style="margin-top: 0.75rem;">
<button id="btn-measure" class="primary large">▶ Measure</button>
</div>
<div id="countdown" class="status-bar" style="display:none"></div>
<div id="result" style="display:none">
<div class="result-display">
<span id="result-duration">—</span>
<span class="unit">ms</span>
</div>
<div class="result-details" id="result-details"></div>
</div>
<div id="warm-warning" class="warning" style="display:none">
⚠️ localStorage was already accessed in this session.
Close and reopen the browser for a clean first-read measurement.
</div>
</div>
<!-- ============================================================ -->
<!-- Summary / copy -->
<!-- ============================================================ -->
<div id="summary">
<strong>Result</strong>
<table>
<tr><td>First read duration:</td><td id="stat-duration">—</td></tr>
<tr><td>Entries:</td><td id="stat-entries">—</td></tr>
<tr><td>Read type:</td><td id="stat-type">—</td></tr>
</table>
<button id="btn-copy" style="margin-top:0.75rem">Copy to Clipboard</button>
</div>
<script>
"use strict";
// ----------------------------------------------------------------
// State
// ----------------------------------------------------------------
let measureDone = false;
let localStorageTouched = false;
let resultDuration = null;
let entryCount = 0;
// ----------------------------------------------------------------
// DOM refs
// ----------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const elNumEntries = $("num-entries");
const elValueSize = $("value-size");
const elDataSizeNote = $("data-size-note");
const btnPopulate = $("btn-populate");
const btnClear = $("btn-clear");
const elPopStatus = $("populate-status");
const elReadDelay = $("read-delay");
const btnMeasure = $("btn-measure");
const elCountdown = $("countdown");
const elResult = $("result");
const elResultDuration = $("result-duration");
const elResultDetails = $("result-details");
const elWarmWarning = $("warm-warning");
const elSummary = $("summary");
const elStatDuration = $("stat-duration");
const elStatEntries = $("stat-entries");
const elStatType = $("stat-type");
const btnCopy = $("btn-copy");
// ----------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------
const CHAR_LIMIT = 5 * 1024 * 1024;
function setStatus(el, text, type) {
el.textContent = text;
el.className = type ? `status-bar ${type}` : "status-bar";
}
// Seeded PRNG (mulberry32). Same seed always produces the same
// sequence, so both backends get identical data.
function mulberry32(seed) {
return function() {
seed |= 0; seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
function seededValue(index, length) {
const rng = mulberry32(index * 2654435761);
let result = "";
for (let j = 0; j < length; j++) {
result += CHARSET.charAt(Math.floor(rng() * CHARSET.length));
}
return result;
}
// ----------------------------------------------------------------
// Data-size estimate
// localStorage quota is ~5M characters per origin (stored as
// UTF-16 internally, so ~10 MB on disk).
// ----------------------------------------------------------------
function estimateDataSize() {
const n = parseInt(elNumEntries.value, 10);
const v = parseInt(elValueSize.value, 10);
const charsPerEntry = `k_${n - 1}`.length + v;
const totalChars = n * charsPerEntry;
const totalKB = (totalChars / 1024).toFixed(1);
let note = `Estimated storage: ~${totalKB} K characters`;
if (totalChars > CHAR_LIMIT * 0.9) {
note += " \u26a0\ufe0f Approaching ~5M character per-origin limit!";
}
elDataSizeNote.textContent = note;
}
elNumEntries.addEventListener("change", estimateDataSize);
elValueSize.addEventListener("change", estimateDataSize);
estimateDataSize();
// ----------------------------------------------------------------
// Populate
// ----------------------------------------------------------------
btnPopulate.addEventListener("click", () => {
btnPopulate.disabled = true;
btnClear.disabled = true;
setStatus(elPopStatus, "Populating\u2026");
const numEntries = parseInt(elNumEntries.value, 10);
const valueSize = parseInt(elValueSize.value, 10);
try {
localStorage.clear();
for (let i = 0; i < numEntries; i++) {
localStorage.setItem(`k_${i}`, seededValue(i, valueSize));
}
localStorageTouched = true;
setStatus(elPopStatus,
`\u2714 Populated ${numEntries} entries (${valueSize} chars each). ` +
`Close the browser and reopen this page to measure.`,
"success");
} catch (e) {
localStorageTouched = true;
setStatus(elPopStatus,
`\u26a0\ufe0f ${e.message} \u2014 stored ${localStorage.length} entries.`,
"error");
} finally {
btnPopulate.disabled = false;
btnClear.disabled = false;
}
});
// ----------------------------------------------------------------
// Clear
// ----------------------------------------------------------------
btnClear.addEventListener("click", () => {
localStorage.clear();
localStorageTouched = true;
setStatus(elPopStatus, "\u2714 localStorage cleared.", "success");
});
// ----------------------------------------------------------------
// Measure first localStorage access
// ----------------------------------------------------------------
btnMeasure.addEventListener("click", () => {
if (measureDone) return;
const delay = parseInt(elReadDelay.value, 10) || 0;
if (delay > 0) {
btnMeasure.disabled = true;
elCountdown.style.display = "block";
let remaining = delay;
elCountdown.textContent = `Reading in ${remaining / 1000}s\u2026`;
const tick = setInterval(() => {
remaining -= 1000;
if (remaining <= 0) {
clearInterval(tick);
elCountdown.style.display = "none";
doMeasure();
} else {
elCountdown.textContent = `Reading in ${remaining / 1000}s\u2026`;
}
}, 1000);
} else {
doMeasure();
}
});
function doMeasure() {
const t0 = performance.now();
const len = localStorage.length;
const t1 = performance.now();
measureDone = true;
resultDuration = t1 - t0;
entryCount = len;
showResult();
}
function showResult() {
const duration = resultDuration.toFixed(3);
elResultDuration.textContent = duration;
elResultDetails.textContent = `${entryCount} entries \u00b7 ${duration} ms`;
elResult.style.display = "block";
btnMeasure.disabled = true;
btnMeasure.textContent = "\u2714 Measured";
if (localStorageTouched) {
elWarmWarning.style.display = "block";
}
const readType = localStorageTouched ? "Warm (cached)" : "Cold (from disk)";
elStatDuration.textContent = `${duration} ms`;
elStatEntries.textContent = entryCount;
elStatType.textContent = readType;
elSummary.style.display = "block";
}
// ----------------------------------------------------------------
// Copy result
// ----------------------------------------------------------------
btnCopy.addEventListener("click", () => {
if (resultDuration === null) return;
const readType = localStorageTouched ? "warm" : "cold";
const text = [
"LocalStorage First Read Benchmark",
`Duration: ${resultDuration.toFixed(3)} ms`,
`Entries: ${entryCount}`,
`Read type: ${readType}`,
`Timestamp: ${new Date().toISOString()}`,
].join("\n");
navigator.clipboard.writeText(text).then(
() => setStatus(elPopStatus, "\u2714 Copied to clipboard!", "success"),
() => setStatus(elPopStatus, "Failed to copy to clipboard.", "error")
);
});
</script>
</body>
</html>