Skip to content

Commit

Permalink
make benchmarks parametric (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Nov 1, 2020
1 parent 908be7d commit 52deded
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 19 deletions.
111 changes: 94 additions & 17 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,94 @@
const { Writable } = require('stream')
const http = require('http')
const Benchmark = require('benchmark')
const { Client } = require('..')
const { Client, Pool } = require('..')

// # Start the Node.js server
// node benchmarks/server.js
//
// # Start the benchmarks
// node benchmarks/index.js

const httpOptions = {
const connections = parseInt(process.env.CONNECTIONS, 10) || 50
const parallelRequests = parseInt(process.env.PARALLEL, 10) || 10
const pipelining = parseInt(process.env.PIPELINING, 10) || 10
const dest = {}

if (process.env.PORT) {
dest.port = process.env.PORT
dest.url = `https://localhost:${process.env.PORT}`
} else {
dest.url = 'https://localhost'
dest.socketPath = '/var/tmp/undici.sock'
}

const httpNoAgent = {
protocol: 'http:',
hostname: 'localhost',
socketPath: '/var/tmp/undici.sock',
method: 'GET',
path: '/',
...dest
}

const httpOptions = {
...httpNoAgent,
agent: new http.Agent({
keepAlive: true,
maxSockets: 1
})
}

const httpOptionsMultiSocket = {
...httpNoAgent,
agent: new http.Agent({
keepAlive: true,
maxSockets: connections
})
}

const undiciOptions = {
path: '/',
method: 'GET',
headersTimeout: 0,
bodyTimeout: 0
}

const client = new Client(`https://${httpOptions.hostname}`, {
pipelining: 10,
socketPath: '/var/tmp/undici.sock'
const client = new Client(httpOptions.url, {
pipelining,
...dest
})

client.on('disconnect', (err) => {
throw err
const pool = new Pool(httpOptions.url, {
pipelining,
connections,
...dest
})

const suite = new Benchmark.Suite()

Benchmark.options.minSamples = 200
// Benchmark.options.minSamples = 200

suite
.add('http - no agent ', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
http.get(httpOptions, (res) => {
res
.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
}))
.on('finish', resolve)
})
}))).then(() => deferred.resolve())
}
})
.add('http - keepalive', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => new Promise((resolve) => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
http.get(httpOptions, (res) => {
res
.pipe(new Writable({
Expand All @@ -59,10 +102,26 @@ suite
}))).then(() => deferred.resolve())
}
})
.add('http - keepalive - multiple sockets', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
http.get(httpOptionsMultiSocket, (res) => {
res
.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
}))
.on('finish', resolve)
})
}))).then(() => deferred.resolve())
}
})
.add('undici - pipeline', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => new Promise((resolve) => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
client
.pipeline(undiciOptions, data => {
return data.body
Expand All @@ -80,7 +139,7 @@ suite
.add('undici - request', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => new Promise((resolve) => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
client
.request(undiciOptions)
.then(({ body }) => {
Expand All @@ -95,10 +154,28 @@ suite
}))).then(() => deferred.resolve())
}
})
.add('undici - pool - request - multiple sockets', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
pool
.request(undiciOptions)
.then(({ body }) => {
body
.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
}
}))
.on('finish', resolve)
})
}))).then(() => deferred.resolve())
}
})
.add('undici - stream', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => {
Promise.all(Array.from(Array(parallelRequests)).map(() => {
return client.stream(undiciOptions, () => {
return new Writable({
write (chunk, encoding, callback) {
Expand All @@ -112,23 +189,23 @@ suite
.add('undici - dispatch', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => new Promise((resolve) => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
client.dispatch(undiciOptions, new SimpleRequest(resolve))
}))).then(() => deferred.resolve())
}
})
.add('undici - noop', {
defer: true,
fn: deferred => {
Promise.all(Array.from(Array(10)).map(() => new Promise((resolve) => {
Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise((resolve) => {
client.dispatch(undiciOptions, new NoopRequest(resolve))
}))).then(() => deferred.resolve())
}
})
.on('cycle', ({ target }) => {
// Multiply results by 10x to get opts/sec since we do 10 requests
// Multiply results by parallelRequests to get opts/sec since we do mutiple requests
// per run.
target.hz *= 10
target.hz *= parallelRequests
console.log(String(target))
})
.on('complete', () => {
Expand Down
9 changes: 7 additions & 2 deletions benchmarks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const { createServer } = require('http')

const port = process.env.PORT || '/var/tmp/undici.sock'
const timeout = parseInt(process.env.TIMEOUT, 10) || 1

createServer((req, res) => {
res.end('hello world')
}).listen('/var/tmp/undici.sock')
setTimeout(function () {
res.end('hello world')
}, timeout)
}).listen(port)

0 comments on commit 52deded

Please sign in to comment.