Skip to content

Commit

Permalink
Autogenerate ids if we miss an id (#4013)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Nov 28, 2023
1 parent 56bf0da commit 0cbf6ff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
26 changes: 18 additions & 8 deletions lib/plugins/compare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 = [
Expand All @@ -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.' +
Expand Down Expand Up @@ -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 (
Expand All @@ -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 = {
Expand Down Expand Up @@ -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`
)
);
}
Expand All @@ -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`
)
);
}
Expand Down

0 comments on commit 0cbf6ff

Please sign in to comment.