From f80f72c7cca1d1060b37f5c68470f09d79823e4a Mon Sep 17 00:00:00 2001 From: soulgalore Date: Tue, 28 Nov 2023 11:32:53 +0100 Subject: [PATCH] Autogenerate ids if we miss an id --- lib/cli/cli.js | 2 +- lib/plugins/compare/index.js | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/cli/cli.js b/lib/cli/cli.js index c4d2444e3f..681e0a7388 100644 --- a/lib/cli/cli.js +++ b/lib/cli/cli.js @@ -1874,7 +1874,7 @@ export async function parseCommandLine() { .option('compare.id', { type: 'string', describe: - 'The id of the test. Will be used to find the baseline test, that is using the id as a part of the name.', + 'The id of the test. Will be used to find the baseline test, that is using the id as a part of the name. If you do not add an id, an id will be generated using the URL and that will only work if you baseline against the exact same URL.', group: 'compare' }) .option('compare.baselinePath', { diff --git a/lib/plugins/compare/index.js b/lib/plugins/compare/index.js index b0958b07c8..b18e62be57 100644 --- a/lib/plugins/compare/index.js +++ b/lib/plugins/compare/index.js @@ -7,7 +7,6 @@ import intel from 'intel'; import merge from 'lodash.merge'; import dayjs from 'dayjs'; -import { throwIfMissing } from '../../support/util.js'; import { getStatistics, runStatisticalTests, getMetrics } from './helper.js'; import { getBaseline, saveBaseline } from './baseline.js'; @@ -16,6 +15,14 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url)); const log = intel.getLogger('sitespeedio.plugin.compare'); const defaultConfig = {}; +function urlToId(url) { + return url + .replace(/^https?:\/\//, '') + .replaceAll(/[^\d.A-Za-z]/g, '_') + .replaceAll(/__+/g, '_') + .replaceAll(/^_|_$/g, ''); +} + const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'; const DEFAULT_METRICS_PAGESUMMARY = [ @@ -40,7 +47,6 @@ export default class ComparePlugin extends SitespeedioPlugin { this.make = context.messageMaker('compare').make; this.compareOptions = merge({}, defaultConfig, options.compare); this.options = options; - throwIfMissing(options.compare, ['id'], 'compare'); this.pug = readFileSync(resolve(__dirname, 'pug', 'index.pug'), 'utf8'); log.info( 'Starting the compare plugin.' + @@ -73,10 +79,16 @@ export default class ComparePlugin extends SitespeedioPlugin { } case 'browsertime.pageSummary': { this.page++; + const id = this.options.compare.id || urlToId(message.data.info.url); const baseline = await getBaseline( - this.options.compare.id + '-' + this.page, + id + '-' + this.page, this.compareOptions ); + if (this.options.compare.id) { + log.info('Using id %s for page baseline', id); + } else { + log.info('Using auto generated id for the baseline: %s ', id); + } if (baseline) { if ( @@ -88,9 +100,7 @@ export default class ComparePlugin extends SitespeedioPlugin { baseline.timestamps.length, this.options.browsertime.iterations ); - log.info( - 'Got a baseline:' + this.options.compare.id + '-' + this.page - ); + log.info('Got a baseline:' + id + '-' + this.page); const newMetrics = getMetrics(message.data); const baselineMetrics = getMetrics(baseline); const metricsInputData = { @@ -196,7 +206,7 @@ export default class ComparePlugin extends SitespeedioPlugin { message.data, join( this.compareOptions.baselinePath || process.cwd(), - `${this.options.compare.id}-${this.page}.json` + `${id}-${this.page}.json` ) ); } @@ -216,7 +226,7 @@ export default class ComparePlugin extends SitespeedioPlugin { message.data, join( this.compareOptions.baselinePath || process.cwd(), - `${this.options.compare.id}-${this.page}.json` + `${id}-${this.page}.json` ) ); }