0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/benchmarks/handle-event/index.js

150 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-12-27 09:24:00 +00:00
import Benchmark from 'benchmark'
import { makeEdgeEnv } from 'edge-mock'
import { Router as IttyRouter } from 'itty-router'
import { Request, Response } from 'node-fetch'
2021-12-27 09:24:00 +00:00
import { Router as SunderRouter, Sunder } from 'sunder'
import { Router as WorktopRouter } from 'worktop'
import { Hono } from '../../dist/hono'
2023-02-10 21:19:23 +00:00
import { RegExpRouter } from '../../dist/router/reg-exp-router'
2021-12-27 09:24:00 +00:00
globalThis.Request = Request
globalThis.Response = Response
const initHono = (hono) => {
hono.get('/user', () => new Response('User'))
hono.get('/user/comments', () => new Response('User Comments'))
hono.get('/user/avatar', () => new Response('User Avatar'))
hono.get('/user/lookup/email/:address', () => new Response('User Lookup Email Address'))
hono.get('/event/:id', () => new Response('Event'))
hono.get('/event/:id/comments', () => new Response('Event Comments'))
hono.post('/event/:id/comments', () => new Response('POST Event Comments'))
hono.post('/status', () => new Response('Status'))
hono.get('/very/deeply/nested/route/hello/there', () => new Response('Very Deeply Nested Route'))
hono.get('/user/lookup/username/:username', (c) => {
return new Response(`Hello ${c.req.param('username')}`)
2021-12-27 09:24:00 +00:00
})
return hono
}
2023-02-10 21:19:23 +00:00
const hono = initHono(new Hono({ router: new RegExpRouter() }))
2021-12-27 09:24:00 +00:00
// itty-router
2021-12-27 23:51:05 +00:00
const ittyRouter = IttyRouter()
2021-12-27 09:24:00 +00:00
ittyRouter.get('/user', () => new Response('User'))
ittyRouter.get('/user/comments', () => new Response('User Comments'))
ittyRouter.get('/user/avatar', () => new Response('User Avatar'))
ittyRouter.get('/user/lookup/email/:address', () => new Response('User Lookup Email Address'))
ittyRouter.get('/event/:id', () => new Response('Event'))
ittyRouter.get('/event/:id/comments', () => new Response('Event Comments'))
ittyRouter.post('/event/:id/comments', () => new Response('POST Event Comments'))
ittyRouter.post('/status', () => new Response('Status'))
ittyRouter.get(
'/very/deeply/nested/route/hello/there',
() => new Response('Very Deeply Nested Route')
)
2021-12-27 09:24:00 +00:00
ittyRouter.get('/user/lookup/username/:username', ({ params }) => {
return new Response(`Hello ${params.username}`, {
status: 200,
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
},
})
})
// Sunder
2021-12-27 09:24:00 +00:00
const sunderRouter = new SunderRouter()
2021-12-27 23:51:05 +00:00
sunderRouter.get('/user', (ctx) => {
ctx.response.body = 'User'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/user/comments', (ctx) => {
ctx.response.body = 'User Comments'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/user/avatar', (ctx) => {
ctx.response.body = 'User Avatar'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/user/lookup/email/:address', (ctx) => {
ctx.response.body = 'User Lookup Email Address'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/event/:id', (ctx) => {
ctx.response.body = 'Event'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/event/:id/comments', (ctx) => {
ctx.response.body = 'Event Comments'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.post('/event/:id/comments', (ctx) => {
ctx.response.body = 'POST Event Comments'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.post('/status', (ctx) => {
ctx.response.body = 'Status'
2021-12-27 09:24:00 +00:00
})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/very/deeply/nested/route/hello/there', (ctx) => {
ctx.response.body = 'Very Deeply Nested Route'
2021-12-27 09:24:00 +00:00
})
//sunderRouter.get('/static/*', () => {})
2021-12-27 23:51:05 +00:00
sunderRouter.get('/user/lookup/username/:username', (ctx) => {
ctx.response.body = `Hello ${ctx.params.username}`
2021-12-27 09:24:00 +00:00
})
const sunderApp = new Sunder()
sunderApp.use(sunderRouter.middleware)
// worktop
const worktopRouter = new WorktopRouter()
2022-03-12 04:02:37 +00:00
worktopRouter.add('GET', '/user', async (req, res) => res.send(200, 'User'))
worktopRouter.add('GET', '/user/comments', (req, res) => res.send(200, 'User Comments'))
worktopRouter.add('GET', '/user/avatar', (req, res) => res.send(200, 'User Avatar'))
worktopRouter.add('GET', '/user/lookup/email/:address', (req, res) =>
res.send(200, 'User Lookup Email Address')
)
worktopRouter.add('GET', '/event/:id', (req, res) => res.send(200, 'Event'))
worktopRouter.add('POST', '/event/:id/comments', (req, res) => res.send(200, 'POST Event Comments'))
worktopRouter.add('POST', '/status', (req, res) => res.send(200, 'Status'))
worktopRouter.add('GET', '/very/deeply/nested/route/hello/there', (req, res) =>
res.send(200, 'Very Deeply Nested Route')
)
worktopRouter.add('GET', '/user/lookup/username/:username', (req, res) =>
res.send(200, `Hello ${req.params.username}`)
)
2021-12-27 09:24:00 +00:00
// Request Object
const request = new Request('http://localhost/user/lookup/username/hey', { method: 'GET' })
makeEdgeEnv()
2021-12-27 09:24:00 +00:00
// FetchEvent Object
const event = new FetchEvent('fetch', { request })
const fn = async () => {
let res = await hono.handleEvent(event)
console.log(await res.text())
res = await ittyRouter.handle(event.request)
console.log(await res.text())
res = await sunderApp.handle(event)
console.log(await res.text())
res = await worktopRouter.run(event)
console.log(await res.text())
2021-12-27 09:24:00 +00:00
}
fn()
const suite = new Benchmark.Suite()
2021-12-27 09:24:00 +00:00
suite
.add('Hono', async () => {
await hono.handleEvent(event)
2021-12-27 09:24:00 +00:00
})
.add('itty-router', async () => {
await ittyRouter.handle(event.request)
2021-12-27 09:24:00 +00:00
})
2021-12-27 22:41:53 +00:00
.add('sunder', async () => {
await sunderApp.handle(event)
2021-12-27 09:24:00 +00:00
})
.add('worktop', async () => {
await worktopRouter.run(event)
})
2021-12-27 09:24:00 +00:00
.on('cycle', (event) => {
console.log(String(event.target))
})
.on('complete', function () {
console.log(`Fastest is ${this.filter('fastest').map('name')}`)
})
.run({ async: true })