Skip to content
Open
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
105 changes: 68 additions & 37 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const { promisify } = require('node:util')
const request = promisify(require('request'))

const iterations = (parseInt(process.env.SAMPLES, 10) || 10) + 1
const errorThreshold = parseInt(process.env.ERROR_THRESHOLD, 10) || 3
const parsedErrorThreshold = parseInt(process.env.ERROR_THRESHOLD, 10)
const errorThreshold = Number.isNaN(parsedErrorThreshold) ? 3 : parsedErrorThreshold
const connections = parseInt(process.env.CONNECTIONS, 10) || 50
const pipelining = parseInt(process.env.PIPELINING, 10) || 10
const headersTimeout = parseInt(process.env.HEADERS_TIMEOUT, 10) || 0
Expand Down Expand Up @@ -121,10 +122,7 @@ async function cleanup () {
requestAgent.destroy()
superagentAgent.destroy()

await Promise.allSettled([
dispatcher.destroy(),
globalDispatcher.destroy()
])
await Promise.allSettled([dispatcher.destroy(), globalDispatcher.destroy()])
}

class SimpleRequest {
Expand Down Expand Up @@ -246,21 +244,36 @@ if (process.env.PORT) {
// fetch does not support the socket
experiments['undici - fetch'] = () => {
return makeParallelRequests(resolve => {
fetch(dest.url).then(res => {
res.body.pipeTo(new WritableStream({ write () { }, close () { resolve() } }))
}).catch(console.log)
fetch(dest.url)
.then(res => {
res.body.pipeTo(
new WritableStream({
write () {},
close () {
resolve()
}
})
)
})
.catch(console.log)
})
}

experiments['node-fetch'] = () => {
return makeParallelRequests(resolve => {
nodeFetch(dest.url, { agent: fetchAgent }).then(res => {
res.body.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
})).on('finish', resolve)
}).catch(console.log)
nodeFetch(dest.url, { agent: fetchAgent })
.then(res => {
res.body
.pipe(
new Writable({
write (chunk, encoding, callback) {
callback()
}
})
)
.on('finish', resolve)
})
.catch(console.log)
})
}

Expand All @@ -272,13 +285,20 @@ if (process.env.PORT) {
}
experiments.axios = () => {
return makeParallelRequests(resolve => {
axios.request(axiosOptions).then(res => {
res.data.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
})).on('finish', resolve)
}).catch(console.log)
axios
.request(axiosOptions)
.then(res => {
res.data
.pipe(
new Writable({
write (chunk, encoding, callback) {
callback()
}
})
)
.on('finish', resolve)
})
.catch(console.log)
})
}

Expand All @@ -293,11 +313,15 @@ if (process.env.PORT) {
}
experiments.got = () => {
return makeParallelRequests(resolve => {
got(gotOptions).pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
})).on('finish', resolve)
got(gotOptions)
.pipe(
new Writable({
write (chunk, encoding, callback) {
callback()
}
})
)
.on('finish', resolve)
})
}

Expand All @@ -310,20 +334,27 @@ if (process.env.PORT) {
}
experiments.request = () => {
return makeParallelRequests(resolve => {
request(requestOptions).then(() => {
// already body consumed
resolve()
}).catch(console.log)
request(requestOptions)
.then(() => {
// already body consumed
resolve()
})
.catch(console.log)
})
}

experiments.superagent = () => {
return makeParallelRequests(resolve => {
superagent.get(dest.url).pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
})).on('finish', resolve)
superagent
.get(dest.url)
.pipe(
new Writable({
write (chunk, encoding, callback) {
callback()
}
})
)
.on('finish', resolve)
})
}
}
Expand All @@ -336,7 +367,7 @@ async function main () {
got = _got.default
const _superagent = await import('superagent')
// https://github.com/ladjs/superagent/issues/1540#issue-561464561
superagent = _superagent.agent().use((req) => req.agent(superagentAgent))
superagent = _superagent.agent().use(req => req.agent(superagentAgent))

// cronometro runs each benchmark in a worker, so attach cleanup to every
// test before the worker exits.
Expand Down
29 changes: 29 additions & 0 deletions build/milo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const { execFileSync } = require('node:child_process')
const { cpSync, existsSync, mkdirSync, rmSync } = require('node:fs')
const { join, resolve } = require('node:path')

const ROOT = resolve(__dirname, '../')
const OUT = join(ROOT, 'lib/milo')
const PACKAGE = '@perseveranza-pets/milo-cjs@latest'
const PACKAGE_DIR = process.env.MILO_PACKAGE_DIR

rmSync(OUT, { recursive: true, force: true })
mkdirSync(OUT, { recursive: true })

if (PACKAGE_DIR) {
cpSync(PACKAGE_DIR, OUT, { recursive: true })
} else {
const packResult = JSON.parse(execFileSync('npm', ['pack', PACKAGE, '--json'], { cwd: OUT, encoding: 'utf8' }))

const tarball = join(OUT, packResult[0].filename)
if (!existsSync(tarball)) {
throw new Error('Failed to download milo package')
}

execFileSync('tar', ['-xzf', tarball, '-C', OUT, '--strip-components=1'], {
stdio: 'inherit'
})
rmSync(tarball)
}
Loading
Loading