|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { spawn } = require("node:child_process"); |
| 4 | + |
| 5 | +const cliSelect = require("cli-select"); |
| 6 | +const simpleGit = require("simple-git"); |
| 7 | + |
| 8 | +const git = simpleGit(process.cwd()); |
| 9 | + |
| 10 | +const COMMAND = "npm run benchmark"; |
| 11 | +const DEFAULT_BRANCH = "main"; |
| 12 | +const PERCENT_THRESHOLD = 5; |
| 13 | +const greyColor = "\x1b[30m"; |
| 14 | +const redColor = "\x1b[31m"; |
| 15 | +const greenColor = "\x1b[32m"; |
| 16 | +const resetColor = "\x1b[0m"; |
| 17 | + |
| 18 | +async function selectBranchName(message, branches) { |
| 19 | + console.log(message); |
| 20 | + const result = await cliSelect({ |
| 21 | + type: "list", |
| 22 | + name: "branch", |
| 23 | + values: branches, |
| 24 | + }); |
| 25 | + console.log(result.value); |
| 26 | + return result.value; |
| 27 | +} |
| 28 | + |
| 29 | +async function executeCommandOnBranch(command, branch) { |
| 30 | + console.log(`${greyColor}Checking out "${branch}"${resetColor}`); |
| 31 | + await git.checkout(branch); |
| 32 | + |
| 33 | + console.log(`${greyColor}Execute "${command}"${resetColor}`); |
| 34 | + const childProcess = spawn(command, { stdio: "pipe", shell: true }); |
| 35 | + |
| 36 | + let result = ""; |
| 37 | + childProcess.stdout.on("data", (data) => { |
| 38 | + process.stdout.write(data.toString()); |
| 39 | + result += data.toString(); |
| 40 | + }); |
| 41 | + |
| 42 | + await new Promise((resolve) => childProcess.on("close", resolve)); |
| 43 | + |
| 44 | + console.log(); |
| 45 | + |
| 46 | + return parseBenchmarksStdout(result); |
| 47 | +} |
| 48 | + |
| 49 | +function parseBenchmarksStdout(text) { |
| 50 | + const results = []; |
| 51 | + |
| 52 | + const lines = text.split("\n"); |
| 53 | + for (const line of lines) { |
| 54 | + const match = /^(.+?)(\.*) x (.+) ops\/sec .*$/.exec(line); |
| 55 | + if (match !== null) { |
| 56 | + results.push({ |
| 57 | + name: match[1], |
| 58 | + alignedName: match[1] + match[2], |
| 59 | + result: parseInt(match[3].split(",").join("")), |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return results; |
| 65 | +} |
| 66 | + |
| 67 | +function compareResults(featureBranch, mainBranch) { |
| 68 | + for (const { name, alignedName, result: mainBranchResult } of mainBranch) { |
| 69 | + const featureBranchBenchmark = featureBranch.find( |
| 70 | + (result) => result.name === name, |
| 71 | + ); |
| 72 | + if (featureBranchBenchmark) { |
| 73 | + const featureBranchResult = featureBranchBenchmark.result; |
| 74 | + const percent = |
| 75 | + ((featureBranchResult - mainBranchResult) * 100) / mainBranchResult; |
| 76 | + const roundedPercent = Math.round(percent * 100) / 100; |
| 77 | + |
| 78 | + const percentString = |
| 79 | + roundedPercent > 0 ? `+${roundedPercent}%` : `${roundedPercent}%`; |
| 80 | + const message = alignedName + percentString.padStart(7, "."); |
| 81 | + |
| 82 | + if (roundedPercent > PERCENT_THRESHOLD) { |
| 83 | + console.log(`${greenColor}${message}${resetColor}`); |
| 84 | + } else if (roundedPercent < -PERCENT_THRESHOLD) { |
| 85 | + console.log(`${redColor}${message}${resetColor}`); |
| 86 | + } else { |
| 87 | + console.log(message); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +(async function () { |
| 94 | + const branches = await git.branch(); |
| 95 | + const currentBranch = branches.branches[branches.current]; |
| 96 | + |
| 97 | + let featureBranch = null; |
| 98 | + let mainBranch = null; |
| 99 | + |
| 100 | + if (process.argv[2] === "--ci") { |
| 101 | + featureBranch = currentBranch.name; |
| 102 | + mainBranch = DEFAULT_BRANCH; |
| 103 | + } else { |
| 104 | + featureBranch = await selectBranchName( |
| 105 | + "Select the branch you want to compare (feature branch):", |
| 106 | + branches.all, |
| 107 | + ); |
| 108 | + mainBranch = await selectBranchName( |
| 109 | + "Select the branch you want to compare with (main branch):", |
| 110 | + branches.all, |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + const featureBranchResult = await executeCommandOnBranch( |
| 116 | + COMMAND, |
| 117 | + featureBranch, |
| 118 | + ); |
| 119 | + const mainBranchResult = await executeCommandOnBranch(COMMAND, mainBranch); |
| 120 | + compareResults(featureBranchResult, mainBranchResult); |
| 121 | + } catch (error) { |
| 122 | + console.error("Switch to origin branch due to an error", error.message); |
| 123 | + } |
| 124 | + |
| 125 | + await git.checkout(currentBranch.commit); |
| 126 | + await git.checkout(currentBranch.name); |
| 127 | + |
| 128 | + console.log( |
| 129 | + `${greyColor}Back to ${currentBranch.name} ${currentBranch.commit}${resetColor}`, |
| 130 | + ); |
| 131 | +})(); |
0 commit comments