generated from streetsidesoftware/template-typescript-cli-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
205 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ words: | |
- lcov | ||
- ngram | ||
- pnpm | ||
- Positionals | ||
- vitest | ||
ignoreWords: [] | ||
import: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import asTable from 'as-table'; | ||
import chalk from 'chalk'; | ||
|
||
import type { PerfSuite } from './perfSuite.mjs'; | ||
import { getActiveSuites } from './perfSuite.mjs'; | ||
|
||
export interface RunOptions { | ||
repeat?: number | undefined; | ||
timeout?: number | undefined; | ||
} | ||
|
||
/** | ||
* | ||
* @param suiteNames | ||
* @param options | ||
*/ | ||
export async function runBenchmarkSuites(suiteToRun?: (string | PerfSuite)[], options?: RunOptions) { | ||
const suites = getActiveSuites(); | ||
|
||
let numSuitesRun = 0; | ||
let showRepeatMsg = false; | ||
|
||
for (let repeat = options?.repeat || 1; repeat > 0; repeat--) { | ||
if (showRepeatMsg) { | ||
console.log(chalk.yellow(`Repeating tests: ${repeat} more time${repeat > 1 ? 's' : ''}.`)); | ||
} | ||
numSuitesRun = await runTestSuites(suites, suiteToRun || suites, options || {}); | ||
if (!numSuitesRun) break; | ||
showRepeatMsg = true; | ||
} | ||
|
||
if (!numSuitesRun) { | ||
console.log(chalk.red('No suites to run.')); | ||
console.log(chalk.yellow('Available suites:')); | ||
const width = process.stdout.columns || 80; | ||
const table = asTable.configure({ maxTotalWidth: width - 2 })( | ||
suites.map((suite) => ({ Suite: suite.name, Description: suite.description })), | ||
); | ||
console.log( | ||
table | ||
.split('\n') | ||
.map((line) => ` ${line}`) | ||
.join('\n'), | ||
); | ||
} | ||
} | ||
|
||
async function runTestSuites( | ||
suites: PerfSuite[], | ||
suitesToRun: (string | PerfSuite)[], | ||
options: RunOptions, | ||
): Promise<number> { | ||
const timeout = options.timeout || 1000; | ||
const suitesRun = new Set<PerfSuite>(); | ||
|
||
async function _runSuite(suites: PerfSuite[]) { | ||
for (const suite of suites) { | ||
if (suitesRun.has(suite)) continue; | ||
suitesRun.add(suite); | ||
console.log(chalk.green(`Running Perf Suite: ${suite.name}`)); | ||
await suite.setTimeout(timeout).runTests(); | ||
} | ||
} | ||
|
||
async function runSuite(name: string | PerfSuite) { | ||
if (typeof name !== 'string') { | ||
return await _runSuite([name]); | ||
} | ||
|
||
if (name === 'all') { | ||
await _runSuite(suites); | ||
return; | ||
} | ||
const matching = suites.filter((suite) => suite.name.toLowerCase().startsWith(name.toLowerCase())); | ||
if (!matching.length) { | ||
console.log(chalk.red(`Unknown test method: ${name}`)); | ||
return; | ||
} | ||
await _runSuite(matching); | ||
} | ||
|
||
for (const name of suitesToRun) { | ||
await runSuite(name); | ||
} | ||
|
||
return suitesRun.size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* This cli is designed to run the benchmarking suites found in the files on the command line. | ||
*/ | ||
|
||
import { pathToFileURL } from 'node:url'; | ||
import type { ParseArgsConfig } from 'node:util'; | ||
import { parseArgs } from 'node:util'; | ||
|
||
import { runBenchmarkSuites } from './run.mjs'; | ||
|
||
const cwdUrl = pathToFileURL(process.cwd() + '/'); | ||
|
||
async function run(args: string[]) { | ||
const parseConfig: ParseArgsConfig = { | ||
args, | ||
strict: true, | ||
allowPositionals: true, | ||
options: { | ||
repeat: { type: 'string', short: 'r' }, | ||
timeout: { type: 'string', short: 't' }, | ||
}, | ||
}; | ||
|
||
const parsed = parseArgs(parseConfig); | ||
|
||
const repeat = Number(parsed.values['repeat'] || '0') || undefined; | ||
const timeout = Number(parsed.values['timeout'] || '0') || undefined; | ||
|
||
const errors: Error[] = []; | ||
|
||
async function importFile(file: string) { | ||
const url = new URL(file, cwdUrl).toString(); | ||
try { | ||
await import(url); | ||
} catch (_) { | ||
errors.push(new Error(`Failed to import file: ${file}`)); | ||
} | ||
} | ||
|
||
// Import the files specified on the command line | ||
await Promise.all(parsed.positionals.map(async (file) => importFile(file))); | ||
|
||
if (errors.length) { | ||
console.error('Errors:'); | ||
errors.forEach((err) => console.error(err.message)); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
|
||
await runBenchmarkSuites(undefined, { repeat, timeout }); | ||
} | ||
|
||
run(process.argv.slice(2)); |