redoc/perf/mount-time.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
const puppeteer = require('puppeteer');
const crypto = require('crypto');
async function run() {
return await puppeteer
.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })
.then(async browser => {
const page = await browser.newPage();
let resolve;
const prom = new Promise(_resolve => {
resolve = _resolve;
});
2017-11-19 23:11:40 +03:00
page.on('console', obj => {
if (obj && obj.timings) {
resolve(obj);
2017-10-12 00:01:37 +03:00
}
});
await page.goto('http://localhost:9090', {
waitUntil: 'networkidle',
});
const res = await prom;
2017-11-19 23:11:40 +03:00
await browser.close();
2017-10-12 00:01:37 +03:00
return res;
});
}
function clearLine() {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
2017-11-19 23:11:40 +03:00
const metrics = ['Total Time', 'Store Init Time', 'Render Time'];
const forEachMetric = fn => metrics.forEach(metric => fn(metric));
2017-10-12 00:01:37 +03:00
async function benchmark() {
const N = 5;
2017-11-19 23:11:40 +03:00
let sum = {};
let max = {};
let min = {};
forEachMetric(metric => {
sum[metric] = 0;
max[metric] = 0;
min[metric] = Number.MAX_SAFE_INTEGER;
});
2017-10-12 00:01:37 +03:00
for (let i = 0; i < N; i++) {
const res = await run();
2017-11-19 23:11:40 +03:00
forEachMetric(metric => {
if (res[metric] > max[metric]) max[metric] = res[metric];
if (res[metric] < min[metric]) min[metric] = res[metric];
sum[metric] += res[metric];
});
2017-10-12 00:01:37 +03:00
clearLine();
process.stdout.write(`Running: ${i + 1} of ${N}`);
}
clearLine();
2017-11-19 23:11:40 +03:00
const average = {};
forEachMetric(metric => {
average[metric] = sum[metric] / N;
});
2017-10-12 00:01:37 +03:00
console.log('Completed ', N, 'runs');
console.log('=======================');
2017-11-19 23:11:40 +03:00
forEachMetric(metric => {
console.log(`Average ${metric}: `, average[metric]);
console.log(`Minimum ${metric}: `, min[metric]);
console.log(`Maximum ${metric}: `, max[metric]);
console.log();
});
2017-10-12 00:01:37 +03:00
}
benchmark();