Skip to content

Commit c86670e

Browse files
committed
benchmark scripts
1 parent 5e4bb5b commit c86670e

22 files changed

Lines changed: 7400320 additions & 0 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/docs
33
/coverage
44
/node_modules
5+
/benchmark/node_modules
6+
/benchmark/filenames.json
57
/.tap

‎benchmark/Makefile‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all: bench
2+
3+
bench: filenames.json patterns.json
4+
node index.js
5+
6+
patterns.json: gen-patterns.js patterns.txt
7+
node gen-patterns.js
8+
9+
filenames.json: gen-filenames.js
10+
node gen-filenames.js

‎benchmark/files.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const files = new Set()
2+
3+
const sets = [
4+
'abcd',
5+
'efgh',
6+
'ijlk',
7+
'mnop',
8+
'qrst',
9+
'uvwx',
10+
]
11+
12+
const explode = (head, tail) => {
13+
if (!tail.length) {
14+
files.add(head)
15+
return
16+
}
17+
18+
const chop = tail.slice(1)
19+
for (const i of tail[0]) {
20+
explode(head + i, chop)
21+
explode(head + '/' + i, chop)
22+
explode(head + '/.' + i, chop)
23+
}
24+
}
25+
26+
explode('', sets)

‎benchmark/gen-filenames.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const files = new Set()
2+
3+
const sets = 'abcdefg'
4+
5+
const explode = (head, tail) => {
6+
if (!tail.length) {
7+
files.add(head)
8+
return
9+
}
10+
11+
const chop = tail.slice(1)
12+
for (const i of tail) {
13+
try {
14+
explode(head + i, chop)
15+
const s = head === '' ? head : (head + '/')
16+
explode(s + i, chop)
17+
explode(s + '.' + i, chop)
18+
} catch (e) {
19+
return
20+
}
21+
}
22+
}
23+
24+
explode('', sets)
25+
26+
const fs = require('fs')
27+
fs.writeFileSync(__dirname + '/filenames.json', JSON.stringify([...files], 0, 2) + '\n')

