Skip to content

Commit

Permalink
adding benchmarking tool
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Apr 6, 2024
1 parent 086443a commit 9ea0db2
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const httpNext = require('./index')
const httpPrevious = require('0http')

function getReqObject (url) {
const req = {}
req.method = 'GET'
req.url = url

return req
}

function getResObject () {
const res = {}
res.statusCode = null
res.setHeader = () => {}
res.writeHead = () => {}
res.end = () => {}
return res
}

function setupRouter (router) {
router.use((req, res, next) => {
return next()
})

router.get('/', (req, res) => {
return res.end('OK')
})
router.get('/:id', (req, res) => {
return res.end(req.params.id)
})
router.get('/:id/error', () => {
throw new Error('Error')
})
}

const { router } = httpNext({
cacheSize: 0,
id: '/'
})
setupRouter(router)

const { router: routerPrevious } = httpPrevious({
cacheSize: 0
})
setupRouter(routerPrevious)

import('mitata/src/cli.mjs').then(({ run, bench, group, baseline }) => {
group('Next Router', () => {
baseline('Base URL', () => {
const req = getReqObject('/')
const res = getResObject()

router.lookup(req, res)
})
bench('Parameter URL', () => {
const req = getReqObject('/0')
const res = getResObject()

router.lookup(req, res)
})
bench('Not Found URL', () => {
const req = getReqObject('/0/404')
const res = getResObject()

router.lookup(req, res)
})
bench('Error URL', () => {
const req = getReqObject('/0/error')
const res = getResObject()

router.lookup(req, res)
})
})

group('Previous Router', () => {
baseline('Base URL', () => {
const req = getReqObject('/')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Parameter URL', () => {
const req = getReqObject('/0')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Not Found URL', () => {
const req = getReqObject('/0/404')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Error URL', () => {
const req = getReqObject('/0/error')
const res = getResObject()

routerPrevious.lookup(req, res)
})
})

run({
silent: false,
avg: true,
json: false,
colors: true,
min_max: false,
percentiles: false
})
})

0 comments on commit 9ea0db2

Please sign in to comment.