Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(benchmark): Adds sanitiseVersion function #340

Open
wants to merge 2 commits into
base: issue-335
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions benchmark/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const runSuites = (suites) => {
return recurse(suites, [])
};

const printResults = (results, compareToVersion) => {
const printResults = (results, versionToCompare) => {
const parsedResults = results.map(result => ({
version: result.version,
name: result.name,
Expand Down Expand Up @@ -75,7 +75,7 @@ const printResults = (results, compareToVersion) => {
const resultsByFuncSummary = Object.keys(resultsByFunc).reduce(
(memo, name) => {
const sourceOpsXSec = parseInt(resultsByFunc[name].source, 10);
const compareToOpsXSec = parseInt(resultsByFunc[name][compareToVersion], 10);
const compareToOpsXSec = parseInt(resultsByFunc[name][versionToCompare], 10);
const diff = (
sourceOpsXSec > compareToOpsXSec
? `${(sourceOpsXSec / compareToOpsXSec).toFixed(2)}x up`
Expand Down Expand Up @@ -109,16 +109,32 @@ const fetchModule = async (version) => {
return mod.namespace;
};

const main = async (compareToVersion = 'latest') => {
const sanitiseVersion = (version = 'latest') => {
const semverRegExp = new RegExp([
'^v(0|[1-9]\\d*)',
'\\.(0|[1-9]\\d*)',
'\\.(0|[1-9]\\d*|x)',
'(?:[-]?[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$'
].join(''));

if (version !== 'latest' && !semverRegExp.test(version)) {
throw new Error(`Unknown version argument: ${version}`);
}

return version;
};

const main = async () => {
const versionToCompare = sanitiseVersion(process.argv[2]);
const results = await runSuites([
{ module: psl, version: 'source' },
{ module: await fetchModule(compareToVersion), version: compareToVersion },
{ module: await fetchModule(versionToCompare), version: versionToCompare },
]);

printResults(results, compareToVersion);
printResults(results, versionToCompare);
};

main(process.argv[2]).catch((error) => {
main().catch((error) => {
console.error(error);
process.exit(1);
});
});