From b236c3d2f357a16a733c96ec2ca8c57848b70091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vinicius=20Louren=C3=A7o?= <12551007+H4ad@users.noreply.github.com> Date: Sun, 7 Apr 2024 19:03:04 -0300 Subject: [PATCH] chore: add benchmarks (#696) I had these benchmarks from the last time I did some perf research in this repo, so I thought maybe it was a good idea to have them in the repo itself, so other people can use them and you guys can verify if the perf is still good or you had any regression. --- .gitignore | 1 + benchmarks/bench-compare.js | 48 +++++++++++++++++++++++++++++++ benchmarks/bench-diff.js | 21 ++++++++++++++ benchmarks/bench-parse-options.js | 33 +++++++++++++++++++++ benchmarks/bench-parse.js | 25 ++++++++++++++++ benchmarks/bench-satisfies.js | 39 +++++++++++++++++++++++++ benchmarks/bench-subset.js | 25 ++++++++++++++++ package.json | 4 ++- 8 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 benchmarks/bench-compare.js create mode 100644 benchmarks/bench-diff.js create mode 100644 benchmarks/bench-parse-options.js create mode 100644 benchmarks/bench-parse.js create mode 100644 benchmarks/bench-satisfies.js create mode 100644 benchmarks/bench-subset.js diff --git a/.gitignore b/.gitignore index 1bcc5e25..ff56062c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ tap-testdir*/ !/.gitignore !/.npmrc !/.release-please-manifest.json +!/benchmarks !/bin/ !/CHANGELOG* !/classes/ diff --git a/benchmarks/bench-compare.js b/benchmarks/bench-compare.js new file mode 100644 index 00000000..2dfc0900 --- /dev/null +++ b/benchmarks/bench-compare.js @@ -0,0 +1,48 @@ +const Benchmark = require('benchmark') +const SemVer = require('../classes/semver') +const suite = new Benchmark.Suite() + +const versions = ['1.0.3', '2.2.2', '2.3.0'] +const versionToCompare = '1.0.2' +const option1 = { includePrelease: true } +const option2 = { includePrelease: true, loose: true } +const option3 = { includePrelease: true, loose: true, rtl: true } + +for (const version of versions) { + suite.add(`compare ${version} to ${versionToCompare}`, function () { + const semver = new SemVer(version) + semver.compare(versionToCompare) + }) +} + +for (const version of versions) { + suite.add( + `compare ${version} to ${versionToCompare} with option (${JSON.stringify(option1)})`, + function () { + const semver = new SemVer(version, option1) + semver.compare(versionToCompare) + }) +} + +for (const version of versions) { + suite.add(`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option2)})`, + function () { + const semver = new SemVer(version, option2) + semver.compare(versionToCompare) + }) +} + +for (const version of versions) { + suite.add( + `compare ${version} to ${versionToCompare} with option (${JSON.stringify(option3)})`, + function () { + const semver = new SemVer(version, option3) + semver.compare(versionToCompare) + }) +} + +suite + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/benchmarks/bench-diff.js b/benchmarks/bench-diff.js new file mode 100644 index 00000000..97d74441 --- /dev/null +++ b/benchmarks/bench-diff.js @@ -0,0 +1,21 @@ +const Benchmark = require('benchmark') +const diff = require('../functions/diff') +const suite = new Benchmark.Suite() + +const cases = [ + ['0.0.1', '0.0.1-pre', 'patch'], + ['0.0.1', '0.0.1-pre-2', 'patch'], + ['1.1.0', '1.1.0-pre', 'minor'], +] + +for (const [v1, v2] of cases) { + suite.add(`diff(${v1}, ${v2})`, function () { + diff(v1, v2) + }) +} + +suite + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/benchmarks/bench-parse-options.js b/benchmarks/bench-parse-options.js new file mode 100644 index 00000000..41ab232c --- /dev/null +++ b/benchmarks/bench-parse-options.js @@ -0,0 +1,33 @@ +const Benchmark = require('benchmark') +const parseOptions = require('../internal/parse-options') +const suite = new Benchmark.Suite() + +const options1 = { + includePrerelease: true, +} + +const options2 = { + includePrerelease: true, + loose: true, +} + +const options3 = { + includePrerelease: true, + loose: true, + rtl: false, +} + +suite + .add('includePrerelease', function () { + parseOptions(options1) + }) + .add('includePrerelease + loose', function () { + parseOptions(options2) + }) + .add('includePrerelease + loose + rtl', function () { + parseOptions(options3) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/benchmarks/bench-parse.js b/benchmarks/bench-parse.js new file mode 100644 index 00000000..e4180c44 --- /dev/null +++ b/benchmarks/bench-parse.js @@ -0,0 +1,25 @@ +const Benchmark = require('benchmark') +const parse = require('../functions/parse') +const { MAX_SAFE_INTEGER } = require('../internal/constants') +const suite = new Benchmark.Suite() + +const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre'] +const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz'] + +for (const test of cases) { + suite.add(`parse(${test})`, function () { + parse(test) + }) +} + +for (const test of invalidCases) { + suite.add(`invalid parse(${test})`, function () { + parse(test) + }) +} + +suite + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/benchmarks/bench-satisfies.js b/benchmarks/bench-satisfies.js new file mode 100644 index 00000000..a95a2c30 --- /dev/null +++ b/benchmarks/bench-satisfies.js @@ -0,0 +1,39 @@ +const Benchmark = require('benchmark') +const satisfies = require('../functions/satisfies') +const suite = new Benchmark.Suite() + +const versions = ['1.0.3||^2.0.0', '2.2.2||~3.0.0', '2.3.0||<4.0.0'] +const versionToCompare = '1.0.6' +const option1 = { includePrelease: true } +const option2 = { includePrelease: true, loose: true } +const option3 = { includePrelease: true, loose: true, rtl: true } + +for (const version of versions) { + suite.add(`satisfies(${versionToCompare}, ${version})`, function () { + satisfies(versionToCompare, version) + }) +} + +for (const version of versions) { + suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option1)})`, function () { + satisfies(versionToCompare, version, option1) + }) +} + +for (const version of versions) { + suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option2)})`, function () { + satisfies(versionToCompare, version, option2) + }) +} + +for (const version of versions) { + suite.add(`satisfies(${versionToCompare}, ${version}, ${JSON.stringify(option3)})`, function () { + satisfies(versionToCompare, version, option3) + }) +} + +suite + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/benchmarks/bench-subset.js b/benchmarks/bench-subset.js new file mode 100644 index 00000000..f8825fae --- /dev/null +++ b/benchmarks/bench-subset.js @@ -0,0 +1,25 @@ +const Benchmark = require('benchmark') +const subset = require('../ranges/subset') +const suite = new Benchmark.Suite() + +// taken from tests +const cases = [ + // everything is a subset of * + ['1.2.3', '*', true], + ['^1.2.3', '*', true], + ['^1.2.3-pre.0', '*', false], + ['^1.2.3-pre.0', '*', true, { includePrerelease: true }], + ['1 || 2 || 3', '*', true], +] + +for (const [sub, dom] of cases) { + suite.add(`subset(${sub}, ${dom})`, function () { + subset(sub, dom) + }) +} + +suite + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ async: false }) diff --git a/package.json b/package.json index f00c6bdd..bec6c7df 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "devDependencies": { "@npmcli/eslint-config": "^4.0.0", "@npmcli/template-oss": "4.21.3", + "benchmark": "^2.1.4", "tap": "^16.0.0" }, "license": "ISC", @@ -71,7 +72,8 @@ "/ranges/", "/index.js", "/preload.js", - "/range.bnf" + "/range.bnf", + "/benchmarks" ], "publish": "true" }