browsertime_benchmark.js (4431B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* eslint-env node */ 6 7 const { logTest, logTask } = require("./utils/profiling"); 8 const { 9 initializeMeasurements, 10 startMeasurements, 11 stopMeasurements, 12 finalizeMeasurements, 13 } = require("./utils/support_measurements"); 14 15 module.exports = logTest( 16 "browsertime benchmark", 17 async function (context, commands) { 18 context.log.info("Starting a browsertime benchamrk"); 19 let url = context.options.browsertime.url; 20 let page_cycles = context.options.browsertime.page_cycles; 21 let page_cycle_delay = context.options.browsertime.page_cycle_delay; 22 let post_startup_delay = context.options.browsertime.post_startup_delay; 23 let page_timeout = context.options.timeouts.pageLoad; 24 let ret = false; 25 let expose_profiler = context.options.browsertime.expose_profiler; 26 27 await initializeMeasurements( 28 context, 29 commands, 30 context.options.browsertime.cpuTime_test, 31 context.options.browsertime.power_test, 32 context.options.browsertime.wallclock_tracking_test 33 ); 34 35 context.log.info( 36 "Waiting for %d ms (post_startup_delay)", 37 post_startup_delay 38 ); 39 await commands.wait.byTime(post_startup_delay); 40 41 for (let count = 0; count < page_cycles; count++) { 42 await logTask(context, "cycle " + count, async function () { 43 context.log.info("Navigating to about:blank"); 44 await commands.navigate("about:blank"); 45 46 context.log.info( 47 "Cycle %d, waiting for %d ms", 48 count, 49 page_cycle_delay 50 ); 51 await commands.wait.byTime(page_cycle_delay); 52 53 context.log.info("Cycle %d, starting the measure", count); 54 if (expose_profiler === "true") { 55 context.log.info("Custom profiler start!"); 56 if (context.options.browser === "firefox") { 57 await commands.profiler.start(); 58 } else if (context.options.browser === "chrome") { 59 await commands.trace.start(); 60 } 61 } 62 63 await commands.measure.start(url); 64 await startMeasurements(context, commands); 65 66 context.log.info("Benchmark custom metric collection"); 67 68 let data = null; 69 let starttime = await commands.js.run(`return performance.now();`); 70 while ( 71 data == null && 72 (await commands.js.run(`return performance.now();`)) - starttime < 73 page_timeout 74 ) { 75 let wait_time = 76 context.options.browsertime.benchmark_wait_time || 3000; 77 context.log.info( 78 "Waiting %d ms for data from benchmark...", 79 wait_time 80 ); 81 await commands.wait.byTime(wait_time); 82 data = await commands.js.run( 83 "return window.sessionStorage.getItem('benchmark_results');" 84 ); 85 } 86 87 if (expose_profiler === "true") { 88 context.log.info("Custom profiler stop!"); 89 if (context.options.browser === "firefox") { 90 await commands.profiler.stop(); 91 } else if (context.options.browser === "chrome") { 92 await commands.trace.stop(); 93 } 94 } 95 if ( 96 data == null && 97 (await commands.js.run(`return performance.now();`)) - starttime >= 98 page_timeout 99 ) { 100 ret = false; 101 context.log.error("Benchmark timed out. Aborting..."); 102 } else if (data) { 103 await stopMeasurements(); 104 105 // Reset benchmark results 106 await commands.js.run( 107 "return window.sessionStorage.removeItem('benchmark_results');" 108 ); 109 110 context.log.info("Value of benchmark data: ", data); 111 data = JSON.parse(data); 112 113 if (!Array.isArray(data)) { 114 commands.measure.addObject({ browsertime_benchmark: data }); 115 } else { 116 commands.measure.addObject({ 117 browsertime_benchmark: { 118 [data[1]]: data.slice(2), 119 }, 120 }); 121 } 122 ret = true; 123 } else { 124 context.log.error("No data collected from benchmark."); 125 ret = false; 126 } 127 }); 128 } 129 await finalizeMeasurements(); 130 131 context.log.info("Browsertime benchmark ended."); 132 return ret; 133 } 134 );