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 more powerful benchmarks #54

Merged
merged 11 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/node_modules/
/package-lock.json
/.nyc_output
/coverage
/coverage
/benchmarks/node_modules/
/benchmarks/package-lock.json
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions benchmarks/_results/Busboy_comparison-busboy-Node_12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"runtimeVersion": "12.22.7, V8 7.8.279.23-node.56",
"benchmarkName": "Busboy comparison",
"benchmarkEntryName": "busboy",
"benchmarkCycles": 10,
"benchmarkCycleSamples": 50,
"warmupCycles": 10,
"meanTimeNs": 1945927.3472222222,
"meanTimeMs": 1.9459273472222223
}
38 changes: 38 additions & 0 deletions benchmarks/busboy/contestants/busboy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Busboy = require('busboy')
const { buffer, boundary } = require('../data')

function process () {
const busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
})
let processedData = ''

return new Promise((resolve, reject) => {
busboy.on('file', (field, file, filename, encoding, mimetype) => {
// console.log('read file')
file.on('data', (data) => {
processedData += data.toString()
// console.log(`File [${filename}] got ${data.length} bytes`);
})
file.on('end', (fieldname) => {
// console.log(`File [${fieldname}] Finished`);
})
})

busboy.on('error', function (err) {
reject(err)
})
busboy.on('finish', function () {
resolve(processedData)
})
busboy.write(buffer, () => { })

busboy.end()
})
}

module.exports = {
process
}
39 changes: 39 additions & 0 deletions benchmarks/busboy/contestants/fastify-busboy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Busboy = require('../../../lib/main')
const { buffer, boundary } = require('../data')

function process () {
const busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
})

let processedData = ''

return new Promise((resolve, reject) => {
busboy.on('file', (field, file, filename, encoding, mimetype) => {
// console.log('read file')
file.on('data', (data) => {
processedData += data.toString()
// console.log(`File [${filename}] got ${data.length} bytes`);
})
file.on('end', (fieldname) => {
// console.log(`File [${fieldname}] Finished`);
})
})

busboy.on('error', function (err) {
reject(err)
})
busboy.on('finish', function () {
resolve(processedData)
})
busboy.write(buffer, () => { })

busboy.end()
})
}

module.exports = {
process
}
32 changes: 32 additions & 0 deletions benchmarks/busboy/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const boundary = '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k'
const randomContent = Buffer.from(makeString(1024 * 500), 'utf8')
const buffer = createMultipartBuffer(boundary)

function makeString (length) {
let result = ''
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
for (var i = 0; i < length; i++) { // eslint-disable-line no-var
result += characters.charAt(Math.floor(Math.random() *
charactersLength))
}
return result
}

function createMultipartBuffer (boundary) {
const payload = [
'--' + boundary,
'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
'Content-Type: application/octet-stream',
'',
randomContent,
'--' + boundary + '--'
].join('\r\n')
return Buffer.from(payload, 'ascii')
}

module.exports = {
boundary,
buffer,
randomContent
}
48 changes: 48 additions & 0 deletions benchmarks/busboy/executioner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { process: processBusboy } = require('./contestants/busboy')
const { process: processFastify } = require('./contestants/fastify-busboy')
const { getCommonBuilder } = require('../common/commonBuilder')
const { validateAccuracy } = require('./validator')
const { resolveContestant } = require('../common/contestantResolver')
const { outputResults } = require('../common/resultUtils')

const contestants = {
busboy: measureBusboy,
fastify: measureFastify
}

async function measureBusboy () {
const benchmark = getCommonBuilder()
.benchmarkName('Busboy comparison')
.benchmarkEntryName('busboy')
.asyncFunctionUnderTest(processBusboy)
.build()
const benchmarkResults = await benchmark.executeAsync()
outputResults(benchmark, benchmarkResults)
}

async function measureFastify () {
const benchmark = getCommonBuilder()
.benchmarkName('Busboy comparison')
.benchmarkEntryName('@fastify/busboy')
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
.asyncFunctionUnderTest(processFastify)
.build()
const benchmarkResults = await benchmark.executeAsync()
outputResults(benchmark, benchmarkResults)
}

function execute () {
return validateAccuracy(processBusboy())
.then(() => {
return validateAccuracy(processFastify())
})
.then(() => {
const contestant = resolveContestant(contestants)
return contestant()
}).then(() => {
console.log('all done')
}).catch((err) => {
console.error(`Something went wrong: ${err.message}`)
})
}

execute()
17 changes: 17 additions & 0 deletions benchmarks/busboy/regenerate.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
rem Make sure to run this in Admin account
rem
call npm run install-node
timeout /t 2
call nvm use 17.2.0
timeout /t 2
call npm run benchmark-all
call nvm use 16.13.1
timeout /t 2
call npm run benchmark-all
call nvm use 14.18.2
timeout /t 2
call npm run benchmark-all
call nvm use 12.22.7
timeout /t 2
call npm run benchmark-all
call npm run combine-results
13 changes: 13 additions & 0 deletions benchmarks/busboy/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { validateEqual } = require('validation-utils')
const { randomContent } = require('./data')

