2021-12-27 09:24:00 +00:00
|
|
|
import Benchmark from 'benchmark'
|
2022-06-02 06:10:57 +00:00
|
|
|
import { makeEdgeEnv } from 'edge-mock'
|
2023-01-03 21:19:18 +00:00
|
|
|
import { Router as IttyRouter } from 'itty-router'
|
2022-05-31 20:48:20 +00:00
|
|
|
import { Request, Response } from 'node-fetch'
|
2021-12-27 09:24:00 +00:00
|
|
|
import { Router as SunderRouter, Sunder } from 'sunder'
|
2022-01-08 21:05:04 +00:00
|
|
|
import { Router as WorktopRouter } from 'worktop'
|
2023-01-03 21:19:18 +00:00
|
|
|
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
|
|
|
|
2022-05-31 20:48:20 +00:00
|
|
|
globalThis.Request = Request
|
|
|
|
globalThis.Response = Response
|
|
|
|
|
2022-02-20 10:14:46 +00:00
|
|
|
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) => {
|
2022-05-31 20:48:20 +00:00
|
|
|
return new Response(`Hello ${c.req.param('username')}`)
|
2021-12-27 09:24:00 +00:00
|
|
|
})
|
2022-02-20 10:14:46 +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
|
|
|
|
2022-05-31 20:48:20 +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'))
|
2022-02-28 12:56:50 +00:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-31 20:48:20 +00:00
|
|
|
// 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)
|
|
|
|
|
2022-01-08 21:05:04 +00:00
|
|
|
// worktop
|
|
|
|
const worktopRouter = new WorktopRouter()
|
2022-03-12 04:02:37 +00:00
|
|
|
worktopRouter.add('GET', '/user', async (req, res) => res.send(200, 'User'))
|
2022-01-08 21:05:04 +00:00
|
|
|
worktopRouter.add('GET', '/user/comments', (req, res) => res.send(200, 'User Comments'))
|
|
|
|
worktopRouter.add('GET', '/user/avatar', (req, res) => res.send(200, 'User Avatar'))
|
2022-02-28 12:56:50 +00:00
|
|
|
worktopRouter.add('GET', '/user/lookup/email/:address', (req, res) =>
|
|
|
|
res.send(200, 'User Lookup Email Address')
|
|
|
|
)
|
2022-01-08 21:05:04 +00:00
|
|
|
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')
|
|
|
|
)
|
2022-02-28 12:56:50 +00:00
|
|
|
worktopRouter.add('GET', '/user/lookup/username/:username', (req, res) =>
|
|
|
|
res.send(200, `Hello ${req.params.username}`)
|
|
|
|
)
|
2022-01-08 21:05:04 +00:00
|
|
|
|
2021-12-27 09:24:00 +00:00
|
|
|
// Request Object
|
2022-05-31 20:48:20 +00:00
|
|
|
const request = new Request('http://localhost/user/lookup/username/hey', { method: 'GET' })
|
2022-06-02 06:10:57 +00:00
|
|
|
|
|
|
|
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())
|
2022-01-08 21:05:04 +00:00
|
|
|
res = await worktopRouter.run(event)
|
|
|
|
console.log(await res.text())
|
2021-12-27 09:24:00 +00:00
|
|
|
}
|
|
|
|
fn()
|
|
|
|
|
2021-12-27 16:04:54 +00:00
|
|
|
const suite = new Benchmark.Suite()
|
|
|
|
|
2021-12-27 09:24:00 +00:00
|
|
|
suite
|
2022-09-20 13:46:13 +00:00
|
|
|
.add('Hono', async () => {
|
2021-12-27 16:04:54 +00:00
|
|
|
await hono.handleEvent(event)
|
2021-12-27 09:24:00 +00:00
|
|
|
})
|
2021-12-27 16:04:54 +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 () => {
|
2021-12-27 16:04:54 +00:00
|
|
|
await sunderApp.handle(event)
|
2021-12-27 09:24:00 +00:00
|
|
|
})
|
2022-01-08 21:05:04 +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 })
|