‎benchmark/gen-patterns.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs')
2+
const patterns = fs.readFileSync(__dirname + '/patterns.txt', 'utf8')
3+
.trim()
4+
.replace(/[\n\r]+/g, '\n').split('\n')
5+
.map(p => p.trim())
6+
.filter(p => p && !/^#/.test(p))
7+
8+
fs.writeFileSync(__dirname + '/patterns.json', JSON.stringify(patterns, 0, 2) + '\n')

‎benchmark/gen-realistic-paths.mjs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Write a fixed list of real paths for realistic.mjs, so that the
2+
// results do not change when node_modules changes. The list is this
3+
// repo's tracked files, the files in node_modules, and the top two
4+
// levels of .git, for some dotfiles.
5+
import { execFileSync } from 'node:child_process'
6+
import { readdirSync, writeFileSync } from 'node:fs'
7+
import { resolve } from 'node:path'
8+
9+
const root = resolve(import.meta.dirname, '..')
10+
11+
const walk = (dir, depth = Infinity, out = []) => {
12+
for (const e of readdirSync(resolve(root, dir), { withFileTypes: true })) {
13+
const p = `${dir}/${e.name}`
14+
out.push(p)
15+
if (e.isDirectory() && !e.isSymbolicLink() && depth > 1) {
16+
walk(p, depth - 1, out)
17+
}
18+
}
19+
return out
20+
}
21+
22+
const tracked = execFileSync('git', ['ls-files'], { cwd: root })
23+
.toString()
24+
.trim()
25+
.split('\n')
26+
const paths = [
27+
...new Set([...tracked, ...walk('node_modules'), ...walk('.git', 2)]),
28+
].sort()
29+
30+
writeFileSync(
31+
resolve(import.meta.dirname, 'realistic-paths.json'),
32+
JSON.stringify(paths, null, 0) + '\n',
33+
)
34+
console.log(paths.length, 'paths')

‎benchmark/index.js‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
const which = process.argv[2]
2+
3+
const matchers = new Set([
4+
//'baseline',
5+
'minimatch-current',
6+
//'minimatch-3',
7+
//'minimatch-4',
8+
//'minimatch-5',
9+
// 'anymatch',
10+
// 'glob-to-regexp',
11+
'micromatch',
12+
'picomatch',
13+
])
14+
15+
const patterns = require('./patterns.json')
16+
const filenames = require('./filenames.json')
17+
const totalTests = patterns.length * filenames.length
18+
19+
const runAll = async () => {
20+
const promiseSpawn = require('@npmcli/promise-spawn')
21+
const promiseCallLimit = require('promise-call-limit')
22+
const limit = Math.max(require('os').cpus().length - 1, 1)
23+
const fns = []
24+
for (const which of matchers) {
25+
fns.push(async () => {
26+
console.error(`testing ${which}...`)
27+
const start = performance.now()
28+
const res = await promiseSpawn(process.execPath, [__filename, which], {
29+
stdioString: true,
30+
}, { matcher: which })
31+
const dur = performance.now() - start
32+
console.error(`finished ${which} (${dur}ms)`)
33+
return res
34+
})
35+
}
36+
const results = (await promiseCallLimit(fns, limit)).map(result => {
37+
let output
38+
try {
39+
output = JSON.parse(result.stdout)
40+
} catch (e) {
41+
output = e
42+
}
43+
if (result.stderr) {
44+
output.stderr = result.stderr
45+
}
46+
output.mps = totalTests / output.total
47+
return [result.matcher, output]
48+
}).sort(([_,{total:a}], [__,{total:b}]) => b - a)
49+
const max = Math.min(...results.map(([_, r]) => r.total))
50+
const mdt = require('markdown-tables')
51+
// todo: make a pretty graph
52+
for (const [name, res] of results) {
53+
console.log(`## ${name} (${round(max/res.total)} ${round(res.mps)}mps)`)
54+
if (res.stderr) {
55+
console.error(res.stderr.trim())
56+
}
57+
console.log(mdt(['pattern,setup,match,found'].concat(
58+
Object.entries(res).filter(([t,r]) => typeof r === 'object')
59+
.map(([pattern, {setup, match, found}]) =>
60+
[
61+
pattern.replace(/\,/g, '_'),
62+
round(setup), round(match), found].join(','))).join('\n')))
63+
}
64+
//console.log(require('util').inspect(results, { depth: Infinity }))
65+
}
66+
67+
const R = 100000
68+
const round = n => Math.floor(n * R)/R
69+
70+
const runTest = which => {
71+
const [[times, results], total] = time(() => runTest_(which))
72+
times.total = total
73+
console.log(JSON.stringify(times))
74+
75+
const logMatches = Object.entries(results).map(([pattern, res]) =>
76+
`${pattern}\n ${JSON.stringify(res, 0, 2).split('\n').join('\n ')}\n`).join('')
77+
const fs = require('fs')
78+
fs.writeFileSync(`${__dirname}/matches-${which}.json`, logMatches)
79+
}
80+
81+
const runTest_ = which => {
82+
const output = {}
83+
const n = 1000
84+
85+
const times = {}
86+
const results = {}
87+
for (const p of patterns) {
88+
const [matcher, setup] = time(() => getMatcher(which, p))
89+
if (!matcher) {
90+
continue
91+
}
92+
const [matched, match] = time(() => filenames.filter(f => matcher(f)))
93+
times[p] = {setup, match, found: matched.length}
94+
results[p] = matched
95+
}
96+
return [times, results]
97+
}
98+
99+
const getMatcher = (which, pattern) => {
100+
try {
101+
const mod = which === 'baseline' ? null : require(which)
102+
switch (which) {
103+
case 'baseline': {
104+
return s => false
105+
}
106+
case 'minimatch-3':
107+
case 'minimatch-4':
108+
case 'minimatch-5':
109+
case 'minimatch-current': {
110+
const m = new mod.Minimatch(pattern, {
111+
nonegate: true,
112+
optimizationLevel: 2,
113+
})
114+
return s => m.match(s)
115+
}
116+
case 'picomatch': {
117+
const m = mod(pattern)
118+
return s => m(s)
119+
}
120+
case 'anymatch': {
121+
return s => mod([pattern], s)
122+
}
123+
case 'glob-to-regexp': {
124+
const re = mod(pattern, { extended: true, globstar: true })
125+
return s => re.test(s)
126+
}
127+
case 'nanomatch':
128+
case 'micromatch': {
129+
const m = mod.matcher(pattern)
130+
return s => m(s)
131+
}
132+
default: {
133+
throw new Error(`unknown matcher ${which}`)
134+
}
135+
}
136+
} catch (er) {
137+
console.error(pattern, er.message)
138+
return null
139+
}
140+
}
141+
142+
const time = fn => {
143+
const start = performance.now()
144+
const res = fn()
145+
return [res, performance.now() - start]
146+
}
147+
148+
if (!which) {
149+
runAll()
150+
} else if (!matchers.has(which)) {
151+
console.error(`unknown matcher: ${which}`)
152+
process.exit(1)
153+
} else {
154+
runTest(which)
155+
}
156+

0 commit comments

Comments
 (0)