|
| 1 | +const httpNext = require('./index') |
| 2 | +const httpPrevious = require('0http') |
| 3 | + |
| 4 | +function getReqObject (url) { |
| 5 | + const req = {} |
| 6 | + req.method = 'GET' |
| 7 | + req.url = url |
| 8 | + |
| 9 | + return req |
| 10 | +} |
| 11 | + |
| 12 | +function getResObject () { |
| 13 | + const res = {} |
| 14 | + res.statusCode = null |
| 15 | + res.setHeader = () => {} |
| 16 | + res.writeHead = () => {} |
| 17 | + res.end = () => {} |
| 18 | + return res |
| 19 | +} |
| 20 | + |
| 21 | +function setupRouter (router) { |
| 22 | + router.use((req, res, next) => { |
| 23 | + return next() |
| 24 | + }) |
| 25 | + |
| 26 | + router.get('/', (req, res) => { |
| 27 | + return res.end('OK') |
| 28 | + }) |
| 29 | + router.get('/:id', (req, res) => { |
| 30 | + return res.end(req.params.id) |
| 31 | + }) |
| 32 | + router.get('/:id/error', () => { |
| 33 | + throw new Error('Error') |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +const { router } = httpNext({ |
| 38 | + cacheSize: 0, |
| 39 | + id: '/' |
| 40 | +}) |
| 41 | +setupRouter(router) |
| 42 | + |
| 43 | +const { router: routerPrevious } = httpPrevious({ |
| 44 | + cacheSize: 0 |
| 45 | +}) |
| 46 | +setupRouter(routerPrevious) |
| 47 | + |
| 48 | +import('mitata/src/cli.mjs').then(({ run, bench, group, baseline }) => { |
| 49 | + group('Next Router', () => { |
| 50 | + baseline('Base URL', () => { |
| 51 | + const req = getReqObject('/') |
| 52 | + const res = getResObject() |
| 53 | + |
| 54 | + router.lookup(req, res) |
| 55 | + }) |
| 56 | + bench('Parameter URL', () => { |
| 57 | + const req = getReqObject('/0') |
| 58 | + const res = getResObject() |
| 59 | + |
| 60 | + router.lookup(req, res) |
| 61 | + }) |
| 62 | + bench('Not Found URL', () => { |
| 63 | + const req = getReqObject('/0/404') |
| 64 | + const res = getResObject() |
| 65 | + |
| 66 | + router.lookup(req, res) |
| 67 | + }) |
| 68 | + bench('Error URL', () => { |
| 69 | + const req = getReqObject('/0/error') |
| 70 | + const res = getResObject() |
| 71 | + |
| 72 | + router.lookup(req, res) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + group('Previous Router', () => { |
| 77 | + baseline('Base URL', () => { |
| 78 | + const req = getReqObject('/') |
| 79 | + const res = getResObject() |
| 80 | + |
| 81 | + routerPrevious.lookup(req, res) |
| 82 | + }) |
| 83 | + bench('Parameter URL', () => { |
| 84 | + const req = getReqObject('/0') |
| 85 | + const res = getResObject() |
| 86 | + |
| 87 | + routerPrevious.lookup(req, res) |
| 88 | + }) |
| 89 | + bench('Not Found URL', () => { |
| 90 | + const req = getReqObject('/0/404') |
| 91 | + const res = getResObject() |
| 92 | + |
| 93 | + routerPrevious.lookup(req, res) |
| 94 | + }) |
| 95 | + bench('Error URL', () => { |
| 96 | + const req = getReqObject('/0/error') |
| 97 | + const res = getResObject() |
| 98 | + |
| 99 | + routerPrevious.lookup(req, res) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + run({ |
| 104 | + silent: false, |
| 105 | + avg: true, |
| 106 | + json: false, |
| 107 | + colors: true, |
| 108 | + min_max: false, |
| 109 | + percentiles: false |
| 110 | + }) |
| 111 | +}) |
0 commit comments