-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
143 lines (125 loc) · 3.74 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
const Benchmark = require('benchmark');
const chalk = require('chalk');
const runSuite = require('./helpers/runSuite');
const routes = [
['GET', '/', () => null],
['POST', '/user', () => null],
['GET', '/user', () => null],
['GET', '/user/:userID', () => null],
['GET', '/user/:userID/posts', () => null],
['GET', '/user/:userID/posts/:postID', () => null],
['GET', '/users/all', () => null],
['GET', '/an/unnecessarily/long/nested/route/here', () => null],
['GET', '/static/*', () => null],
['GET', '/sign-up', () => null],
['GET', '/login', () => null],
['GET', '/event/:id', () => null],
['GET', '/map/:location/events', () => null],
];
function getExpectedHandler(path, method = 'GET') {
return routes.find(
route => route[0] === method && route[1] === path
)[2];
}
const testRoutes = {
'short static': {
method: 'GET',
url: '/user',
expectedHandler: getExpectedHandler('/user'),
},
'short static POST': {
method: 'POST',
url: '/user',
expectedHandler: getExpectedHandler('/user', 'POST'),
},
'static with query string': {
method: 'GET',
url: '/user?foobar',
expectedHandler: getExpectedHandler('/user'),
},
'static with common prefix': {
method: 'GET',
url: '/users/all',
expectedHandler: getExpectedHandler('/users/all'),
},
'long static': {
method: 'GET',
url: '/an/unnecessarily/long/nested/route/here',
expectedHandler: getExpectedHandler('/an/unnecessarily/long/nested/route/here'),
},
'single parameter': {
method: 'GET',
url: '/user/123456789',
expectedHandler: getExpectedHandler('/user/:userID'),
},
'parameter in the middle': {
method: 'GET',
url: '/user/123456789/posts',
expectedHandler: getExpectedHandler('/user/:userID/posts'),
},
'multiple parameters': {
method: 'GET',
url: '/user/123456789/posts/my-very-first-post',
expectedHandler: getExpectedHandler('/user/:userID/posts/:postID'),
},
wildcard: {
method: 'GET',
url: '/static/favicon.ico',
expectedHandler: getExpectedHandler('/static/*'),
},
'route not found (404)': {
method: 'GET',
url: '/not-found',
expectedHandler: undefined,
},
};
const suites = {
'all routes': new Benchmark.Suite(),
};
for (const name in testRoutes) {
suites[name] = new Benchmark.Suite();
}
for (const router of require('./routers')) {
for (const [method, path, handler] of routes) {
router.registerRoute(method, path, handler);
}
const {findHandler} = router;
const allTests = [];
for (const testName in testRoutes) {
const {method, url, expectedHandler} = testRoutes[testName];
suites[testName].add(router.name, () => {
const req = {method, url};
const handler = findHandler(req);
if (handler !== expectedHandler) { // Validate that the router worked as expected
console.log(router.name, 'failed to look up the correct route:', req);
process.exit(1);
}
});
allTests.push({method, url});
}
suites['all routes'].add(router.name, () => {
for (let i = 0; i < allTests.length; i++) {
const test = allTests[i];
const req = {method: test.method, url: test.url};
findHandler(req);
}
});
}
for (const suiteName in suites) {
const suite = suites[suiteName];
suite
.on('start', () => {
const testRoute = testRoutes[suiteName];
if (testRoute === undefined) {
console.log(chalk.bold.yellow(suiteName));
return;
}
const route = routes.find(rt => rt[2] === testRoute.expectedHandler);
const path = route === undefined || testRoute.url.includes('?')
? testRoute.url
: route[1];
console.log(`${chalk.bold.yellow(suiteName)} - ${chalk.gray(path)}`);
});
runSuite(suite, ' ');
}