|
| 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