diff --git a/benchmark/bench-cmp-branch.js b/benchmark/bench-cmp-branch.js deleted file mode 100644 index a0c5d9d..0000000 --- a/benchmark/bench-cmp-branch.js +++ /dev/null @@ -1,129 +0,0 @@ -const { spawn } = require("node:child_process"); - -const cliSelect = require("cli-select"); -const simpleGit = require("simple-git"); - -const git = simpleGit(process.cwd()); - -const COMMAND = "npm run benchmark"; -const DEFAULT_BRANCH = "main"; -const PERCENT_THRESHOLD = 5; -const greyColor = "\x1b[30m"; -const redColor = "\x1b[31m"; -const greenColor = "\x1b[32m"; -const resetColor = "\x1b[0m"; - -async function selectBranchName(message, branches) { - console.log(message); - const result = await cliSelect({ - type: "list", - name: "branch", - values: branches, - }); - console.log(result.value); - return result.value; -} - -async function executeCommandOnBranch(command, branch) { - console.log(`${greyColor}Checking out "${branch}"${resetColor}`); - await git.checkout(branch); - - console.log(`${greyColor}Execute "${command}"${resetColor}`); - const childProcess = spawn(command, { stdio: "pipe", shell: true }); - - let result = ""; - childProcess.stdout.on("data", (data) => { - process.stdout.write(data.toString()); - result += data.toString(); - }); - - await new Promise((resolve) => childProcess.on("close", resolve)); - - console.log(); - - return parseBenchmarksStdout(result); -} - -function parseBenchmarksStdout(text) { - const results = []; - - const lines = text.split("\n"); - for (const line of lines) { - const match = /^(.+?)(\.*) x (.+) ops\/sec .*$/.exec(line); - if (match !== null) { - results.push({ - name: match[1], - alignedName: match[1] + match[2], - result: Number.parseInt(match[3].split(",").join("")), - }); - } - } - - return results; -} - -function compareResults(featureBranch, mainBranch) { - for (const { name, alignedName, result: mainBranchResult } of mainBranch) { - const featureBranchBenchmark = featureBranch.find( - (result) => result.name === name, - ); - if (featureBranchBenchmark) { - const featureBranchResult = featureBranchBenchmark.result; - const percent = - ((featureBranchResult - mainBranchResult) * 100) / mainBranchResult; - const roundedPercent = Math.round(percent * 100) / 100; - - const percentString = - roundedPercent > 0 ? `+${roundedPercent}%` : `${roundedPercent}%`; - const message = alignedName + percentString.padStart(7, "."); - - if (roundedPercent > PERCENT_THRESHOLD) { - console.log(`${greenColor}${message}${resetColor}`); - } else if (roundedPercent < -PERCENT_THRESHOLD) { - console.log(`${redColor}${message}${resetColor}`); - } else { - console.log(message); - } - } - } -} - -(async () => { - const branches = await git.branch(); - const currentBranch = branches.branches[branches.current]; - - let featureBranch = null; - let mainBranch = null; - - if (process.argv[2] === "--ci") { - featureBranch = currentBranch.name; - mainBranch = DEFAULT_BRANCH; - } else { - featureBranch = await selectBranchName( - "Select the branch you want to compare (feature branch):", - branches.all, - ); - mainBranch = await selectBranchName( - "Select the branch you want to compare with (main branch):", - branches.all, - ); - } - - try { - const featureBranchResult = await executeCommandOnBranch( - COMMAND, - featureBranch, - ); - const mainBranchResult = await executeCommandOnBranch(COMMAND, mainBranch); - compareResults(featureBranchResult, mainBranchResult); - } catch (error) { - console.error("Switch to origin branch due to an error", error.message); - } - - await git.checkout(currentBranch.commit); - await git.checkout(currentBranch.name); - - console.log( - `${greyColor}Back to ${currentBranch.name} ${currentBranch.commit}${resetColor}`, - ); -})(); diff --git a/benchmark/bench-thread.js b/benchmark/bench-thread.js deleted file mode 100644 index ce68c24..0000000 --- a/benchmark/bench-thread.js +++ /dev/null @@ -1,34 +0,0 @@ -const { workerData: benchmark, parentPort } = require("node:worker_threads"); - -const Benchmark = require("benchmark"); -Benchmark.options.minSamples = 100; - -const suite = Benchmark.Suite(); - -const encodeString = require("../lib/internals/querystring").encodeString; -const parse = require("../lib/parse"); -const stringify = require("../lib/stringify"); - -switch (benchmark.type) { - case "encodeString": - suite.add(`${benchmark.type}: ${benchmark.name}`, () => { - encodeString(benchmark.input); - }); - break; - case "parse": - suite.add(`${benchmark.type}: ${benchmark.name}`, () => { - parse(benchmark.input); - }); - break; - case "stringify": - suite.add(`${benchmark.type}: ${benchmark.name}`, () => { - stringify(benchmark.input); - }); - break; -} - -suite - .on("cycle", (event) => { - parentPort.postMessage(String(event.target)); - }) - .run(); diff --git a/benchmark/bench.js b/benchmark/bench.js deleted file mode 100644 index fe2af29..0000000 --- a/benchmark/bench.js +++ /dev/null @@ -1,166 +0,0 @@ -const path = require("node:path"); -const { Worker } = require("node:worker_threads"); - -const BENCH_THREAD_PATH = path.join(__dirname, "bench-thread.js"); - -const benchmarks = [ - { - type: "encodeString", - name: '""', - input: "", - }, - { - type: "encodeString", - name: '"123"', - input: "123", - }, - { - type: "encodeString", - name: '"รค"', - input: "รค", - }, - { - type: "encodeString", - name: '"๐Œ†" ', - input: "๐Œ†", - }, - { - type: "stringify", - name: "undefined", - input: undefined, - }, - { - type: "stringify", - name: "null", - input: null, - }, - { - type: "stringify", - name: "{}", - input: {}, - }, - { - type: "stringify", - name: "{ id: true }", - input: { id: true }, - }, - { - type: "stringify", - name: "{ id: false }", - input: { id: false }, - }, - { - type: "stringify", - name: "{ id: 123 }", - input: { id: 123 }, - }, - { - type: "stringify", - name: "{ id: 1e+22 }", - input: { id: 1e22 }, - }, - { - type: "stringify", - name: "{ id: 123n }", - input: { id: 123n }, - }, - { - type: "stringify", - name: "{ id: Infinity }", - input: { id: Number.POSITIVE_INFINITY }, - }, - { - type: "stringify", - name: '{ id: ["1", "3"] }', - input: { id: ["1", "3"] }, - }, - { - type: "stringify", - name: '{ id: "" }', - input: { id: "" }, - }, - { - type: "stringify", - name: '{ id: "123" }', - input: { id: "123" }, - }, - { - type: "stringify", - name: '{ id: "รค" }', - input: { id: "รค" }, - }, - { - type: "stringify", - name: '{ id: "๐Œ†" } ', - input: { id: "๐Œ†" }, - }, - { - type: "stringify", - name: '{ foo: ["1", "3"], bar: "2" }', - input: { foo: ["1", "3"], bar: "2" }, - }, - { - type: "parse", - name: "", - input: "", - }, - { - type: "parse", - name: "id=123", - input: "id=123", - }, - { - type: "parse", - name: "id=123&id=123", - input: "id=123&id=123", - }, - { - type: "parse", - name: "full%20name=Yagiz", - input: "full%20name=Yagiz", - }, - { - type: "parse", - name: "invalid%key=hello", - input: "invalid%key=hello", - }, - { - type: "parse", - name: "my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F", - input: "my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F", - }, -]; - -async function runBenchmark(benchmark) { - const worker = new Worker(BENCH_THREAD_PATH, { workerData: benchmark }); - - return new Promise((resolve, reject) => { - let result = null; - worker.on("error", reject); - worker.on("message", (benchResult) => { - result = benchResult; - }); - worker.on("exit", (code) => { - if (code === 0) { - resolve(result); - } else { - reject(new Error(`Worker stopped with exit code ${code}`)); - } - }); - }); -} - -async function runBenchmarks() { - let maxNameLength = 0; - for (const benchmark of benchmarks) { - maxNameLength = Math.max(benchmark.name.length, maxNameLength); - } - - for (const benchmark of benchmarks) { - benchmark.name = benchmark.name.padEnd(maxNameLength, " "); - const resultMessage = await runBenchmark(benchmark); - console.log(resultMessage); - } -} - -runBenchmarks(); diff --git a/package.json b/package.json index 6d9b380..9a08c6b 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,6 @@ "test:watch": "vitest --watch", "test:coverage": "vitest --coverage", "coverage": "vitest run --coverage", - "benchmark": "node benchmark/bench.js", - "benchmark:cmp-branch": "node benchmark/bench-cmp-branch.js", "benchmark:parse": "node benchmark/parse.mjs", "benchmark:stringify": "node benchmark/stringify.mjs", "benchmark:import": "node benchmark/import.mjs" @@ -24,24 +22,24 @@ "author": "Yagiz Nizipli ", "license": "MIT", "devDependencies": { - "@aws-sdk/querystring-builder": "^3.357.0", - "@aws-sdk/querystring-parser": "^3.357.0", + "@aws-sdk/querystring-builder": "^3.370.0", + "@aws-sdk/querystring-parser": "^3.370.0", "@biomejs/biome": "1.8.3", - "@edge-runtime/vm": "^3.0.3", - "@types/node": "^20.4.1", - "@vitest/coverage-v8": "^0.34.1", + "@edge-runtime/vm": "^4.0.0", + "@types/node": "^20.14.10", + "@vitest/coverage-v8": "^2.0.1", "benchmark": "^2.1.4", "cli-select": "^1.1.2", - "cronometro": "^2.0.0", + "cronometro": "^3.0.2", "http-querystring-stringify": "^2.1.0", - "jsdom": "^24.0.0", - "qs": "^6.11.2", + "jsdom": "^24.1.0", + "qs": "^6.12.3", "query-string": "^9.0.0", "querystringify": "^2.2.0", "querystringify-ts": "^0.1.5", "querystringparser": "^0.1.1", - "simple-git": "^3.19.1", - "vitest": "^0.34.1" + "simple-git": "^3.25.0", + "vitest": "^2.0.1" }, "repository": { "url": "git+https://github.com/anonrig/fast-querystring.git",