test_caching.html (2338B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Service worker performance test: caching</title> 5 </head> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script src="../utils.js"></script> 8 <script src="perfutils.js"></script> 9 <script> 10 11 "use strict"; 12 13 const NO_CACHE = "No cache"; 14 const CACHED = "Cached"; 15 const NO_CACHE_AGAIN = "No cache again"; 16 17 var journal = {}; 18 journal[NO_CACHE] = []; 19 journal[CACHED] = []; 20 journal[NO_CACHE_AGAIN] = []; 21 22 const ITERATIONS = 10; 23 24 var perfMetadata = { 25 owner: "DOM LWS", 26 name: "Service Worker Caching", 27 description: "Test service worker caching.", 28 options: { 29 default: { 30 perfherder: true, 31 perfherder_metrics: [ 32 // Here, we can't use the constants defined above because perfherder 33 // grabs data from the parse tree. 34 { name: "No cache", unit: "ms", shouldAlert: true }, 35 { name: "Cached", unit: "ms", shouldAlert: true }, 36 { name: "No cache again", unit: "ms", shouldAlert: true }, 37 ], 38 verbose: true, 39 manifest: "perftest.toml", 40 manifest_flavor: "plain", 41 }, 42 }, 43 }; 44 45 add_task(async () => { 46 await SpecialPowers.pushPrefEnv({ 47 set: [["dom.serviceWorkers.testing.enabled", true]] 48 }); 49 }); 50 51 function create_iframe(url) { 52 return new Promise(function(res) { 53 let iframe = document.createElement("iframe"); 54 iframe.src = url; 55 iframe.onload = function() { res(iframe) } 56 document.body.appendChild(iframe); 57 }); 58 } 59 60 async function time_fetch(journal, iframe, filename) { 61 for (let i = 0; i < ITERATIONS; i++) { 62 let result = await iframe.contentWindow.time_fetch(filename); 63 is(result.status, 200); 64 is(result.data, filename); 65 journal.push(result.elapsed_ms); 66 } 67 } 68 69 add_task(async () => { 70 let reg = await navigator.serviceWorker.register("sw_cacher.js"); 71 await waitForState(reg.installing, "activated"); 72 73 let iframe = await create_iframe("time_fetch.html"); 74 75 await time_fetch(journal[NO_CACHE], iframe, "uncached.txt"); 76 await time_fetch(journal[CACHED], iframe, "cached.txt"); 77 await time_fetch(journal[NO_CACHE_AGAIN], iframe, "uncached.txt"); 78 79 await reg.unregister(); 80 }); 81 82 add_task(() => { 83 reportMetrics(journal); 84 }); 85 86 </script> 87 <body> 88 </body> 89 </html>