diff --git a/README.md b/README.md index 504abb3..c76f72b 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,24 @@ An element to help performance test other elements. The perf-tester element accepts an array of `tests` which are html files containing perf tests to run. A simple "runner.html" as is shown in `/demo` can be created to display output. -Each test file should load `perf.js` and call `console.perf()` to start the test and `console.perfEnd()` to finish it. \ No newline at end of file +Each test file should load `perf.js` and call `console.perf()` to start the test and `console.perfEnd()` to finish it. + +### Node script + +Run the node-script with + + node node-perf-tester.js + +The available options are: + + --runs Number of runs to measure on + --targets The file location of a JSON-structured file with the following format: + + [ + ["", ""] + ] + + where type is the name of the type you are testing and the url the location of the target under test + + --port The port number to connect the protocol to. Use this when you are connecting to a separate device. If this argument is not provided, chrome-headless is used. + --throttling one of [FAST_3G, SLOW_3G] If not running in Chrome headless, you can enable throttling with one of the two options. diff --git a/node-perf-tester.js b/node-perf-tester.js index 03ce432..f67d69e 100644 --- a/node-perf-tester.js +++ b/node-perf-tester.js @@ -7,87 +7,93 @@ const argv = require('minimist')(process.argv.slice(2)); const NUMBER_OF_RUNS = argv.runs || 100; (async function() { - let port = argv.port; - let chrome; - - if (!argv.port) { - const chromeLauncher = require('chrome-launcher'); - chrome = await chromeLauncher.launch({ - chromeFlags: ['--headless', '--disable-gpu', '--remote-debugging-address=0.0.0.0'], - port: 0 - }); - port = chrome.port; + let port = argv.port; + let chrome; + + if (!argv.port) { + const chromeLauncher = require('chrome-launcher'); + chrome = await chromeLauncher.launch({ + chromeFlags: + ['--headless', '--disable-gpu', '--remote-debugging-address=0.0.0.0'], + port: 0 + }); + port = chrome.port; + } + + const tab = await CDP.New({port}); + const client = await CDP({tab, port}); + + const {Page, Network, Runtime} = client; + + const ONE_MB = 1024 * 1024 / 8; + const throttling = { + FAST_3G: { + downloadThroughput: 1.6 * ONE_MB * .9, + uploadThroughput: .75 * ONE_MB * .9 + }, + SLOW_3G: { + downloadThroughput: .5 * ONE_MB * .8, + uploadThroughput: .5 * ONE_MB * .8 } - - const tab = await CDP.New({port}); - const client = await CDP({tab, port}); - - const {Page, Network, Runtime} = client; - - const ONE_MB = 1024 * 1024 / 8; - const throttling = { - FAST_3G: { - downloadThroughput: 1.6 * ONE_MB * .9, - uploadThroughput: .75 * ONE_MB * .9 - }, - SLOW_3G: { - downloadThroughput: .5 * ONE_MB * .8, - uploadThroughput: .5 * ONE_MB * .8 - } + } + + await Promise.all([ + Page.enable(), + Network.enable(), + port && argv.throttling && + Network.emulateNetworkConditions(Object.assign( + {}, throttling[argv.throttling], + {offline: false, latency: 10})), + Network.clearBrowserCache(), + Network.setCacheDisabled({cacheDisabled: true}), + Network.setBypassServiceWorker({bypass: true}), + ]); + + let loadEventPromise; + + Page.loadEventFired(() => { + loadEventPromise(); + }); + + const options = require(path.join(process.cwd(), argv.targets)); + + const perfTimings = {}; + for (const [type] of options) { + perfTimings[type] = []; + } + + process.on('exit', async () => { + for (const [type, timings] of Object.entries(perfTimings)) { + const average = timings.reduce((a, b) => a + b) / timings.length; + console.log( + `Average gain for ${type} in ${timings.length} runs is ${average}`); } - await Promise.all([ - Page.enable(), - Network.enable(), - port && argv.throttling && Network.emulateNetworkConditions( - Object.assign({}, throttling[argv.throttling], { - offline: false, - latency: 10 - }) - ), - Network.clearBrowserCache(), - Network.setCacheDisabled({cacheDisabled: true}), - Network.setBypassServiceWorker({bypass: true}), - ]); - - let loadEventPromise; - - Page.loadEventFired(() => { - loadEventPromise(); - }); - - const options = require(path.join(process.cwd(), argv.targets)); - - const perfTimings = {}; - for (const [type] of options) { - perfTimings[type] = []; + await CDP.Close({port, id: tab.id}); + await client.close(); + if (!argv.port) { + await chrome.kill(); } - - process.on('exit', async() => { - for (const [type, timings] of Object.entries(perfTimings)) { - const average = timings.reduce((a, b) => a + b) / timings.length; - console.log(`Average gain for ${type} in ${timings.length} runs is ${average}`); - } - - await CDP.Close({port, id: tab.id}); - await client.close(); - if (!argv.port) { - await chrome.kill(); - } - }); - - for (let i = 0; i < NUMBER_OF_RUNS; i++) { - for (const [type, url] of options) { - requestType = type; - - Page.navigate({url}); - - await new Promise(resolve => { - loadEventPromise = resolve; - }); - const {result: {value: perfTiming}} = await Runtime.evaluate({expression: 'window.perfTiming'}); - perfTimings[type].push(perfTiming); - } - process.stdout.write(`${i + 1}/${NUMBER_OF_RUNS}\r`); + }); + + for (let i = 0; i < NUMBER_OF_RUNS; i++) { + for (const [type, url] of options) { + requestType = type; + + Page.navigate({url}); + + await new Promise(resolve => { + loadEventPromise = resolve; + }); + const {result: {value: perfTiming}} = + await Runtime.evaluate({expression: 'window.perfTiming'}); + // const {result: {value: perfTiming}} = await + // Runtime.evaluate({expression: 'window.performance.timing.loadEventEnd- + // window.performance.timing.navigationStart'}); + perfTimings[type].push(perfTiming); } + process.stdout.write(`${i + 1}/${NUMBER_OF_RUNS}\r`); + } + + process.exit(0); })() \ No newline at end of file diff --git a/options.json b/options.json index 2c1f787..0637a08 100644 --- a/options.json +++ b/options.json @@ -1,4 +1 @@ -[ - ["original", "http://localhost:8081/youtube/"], - ["effects", "http://localhost:8081/youtube/index-effects.html"] -] \ No newline at end of file +[] \ No newline at end of file