|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { now, print, operations } = require('./util') |
| 4 | +const KoaRouter = require('../lib/router') |
| 5 | + |
| 6 | +const router = new KoaRouter() |
| 7 | + |
| 8 | +const routes = [ |
| 9 | + { method: 'GET', url: '/user' }, |
| 10 | + { method: 'GET', url: '/user/comments' }, |
| 11 | + { method: 'GET', url: '/user/avatar' }, |
| 12 | + { method: 'GET', url: '/user/lookup/username/:username' }, |
| 13 | + { method: 'GET', url: '/user/lookup/email/:address' }, |
| 14 | + { method: 'GET', url: '/event/:id' }, |
| 15 | + { method: 'GET', url: '/event/:id/comments' }, |
| 16 | + { method: 'POST', url: '/event/:id/comment' }, |
| 17 | + { method: 'GET', url: '/map/:location/events' }, |
| 18 | + { method: 'GET', url: '/status' }, |
| 19 | + { method: 'GET', url: '/very/deeply/nested/route/hello/there' }, |
| 20 | + { method: 'GET', url: '/static/(.*)' } |
| 21 | +] |
| 22 | + |
| 23 | +function noop () {} |
| 24 | + |
| 25 | +var i = 0 |
| 26 | +var time = 0 |
| 27 | + |
| 28 | +routes.forEach(route => { |
| 29 | + if (route.method === 'GET') { |
| 30 | + router.get(route.url, noop) |
| 31 | + } else { |
| 32 | + router.post(route.url, noop) |
| 33 | + } |
| 34 | +}) |
| 35 | + |
| 36 | +time = now() |
| 37 | +for (i = 0; i < operations; i++) { |
| 38 | + router.match('/user', 'GET') |
| 39 | +} |
| 40 | +print('short static:', time) |
| 41 | + |
| 42 | +time = now() |
| 43 | +for (i = 0; i < operations; i++) { |
| 44 | + router.match('/user/comments', 'GET') |
| 45 | +} |
| 46 | +print('static with same radix:', time) |
| 47 | + |
| 48 | +time = now() |
| 49 | +for (i = 0; i < operations; i++) { |
| 50 | + router.match('/user/lookup/username/john', 'GET') |
| 51 | +} |
| 52 | +print('dynamic route:', time) |
| 53 | + |
| 54 | +time = now() |
| 55 | +for (i = 0; i < operations; i++) { |
| 56 | + router.match('/event/abcd1234/comments', 'GET') |
| 57 | +} |
| 58 | +print('mixed static dynamic:', time) |
| 59 | + |
| 60 | +time = now() |
| 61 | +for (i = 0; i < operations; i++) { |
| 62 | + router.match('/very/deeply/nested/route/hello/there', 'GET') |
| 63 | +} |
| 64 | +print('long static:', time) |
| 65 | + |
| 66 | +time = now() |
| 67 | +for (i = 0; i < operations; i++) { |
| 68 | + router.match('/static/index.html', 'GET') |
| 69 | +} |
| 70 | +print('wildcard:', time) |
| 71 | + |
| 72 | +time = now() |
| 73 | +for (i = 0; i < operations; i++) { |
| 74 | + router.match('/user', 'GET') |
| 75 | + router.match('/user/comments', 'GET') |
| 76 | + router.match('/user/lookup/username/john', 'GET') |
| 77 | + router.match('/event/abcd1234/comments', 'GET') |
| 78 | + router.match('/very/deeply/nested/route/hello/there', 'GET') |
| 79 | + router.match('/static/index.html', 'GET') |
| 80 | +} |
| 81 | +const output = print('all together:', time) |
| 82 | + |
| 83 | +require('fs').writeFileSync('bench-result.txt', String(output)) |
0 commit comments