Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
Address reviewer feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdLippe committed Sep 5, 2017
1 parent 34d401c commit 4560df9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 83 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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:

[
["<type>", "<url>"]
]

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.
162 changes: 84 additions & 78 deletions node-perf-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})()
5 changes: 1 addition & 4 deletions options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
[
["original", "http://localhost:8081/youtube/"],
["effects", "http://localhost:8081/youtube/index-effects.html"]
]
[]

0 comments on commit 4560df9

Please sign in to comment.