const EXPECTED_RESULT = randomContent.toString()

async function validateAccuracy (actualResultPromise) {
const result = await actualResultPromise
validateEqual(result, EXPECTED_RESULT)
}

module.exports = {
validateAccuracy
}
44 changes: 44 additions & 0 deletions benchmarks/common/commonBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { validateNotNil } = require('validation-utils')
const { BenchmarkBuilder } = require('photofinish')
const getopts = require('getopts')

const options = getopts(process.argv.slice(1), {
alias: {
preset: 'p'
},
default: {}
})

const PRESET = {
LOW: (builder) => {
return builder
.warmupCycles(1000)
.benchmarkCycles(100)
},

MEDIUM: (builder) => {
return builder
.warmupCycles(1000)
.benchmarkCycles(1000)
},

HIGH: (builder) => {
return builder
.warmupCycles(1000)
.benchmarkCycles(100000)
}
}

function getCommonBuilder () {
const presetId = options.preset || 'LOW'
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
const preset = validateNotNil(PRESET[presetId.toUpperCase()], `Unknown preset: ${presetId}`)

const builder = new BenchmarkBuilder()
preset(builder)
return builder
.benchmarkCycleSamples(50)
}

module.exports = {
getCommonBuilder
}
24 changes: 24 additions & 0 deletions benchmarks/common/contestantResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getopts = require('getopts')

const options = getopts(process.argv.slice(1), {
alias: {
contestant: 'c'
},
default: {}
})

function resolveContestant (contestants) {
const contestantId = options.contestant
const contestant = Number.isFinite(contestantId)
? Object.values(contestants)[contestantId]
: contestants[contestantId]

if (!contestant) {
throw new Error(`Unknown contestant ${contestantId}`)
}
return contestant
}

module.exports = {
resolveContestant
}
16 changes: 16 additions & 0 deletions benchmarks/common/executionUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { getCommonBuilder } = require('./commonBuilder')
const { outputResults } = require('./resultUtils')

function getMeasureFn (constestandId, fn) {
return () => {
const benchmark = getCommonBuilder()
.benchmarkEntryName(constestandId)
.functionUnderTest(fn).build()
const benchmarkResults = benchmark.execute()
outputResults(benchmark, benchmarkResults)
}
}

module.exports = {
getMeasureFn
}
15 changes: 15 additions & 0 deletions benchmarks/common/resultUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { exportResults } = require('photofinish')

function outputResults (benchmark, benchmarkResults) {
console.log(
`Mean time for ${
benchmark.benchmarkEntryName
} is ${benchmarkResults.meanTime.getTimeInNanoSeconds()} nanoseconds`
)

exportResults(benchmarkResults, { exportPath: '_results' })
}

module.exports = {
outputResults
}
52 changes: 52 additions & 0 deletions benchmarks/common/resultsCombinator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs')
const path = require('path')
const getopts = require('getopts')
const systemInformation = require('systeminformation')
const { loadResults } = require('photofinish')

const options = getopts(process.argv.slice(1), {
alias: {
resultsDir: 'r',
precision: 'p'
},
default: {}
})

const { generateTable } = require('photofinish')

async function getSpecs () {
const cpuInfo = await systemInformation.cpu()

return {
cpu: {
brand: cpuInfo.brand,
speed: `${cpuInfo.speed} GHz`
}
}
}

async function saveTable () {
const baseResultsDir = options.resultsDir
const benchmarkResults = await loadResults(baseResultsDir)

const table = generateTable(benchmarkResults, {
precision: options.precision,
sortBy: [
{ field: 'meanTimeNs', order: 'asc' }
]
})

const specs = await getSpecs()

console.log(specs)
console.log(table)

const targetFilePath = path.resolve(baseResultsDir, 'results.md')
fs.writeFileSync(
targetFilePath,
`${table}` +
`\n\n**Specs**: ${specs.cpu.brand} (${specs.cpu.speed})`
)
}

saveTable()
21 changes: 21 additions & 0 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "dto",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"getopts": "^2.3.0",
"photofinish": "^1.8.0",
"systeminformation": "^5.9.15",
"tslib": "^2.3.1",
"validation-utils": "^7.0.0"
},
"scripts": {
"install-node": "nvm install 17.2.0 && nvm install 16.13.1 && nvm install 14.18.2 && nvm install 12.22.7",
"benchmark-busboy": "node busboy/executioner.js -c 0",
"benchmark-fastify": "node busboy/executioner.js -c 1",
"benchmark-all": "npm run benchmark-busboy && npm run benchmark-fastify -p high",
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
"benchmark-all-medium": "npm run benchmark-busboy && npm run benchmark-fastify -p medium",

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"benchmark-all-medium": "npm run benchmark-busboy && npm run benchmark-fastify -p medium",
"benchmark-all-medium": "npm run benchmark-busboy -- -p medium && npm run benchmark-fastify -- -p medium",

"benchmark-all-low": "npm run benchmark-busboy && npm run benchmark-fastify -p low",
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
"combine-results": "node common/resultsCombinator.js -r _results -p 6"
}
}
Loading