Skip to content

Commit 697d508

Browse files
authored
Merge branch 'master' into upgrade-to-eslint9
2 parents 7146a7d + 82d832b commit 697d508

File tree

6 files changed

+198
-5
lines changed

6 files changed

+198
-5
lines changed

.github/workflows/ci.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,59 @@ jobs:
2424
run: npm install
2525
- name: Run tests
2626
run: npm run test
27+
28+
benchmarks:
29+
runs-on: ubuntu-latest
30+
env:
31+
THRESHOLD: 50000
32+
steps:
33+
- name: Setup node
34+
uses: actions/setup-node@v3
35+
with:
36+
node-version: 20
37+
# First checkout master and run benchmarks
38+
- name: Checkout master branch
39+
uses: actions/checkout@v3
40+
with:
41+
ref: master
42+
43+
- name: Install dependencies
44+
run: npm install
45+
- name: Run benchmarks
46+
run: npm run benchmark || echo "0" > bench-result.txt
47+
# Store output of bench-result.txt to workflow output
48+
- name: Store benchmark result
49+
id: main_benchmark
50+
run: |
51+
echo "result=$(cat bench-result.txt)" >> $GITHUB_OUTPUT
52+
53+
# Now checkout the PR branch and run benchmarks
54+
- name: Checkout PR branch
55+
uses: actions/checkout@v3
56+
57+
- name: Install dependencies
58+
run: npm install
59+
- name: Run benchmarks
60+
run: npm run benchmark
61+
# Store output of bench-result.txt to workflow output
62+
- name: Store benchmark result
63+
id: branch_benchmark
64+
run: |
65+
echo "result=$(cat bench-result.txt)" >> $GITHUB_OUTPUT
66+
67+
# Verify difference between main and branch benchmark outputs aren't greater than THRESHOLD env var
68+
- name: Verify benchmark results
69+
run: |
70+
main_result=${{ steps.main_benchmark.outputs.result }}
71+
branch_result=${{ steps.branch_benchmark.outputs.result }}
72+
difference=$(echo "$main_result - $branch_result" | bc)
73+
echo "Main benchmark result: $main_result"
74+
echo "Branch benchmark result: $branch_result"
75+
echo "Difference: $difference"
76+
if (( $(echo "$difference > $THRESHOLD" | bc -l) )); then
77+
echo "Benchmark difference exceeds threshold of $THRESHOLD"
78+
exit 1
79+
else
80+
echo "Benchmark difference is within acceptable limit (< $THRESHOLD)."
81+
fi
82+

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Thumbs.db
1212
tmp/
1313
temp/
1414
*.lcov
15-
.env
15+
.env
16+
bench-result.txt

bench/run.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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))

bench/util.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
const chalk = require('chalk').default
4+
5+
const operations = 1000000
6+
7+
function now () {
8+
var ts = process.hrtime()
9+
return (ts[0] * 1e3) + (ts[1] / 1e6)
10+
}
11+
12+
function getOpsSec (ms) {
13+
return Number(((operations * 1000) / ms).toFixed())
14+
}
15+
16+
function print (name, time) {
17+
const opsSec = getOpsSec(now() - time)
18+
console.log(chalk.yellow(name), opsSec.toLocaleString(), 'ops/sec')
19+
return Number(opsSec)
20+
}
21+
22+
function title (name) {
23+
console.log(chalk.green(`
24+
${'='.repeat(name.length + 2)}
25+
${name}
26+
${'='.repeat(name.length + 2)}`))
27+
}
28+
29+
function Queue () {
30+
this.q = []
31+
this.running = false
32+
}
33+
34+
Queue.prototype.add = function add (job) {
35+
this.q.push(job)
36+
if (!this.running) this.run()
37+
}
38+
39+
Queue.prototype.run = function run () {
40+
this.running = true
41+
const job = this.q.shift()
42+
job(() => {
43+
if (this.q.length) {
44+
this.run()
45+
} else {
46+
this.running = false
47+
}
48+
})
49+
}
50+
51+
module.exports = { now, getOpsSec, print, title, Queue, operations }

lib/router.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* @link https://github.com/alexmingoia/koa-router
66
*/
77
const http = require('node:http');
8-
const util = require('node:util');
98

10-
const debug = util.debuglog('koa-router');
9+
const debug = require('debug')('koa-router');
1110

1211
const compose = require('koa-compose');
1312
const HttpError = require('http-errors');

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@koa/router",
33
"description": "Router middleware for koa. Maintained by Forward Email and Lad.",
4-
"version": "13.1.0",
4+
"version": "13.1.1",
55
"author": "Alex Mingoia <[email protected]>",
66
"bugs": {
77
"url": "https://github.com/koajs/router/issues",
@@ -21,6 +21,7 @@
2121
}
2222
],
2323
"dependencies": {
24+
"debug": "^4.4.1",
2425
"http-errors": "^2.0.0",
2526
"koa-compose": "^4.1.0",
2627
"path-to-regexp": "^6.3.0"
@@ -29,6 +30,7 @@
2930
"@commitlint/cli": "^17.7.2",
3031
"@commitlint/config-conventional": "^17.7.0",
3132
"@ladjs/env": "^4.0.0",
33+
"chalk": "^5.4.1",
3234
"eslint": "^9.10.0",
3335
"eslint-config-xo-lass": "^2.0.1",
3436
"eslint-plugin-unicorn": "55.0.0",
@@ -64,6 +66,7 @@
6466
},
6567
"scripts": {
6668
"bench": "make -C bench",
69+
"benchmark": "node bench/run.js",
6770
"coverage": "nyc npm run test",
6871
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
6972
"lint": "eslint . && remark . -qfo && fixpack",
@@ -72,4 +75,4 @@
7275
"test": "mocha test/**/*.js",
7376
"test:watch": "mocha test/**/*.js --watch"
7477
}
75-
}
78+
}

0 commit comments

Comments
 (0)