misc(compare-runs): fix filter, allow for resume, reorganize output

This commit is contained in:
Connor Clark 2020-03-30 12:19:27 -07:00 коммит произвёл GitHub
Родитель 8443176392
Коммит 20e4bc7d8b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 41 добавлений и 19 удалений

Просмотреть файл

@ -7,7 +7,7 @@
// Example:
// node lighthouse-core/scripts/compare-runs.js --name my-collection --collect -n 3 --lh-flags='--only-audits=unminified-javascript' --urls https://www.example.com https://www.nyt.com
// node lighthouse-core/scripts/compare-runs.js --name my-collection --summarize --measure-filter 'loadPage|connect'
// node lighthouse-core/scripts/compare-runs.js --name my-collection --summarize --filter 'loadPage|connect'
// node lighthouse-core/scripts/compare-runs.js --name base --name pr --compare
// The script will report both timings and perf metric results. View just one of them but using --filter:
@ -59,7 +59,7 @@ const argv = yargs
.default('lh-flags', '')
.strict() // fail on unknown commands
.wrap(yargs.terminalWidth())
.argv;
.argv;
const reportExcludeRegex =
argv.reportExclude !== 'none' ? new RegExp(argv.reportExclude, 'i') : null;
@ -117,24 +117,34 @@ function round(value) {
* @return {string}
*/
function getProgressBar(i, total = argv.n * argv.urls.length) {
return new Array(Math.round(i * 40 / total)).fill('▄').join('').padEnd(40);
const bars = new Array(Math.round(i * 40 / total)).fill('▄').join('').padEnd(40);
return `${i + 1} / ${total} [${bars}]`;
}
async function gather() {
const outputDir = dir(argv.name);
await mkdir(ROOT_OUTPUT_DIR, {recursive: true});
// Don't overwrite a previous collection
if (fs.existsSync(outputDir)) throw new Error(`folder already exists: ${outputDir}`);
await mkdir(outputDir);
if (fs.existsSync(outputDir)) {
// eslint-disable-next-line no-console
console.log('Collection already started - resuming.');
}
await mkdir(outputDir, {recursive: true});
const progress = new ProgressLogger();
progress.log('Gathering…');
let progressCount = 0;
for (const url of argv.urls) {
const urlFolder = `${outputDir}/${urlToFolder(url)}`;
await mkdir(urlFolder, {recursive: true});
for (let i = 0; i < argv.n; i++) {
const gatherDir = `${outputDir}/${urlToFolder(url)}/${i}/`;
await mkdir(gatherDir, {recursive: true});
progress.progress(getProgressBar(i));
const gatherDir = `${urlFolder}/${i}`;
progress.progress(getProgressBar(progressCount));
progressCount++;
// Skip if already gathered. Allows for restarting collection.
if (fs.existsSync(gatherDir)) continue;
const cmd = [
'node',
@ -154,17 +164,25 @@ async function audit() {
const progress = new ProgressLogger();
progress.log('Auditing…');
let progressCount = 0;
for (const url of argv.urls) {
const urlDir = `${outputDir}/${urlToFolder(url)}`;
for (let i = 0; i < argv.n; i++) {
const gatherDir = `${outputDir}/${urlToFolder(url)}/${i}/`;
progress.progress(getProgressBar(i));
const gatherDir = `${urlDir}/${i}`;
const outputPath = `${urlDir}/lhr-${i}.json`;
progress.progress(getProgressBar(progressCount));
progressCount++;
// Skip if already audited. Allows for restarting collection.
if (fs.existsSync(outputPath)) continue;
const cmd = [
'node',
`${LH_ROOT}/lighthouse-cli`,
url,
`--audit-mode=${gatherDir}`,
`--output-path=${outputDir}/lhr-${urlToFolder(url)}-${i}.json`,
`--output-path=${outputPath}`,
'--output=json',
argv.lhFlags,
].join(' ');
@ -184,7 +202,7 @@ function aggregateResults(name) {
/** @type {Map<string, number[]>} */
const durationsMap = new Map();
for (const lhrPath of glob.sync(`${outputDir}/*.json`)) {
for (const lhrPath of glob.sync(`${outputDir}/**/lhr-*.json`)) {
const lhrJson = fs.readFileSync(lhrPath, 'utf-8');
/** @type {LH.Result} */
const lhr = JSON.parse(lhrJson);
@ -247,19 +265,22 @@ function aggregateResults(name) {
}
/**
* @param {*[]} results
* @param {Array<{key: string, name: string}>} results
*/
function filter(results) {
const includeFilter = argv.filter ? new RegExp(argv.filter, 'i') : null;
results.forEach((result, i) => {
for (const propName of Object.keys(result)) {
if (reportExcludeRegex && reportExcludeRegex.test(propName)) delete result[propName];
}
if (includeFilter && !includeFilter.test(result.key)) {
delete results[i];
}
for (const propName of Object.keys(result)) {
if (reportExcludeRegex && reportExcludeRegex.test(propName)) {
// @ts-ignore: propName is a key.
delete result[propName];
}
}
});
}
@ -314,6 +335,7 @@ function compare() {
const max = compareValues(baseResult && baseResult.max, otherResult && otherResult.max);
return {
'key': someResult.key,
'name': someResult.name,
'url': someResult.url,
'mean': mean.description,