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 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
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
62 changes: 0 additions & 62 deletions bench/busboy-bench.js

This file was deleted.

57 changes: 0 additions & 57 deletions bench/fastify-busboy-bench.js

This file was deleted.

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')
.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(1000)
},

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

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

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
}
Loading