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

Add comparison of benchmarks results on different branches #268

Merged
Merged
Show file tree
Hide file tree
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
114 changes: 114 additions & 0 deletions bench-cmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use strict'

const { spawn } = require('child_process')

const chalk = require('chalk')
const inquirer = require('inquirer')
const simpleGit = require('simple-git')

const git = simpleGit(process.cwd())

const COMMAND = 'npm run bench'
const DEFAULT_BRANCH = 'main'
const PERCENT_THRESHOLD = 5

async function selectBranchName (message, branches) {
const result = await inquirer.prompt([{
type: 'list',
name: 'branch',
choices: branches,
loop: false,
pageSize: 20,
message
}])
return result.branch
}

async function executeCommandOnBranch (command, branch) {
console.log(chalk.grey(`Checking out "${branch}"`))
await git.checkout(branch)

console.log(chalk.grey(`Execute "${command}"`))
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: 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(chalk.green(message))
} else if (roundedPercent < -PERCENT_THRESHOLD) {
console.log(chalk.red(message))
} else {
console.log(message)
}
}
}
}

(async function () {
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(chalk.gray(`Back to ${currentBranch.name} ${currentBranch.commit}`))
})()
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"types": "index.d.ts",
"scripts": {
"bench": "node bench.js",
"bench:cmp": "node bench-cmp.js",
"bench:cmp:ci": "node bench-cmp.js --ci",
"test:lint": "standard",
"test:typescript": "tsd",
"test": "standard && tap -J test/*.test.js && npm run test:typescript",
Expand Down Expand Up @@ -35,7 +37,10 @@
"devDependencies": {
"@types/node": "^14.0.27",
"benchmark": "^2.1.4",
"chalk": "^4.1.2",
"inquirer": "^8.2.4",
"pre-commit": "^1.2.2",
"simple-git": "^3.7.1",
"standard": "^14.3.4",
"tap": "^16.0.1",
"tap-mocha-reporter": "^5.0.1",
Expand Down