Skip to content

Commit

Permalink
A bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Apr 20, 2024
1 parent a71786e commit f0b64a1
Show file tree
Hide file tree
Showing 15 changed files with 792 additions and 638 deletions.
2 changes: 1 addition & 1 deletion bin.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

import { run } from './dist/tools/appNGram.mjs';
import { run } from './dist/app.mjs';

run();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"dependencies": {
"@cspell/cspell-pipe": "^8.7.0",
"as-table": "^1.0.55",
"chalk": "^5.3.0",
"commander": "^12.0.0",
"globby": "^14.0.1",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 56 additions & 41 deletions src/app.mts
Original file line number Diff line number Diff line change
@@ -1,70 +1,85 @@
import { promises as fs } from 'node:fs';
import './perf-suites/measureAnonymous.mjs';
import './perf-suites/measureMap.mjs';
import './perf-suites/measureSearch.mjs';

import { fileURLToPath } from 'node:url';

import asTable from 'as-table';
import chalk from 'chalk';
import { Argument, Command, program as defaultCommand } from 'commander';
import * as path from 'path';

import { measureAnonymous } from './measureAnonymous.mjs';
import { measureMap } from './measureMap.mjs';
import { measureSearch } from './measureSearch.mjs';
import { getActiveSuites, PerfSuite } from './perfSuite.mjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function version(): Promise<string> {
const pathPackageJson = path.join(__dirname, '../package.json');
const packageJson = JSON.parse(await fs.readFile(pathPackageJson, 'utf8'));
return (typeof packageJson === 'object' && packageJson?.version) || '0.0.0';
}

const knownTests = {
search: measureSearch,
anonymous: measureAnonymous,
map: measureMap,
};

const allTests = {
search: measureSearch,
anonymous: measureAnonymous,
map: measureMap,
all: async (timeout?: number) => {
for (const test of Object.values(knownTests)) {
await test(timeout);
}
},
};

interface AppOptions {
timeout?: number;
all?: boolean;
}

export async function app(program = defaultCommand): Promise<Command> {
const argument = new Argument('[test-methods...]', 'list of test methods to run');
argument.choices(Object.keys(allTests));
argument.default(['all']);
const suites = getActiveSuites();
const setOfSuiteNames = new Set(suites.map((suite) => suite.name));
const suitesNames = [...setOfSuiteNames, 'all'];

const argument = new Argument('[test-suite...]', 'list of test suites to run');
argument.choices(suitesNames);
argument.variadic = true;

program
.name('perf runner')
.addArgument(argument)
.description('Run performance tests.')
.option('-a, --all', 'run all tests', false)
.option('-t, --timeout <timeout>', 'timeout for each test', (v) => Number(v), 1000)
.version(await version())
.action(async (methods: string[], options: AppOptions) => {
.action(async (suiteNamesToRun: string[], options: AppOptions) => {
// console.log('Options: %o', optionsCli);
const timeout = options.timeout || 1000;
const tests = Object.entries(allTests);
for (const method of methods) {
const test = tests.find(([key]) => key === method);
if (!test) {
console.log(chalk.red(`Unknown test method: ${method}`));
continue;
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) {
if (name === 'all') {
await _runSuite(suites);
return;
}
const [key, fn] = test;
console.log(chalk.green(`Running test: ${key}`));
await fn(timeout);
const matching = suites.filter((suite) => suite.name === name);
if (!matching.length) {
console.log(chalk.red(`Unknown test method: ${name}`));
return;
}
await _runSuite(matching);
}

for (const name of suiteNamesToRun) {
await runSuite(name);
}

if (!suitesRun.size) {
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'),
);
}

console.log(chalk.green('done.'));
});

Expand Down
File renamed without changes.
File renamed without changes.
32 changes: 0 additions & 32 deletions src/measureAnonymous.mts

This file was deleted.

110 changes: 0 additions & 110 deletions src/measureMap.mts

This file was deleted.

Loading

0 comments on commit f0b64a1

Please sign in to comment.