Skip to content
Myr

Add script for benchmark

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by Yusuke Wada · parent 7c8e49a · Browse files · 16 files+166 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

16 files+166 added−32 removed

  • other:+166 added−32 removed

16 files+166 added−32 removed

added:benchmark/hono/worker/script.js+1 added
1!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){const n=r(1)();n.get("/",()=>new Response("Root Page!")),n.get("/hello",()=>new Response("GET Hello!")),n.put("/hello",()=>new Response("PUT Hello!")),n.post("/hello",()=>new Response("POST Hello!")),n.delete("/hello",()=>new Response("DELETE Hello!")),n.get("/foo/bar",()=>new Response("GET Foo Bar!")),n.put("/foo/bar",()=>new Response("PUT Foo Bar!")),n.post("/foo/bar",()=>new Response("POST Foo Bar!")),n.delete("/foo/bar",()=>new Response("DELETE Foo Bar!")),n.get("/todos",()=>new Response("Todos Index!")),n.get("/todos/:id",e=>new Response("Todo #"+e.req.params("id"))),n.post("/todos",async e=>{const t=await e.req.json();return new Response("Creating Todo: "+JSON.stringify(t))}),n.all("*",()=>new Response("Not Found.",{status:404})),n.fire()},function(e,t,r){"use strict";const n=r(2),o=r(3),s=r(4);class a{constructor(){this.node=new n,this.tempPath="/"}add(e,t,...r){this.node.insert(e,t,r)}match(e,t){return e=e.toLowerCase(),this.node.search(e,t)}}const i={get:(e,t)=>(...r)=>e.constructor.prototype.hasOwnProperty(t)?e[t](...r):1==r.length?e.addRoute(t,e.router.tempPath,...r):e.addRoute(t,...r)};class l{constructor(){this.router=new a,this.middlewareRouter=new a,this.middleware=[]}addRoute(e,t,...r){return this.router.add(e,t,...r),u(this)}matchRoute(e,t){return this.router.match(e,t)}route(e){return this.router.tempPath=e,u(this)}use(e,t){t=[t];const r=this.middlewareRouter.match("all",e);r&&t.push(...r.handler),this.middlewareRouter.add("all",e,...t)}createContext(e,t){return{req:e,res:t}}async dispatch(e,t){const[r,n]=[e.method,(a=e.url,(a=new URL(a)).pathname)];var a;const i=this.matchRoute(r,n);if(!i)return this.notFound();e.params=e=>i.params[e];const l=[s],u=this.middlewareRouter.match("all",n);u&&l.push(...u.handler);let h=i.handler[0];l.push((e,t)=>{e.res=h(e),t()});const c=o(l),d=this.createContext(e,t);return c(d),d.res}async handleEvent(e){return await this.dispatch(e.request,new Response)}fire(){addEventListener("fetch",e=>{e.respondWith(this.handleEvent(e))})}notFound(){return new Response("Not Found",{status:404})}}const u=(e=new l)=>new Proxy(e,i);e.exports=u},function(e,t){class r{constructor({method:e,label:t,handler:r,children:n}={}){this.label=t||"",this.children=n||[],this.method={},e&&r&&(this.method[e]=r)}insert(e,t,n){let o=this;for(const e of this.splitPath(t)){let t=o.children[e];t?o=t:(o.children[e]=new r({label:e,handler:n}),o=o.children[e])}o.method[e]=n}splitPath(e){return["/",...e.split("/").filter(e=>""!==e)]}getPattern(e){const t=e.match(/^\:.+?\{(.+)\}$/);return t?"("+t[1]+")":"(.+)"}getParamName(e){const t=e.match(/^\:([^\{\}]+)/);if(t)return t[1]}search(e,t){let r,n=this,o={};if("/"===t){const t=this.children["/"];if(!t)return this.noRoute();const n=t.children["*"];if(n)r=n.method[e]||n.method.all;else if(!t.method[e])return this.noRoute()}for(const e of this.splitPath(t)){let t=n.children[e];if(t){n=t;continue}let r=!1;for(const t in n.children){if("*"===t){n=n.children[t],r=!0;break}if(t.match(/^:/)){const s=this.getPattern(t),a=e.match(new RegExp(s));if(a){o[this.getParamName(t)]=a[0],n=n.children[t],r=!0;break}return this.noRoute()}}if(0==r)return this.noRoute()}return r=r||n.method.all||n.method[e],r?(({handler:e,params:t}={})=>({handler:e,params:t}))({handler:r,params:o}):this.noRoute()}noRoute(){return null}}e.exports=r},function(e,t){e.exports=e=>function(t,r){let n=-1;return function o(s){if(s<=n)return Promise.reject(new Error("next() called multiple times"));n=s;let a=e[s];s===e.length&&(a=r);if(!a)return Promise.resolve();try{return Promise.resolve(a(t,o.bind(null,s+1)))}catch(e){return Promise.reject(e)}}(0)}},function(e,t){e.exports=(e,t)=>{e.req.query=t=>new URL(e.req.url).searchParams.get(t),t(),"string"==typeof e.res&&(e.res=new Response(e.res,{status:200,headers:{"Content-Type":"text/plain"}}))}}]);
added:benchmark/hono/.gitignore
added:benchmark/hono/index.js+27 added
added:benchmark/hono/package-lock.json+13 added
added:benchmark/hono/package.json+11 added
added:benchmark/hono/wrangler.toml+8 added
added:benchmark/itty-router/.gitignore+3 added
1dist2node_modules3worker
added:benchmark/itty-router/index.js+28 added
added:benchmark/itty-router/package-lock.json+28 added
1{2 "name": "itty-router",3 "version": "1.0.0",4 "lockfileVersion": 2,5 "requires": true,6 "packages": {7 "": {8 "name": "itty-router",9 "version": "1.0.0",10 "license": "ISC",11 "dependencies": {12 "itty-router": "^2.4.8"13 }14 },15 "node_modules/itty-router": {16 "version": "2.4.8",17 "resolved": "https://registry.npmjs.org/itty-router/-/itty-router-2.4.8.tgz",18 "integrity": "sha512-VpAHeAN+/ABLqT/zEnwMomNCDh8eRrs06C8HRR8OxjVwGhXtvzoJbggjUnjAeNGxqjV4w7pp87Iu8jzEpr92fg=="19 }20 },21 "dependencies": {22 "itty-router": {23 "version": "2.4.8",24 "resolved": "https://registry.npmjs.org/itty-router/-/itty-router-2.4.8.tgz",25 "integrity": "sha512-VpAHeAN+/ABLqT/zEnwMomNCDh8eRrs06C8HRR8OxjVwGhXtvzoJbggjUnjAeNGxqjV4w7pp87Iu8jzEpr92fg=="26 }27 }28}
added:benchmark/itty-router/package.json+14 added
1{2 "name": "itty-router-todos",3 "version": "0.0.1",4 "description": "",5 "main": "index.js",6 "scripts": {7 "test": "echo \"Error: no test specified\" && exit 1"8 },9 "author": "Yusuke Wada <yusuke@kamawada.com> (https://github.com/yusukebe)",10 "license": "MIT",11 "dependencies": {12 "itty-router": "^2.4.8"13 }14}
added:benchmark/itty-router/wrangler.toml+8 added
1name = "itty-router"2type = "webpack"3route = ''4zone_id = ''5usage_model = ''6compatibility_flags = []7workers_dev = true8compatibility_date = "2021-12-22"
added:benchmark/README.md+1 added
1# Sample applications for Benchmark
modified:src/middleware/filter.js+1 added-1 removed
1const filter = (c, next) => {2 const url = new URL(c.req.url)2 c.req.query = (key) => {3 const url = new URL(c.req.url)4 return url.searchParams.get(key)5 }67 next()
12 unmodified lines
modified:src/hono.js+10 added-10 removed
1'use strict'23const Node = require('./node')4const compose = require('./compose')5const filter = require('./middleware/filter')5const defaultFilter = require('./middleware/filter')67class Router {8 constructor() {9 this.node = new Node()
24 unmodified lines
34 }35 },36}3738const getPathFromURL = (url) => {39 url = new URL(url)40 return url.pathname41}4243class App {44 constructor() {45 this.router = new Router()46 this.middlewareRouter = new Router()
28 unmodified lines
75 return { req: req, res: res }76 }7778 async dispatch(request, response) {74 const url = new URL(request.url)75 const [method, path] = [request.method, url.pathname]79 const [method, path] = [request.method, getPathFromURL(request.url)]8081 const result = this.matchRoute(method, path)82 if (!result) return this.notFound()8384 request.params = (key) => result.params[key]8582 const middleware = [filter]86 const middleware = [defaultFilter]8788 const mwResult = this.middlewareRouter.match('all', path)89 if (mwResult) {90 middleware.push(...mwResult.handler)91 }9288 let handler89 for (const resultHandler of result.handler) {90 if (resultHandler) {91 handler = resultHandler92 }93 }93 let handler = result.handler[0] // XXX9495 let wrappedHandler = (context, next) => {96 context.res = handler(context)97 next()
31 unmodified lines
modified:src/node.js+11 added-20 removed
1const methodNameOfAll = 'all'1const METHOD_NAME_OF_ALL = 'all'23class Result {4 constructor({ handler, params } = {}) {5 this.handler = handler || []6 this.params = params || {}7 }3const createResult = ({ handler, params } = {}) => {4 return { handler: handler, params: params }5}67class Node {8 constructor({ method, label, handler, children } = {}) {
6 unmodified lines
15 }1617 insert(method, path, handler) {18 let curNode = this22 const ps = this.splitPath(path)23 for (const p of ps) {19 for (const p of this.splitPath(path)) {20 let nextNode = curNode.children[p]21 if (nextNode) {22 curNode = nextNode23 } else {
7 unmodified lines
31 curNode.method[method] = handler32 }3334 splitPath(path) {39 let ps = ['/']40 for (const p of path.split('/')) {41 if (p) {42 ps.push(p)43 }44 }45 return ps35 return ['/', ...path.split('/').filter((p) => p !== '')]36 }3738 getPattern(label) {39 // :id{[0-9]+} → [0-9]+$
13 unmodified lines
53 }5455 search(method, path) {56 let curNode = this67 let [handler, params] = [, {}]57 let handler58 let params = {}5960 if (path === '/') {61 const root = this.children['/']62 if (!root) return this.noRoute()63 // app.get('*', () => 'All')64 const rootAsterisk = root.children['*']65 if (rootAsterisk) {66 handler =76 rootAsterisk.method[method] || rootAsterisk.method[methodNameOfAll]67 rootAsterisk.method[method] || rootAsterisk.method[METHOD_NAME_OF_ALL]68 } else if (!root.method[method]) {69 return this.noRoute()70 }71 }
32 unmodified lines
104 }105 }106107 handler =117 handler || curNode.method[methodNameOfAll] || curNode.method[method]108 handler || curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]109110 if (!handler) {111 return this.noRoute()112 }122 const res = new Result({ handler: handler, params: params })123 return res113114 return createResult({ handler: handler, params: params })115 }116117 noRoute() {118 return null
4 unmodified lines
modified:.npmignore+2 added-1 removed
1example1example2benchmark