@ -2,6 +2,8 @@
const path = require ( 'path' ) ;
const path = require ( 'path' ) ;
const { promises : fs , constants : fsConstants } = require ( 'fs' ) ;
const { promises : fs , constants : fsConstants } = require ( 'fs' ) ;
const yargs = require ( 'yargs/yargs' ) ;
const { hideBin } = require ( 'yargs/helpers' ) ;
const ttest = require ( 'ttest' ) ;
const ttest = require ( 'ttest' ) ;
const { withFixtures } = require ( './helpers' ) ;
const { withFixtures } = require ( './helpers' ) ;
const { PAGES } = require ( './webdriver/driver' ) ;
const { PAGES } = require ( './webdriver/driver' ) ;
@ -129,60 +131,52 @@ async function getFirstParentDirectoryThatExists(directory) {
}
}
async function main ( ) {
async function main ( ) {
const args = process . argv . slice ( 2 ) ;
const { argv } = yargs ( hideBin ( process . argv ) ) . usage (
'$0 [options]' ,
'Run a page load benchmark' ,
( _yargs ) =>
_yargs
. option ( 'pages' , {
array : true ,
default : [ 'home' ] ,
description :
'Set the page(s) to be benchmarked. This flag can accept multiple values (space-separated).' ,
choices : ALL _PAGES ,
} )
. option ( 'samples' , {
default : DEFAULT _NUM _SAMPLES ,
description : 'The number of times the benchmark should be run.' ,
type : 'number' ,
} )
. option ( 'out' , {
description :
'Output filename. Output printed to STDOUT of this is omitted.' ,
type : 'string' ,
normalize : true ,
} ) ,
) ;
const { pages , samples , out } = argv ;
let pages = [ 'home' ] ;
let numSamples = DEFAULT _NUM _SAMPLES ;
let outputPath ;
let outputDirectory ;
let outputDirectory ;
let existingParentDirectory ;
let existingParentDirectory ;
if ( out ) {
while ( args . length ) {
outputDirectory = path . dirname ( out ) ;
if ( /^(--pages|-p)$/u . test ( args [ 0 ] ) ) {
existingParentDirectory = await getFirstParentDirectoryThatExists (
if ( args [ 1 ] === undefined ) {
outputDirectory ,
throw new Error ( 'Missing pages argument' ) ;
) ;
}
if ( ! ( await isWritable ( existingParentDirectory ) ) ) {
pages = args [ 1 ] . split ( ',' ) ;
throw new Error ( 'Specified output file directory is not writable' ) ;
for ( const page of pages ) {
if ( ! ALL _PAGES . includes ( page ) ) {
throw new Error ( ` Invalid page: ' ${ page } ` ) ;
}
}
args . splice ( 0 , 2 ) ;
} else if ( /^(--samples|-s)$/u . test ( args [ 0 ] ) ) {
if ( args [ 1 ] === undefined ) {
throw new Error ( 'Missing number of samples' ) ;
}
numSamples = parseInt ( args [ 1 ] , 10 ) ;
if ( isNaN ( numSamples ) ) {
throw new Error ( ` Invalid 'samples' argument given: ' ${ args [ 1 ] } ' ` ) ;
}
args . splice ( 0 , 2 ) ;
} else if ( /^(--out|-o)$/u . test ( args [ 0 ] ) ) {
if ( args [ 1 ] === undefined ) {
throw new Error ( 'Missing output filename' ) ;
}
outputPath = path . resolve ( args [ 1 ] ) ;
outputDirectory = path . dirname ( outputPath ) ;
existingParentDirectory = await getFirstParentDirectoryThatExists (
outputDirectory ,
) ;
if ( ! ( await isWritable ( existingParentDirectory ) ) ) {
throw new Error ( ` Specified directory is not writable: ' ${ args [ 1 ] } ' ` ) ;
}
args . splice ( 0 , 2 ) ;
} else {
throw new Error ( ` Unrecognized argument: ' ${ args [ 0 ] } ' ` ) ;
}
}
}
}
const results = await profilePageLoad ( pages , numS amples) ;
const results = await profilePageLoad ( pages , samples ) ;
if ( outputPath ) {
if ( out ) {
if ( outputDirectory !== existingParentDirectory ) {
if ( outputDirectory !== existingParentDirectory ) {
await fs . mkdir ( outputDirectory , { recursive : true } ) ;
await fs . mkdir ( outputDirectory , { recursive : true } ) ;
}
}
await fs . writeFile ( outputPath , JSON . stringify ( results , null , 2 ) ) ;
await fs . writeFile ( out , JSON . stringify ( results , null , 2 ) ) ;
} else {
} else {
console . log ( JSON . stringify ( results , null , 2 ) ) ;
console . log ( JSON . stringify ( results , null , 2 ) ) ;
}
}