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

Improve benchmarks #258

Merged
merged 5 commits into from
Sep 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 87 additions & 6 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict'

const http = require('node:http')

const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
const Request = require('../lib/request')
const parseURL = require('../lib/parseURL')
const Response = require('../lib/response')
const inject = require('..')
const parseURL = require('../lib/parse-url')
const { Readable } = require('stream')
const { assert } = require('console')
const { Bench } = require('tinybench')

const suite = new Bench()

const mockReq = {
url: 'http://localhost',
Expand Down Expand Up @@ -53,6 +57,28 @@ const mockReqCookiesPayload = {
bim: { bar: { boom: 'paf' } }
}
}
const mockReqCookiesPayloadBuffer = {
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
payload: Buffer.from('foo')
}
const mockReqCookiesPayloadReadable = () => ({
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
payload: Readable.from(['foo', 'bar', 'baz'])
})

suite
.add('Request', function () {
Expand All @@ -77,7 +103,62 @@ suite
xs: ['foo', 'bar']
})
})
.on('cycle', function (event) {
console.log(String(event.target))
.add('read request body JSON', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayload)
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('read request body buffer', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayloadBuffer)
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('read request body readable', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayloadReadable())
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('Response write end', function () {
const req = new Request(mockReq)
return new Promise((resolve) => {
const res = new Response(req, resolve)
res.write('foo')
res.end()
})
})
.add('Response writeHead end', function () {
const req = new Request(mockReq)
return new Promise((resolve) => {
const res = new Response(req, resolve)
res.writeHead(400, { 'content-length': 200 })
res.end()
})
})
.add('base inject', async function () {
const d = await inject((req, res) => {
req.on('data', () => {})
req.on('end', () => { res.end('1') })
}, mockReqCookiesPayload)
assert(d.payload === '1')
})
.run()
.then((tasks) => {
const errors = tasks.map(t => t.result?.error).filter((t) => t)
if (errors.length) {
errors.map((e) => console.error(e))
} else {
console.table(suite.table())
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@fastify/ajv-compiler": "^3.1.0",
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^20.1.0",
"benchmark": "^2.1.4",
"tinybench": "^2.5.1",
"end-of-stream": "^1.4.4",
"express": "^4.17.1",
"form-auto-content": "^3.0.0",
Expand Down