-
-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor + chore cleanup #322
Open
lukekarrys
wants to merge
9
commits into
isaacs:main
Choose a base branch
from
lukekarrys:lk/refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
56cb4cb
benchmark: convert to esm
lukekarrys 768b1bc
benchmark: add options to filter and compare benchmarks
lukekarrys 9b3b6bf
Configure tap coverage-map
lukekarrys ec31e8b
Use tap.intercept to test bin
lukekarrys c40bef9
Mock process.platform with t.intercept
lukekarrys c1b5c88
Use the same ignoreENOENT and fixEPERM utils everywhere
lukekarrys 604153a
Do not pass unnecessary rest args to fs functions
lukekarrys ab09f16
Create separate dir for integration tests
lukekarrys 9e6fc9b
Add linting and update dependencies
lukekarrys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
!/typedoc.json | ||
!/tsconfig-*.json | ||
!/.prettierignore | ||
/benchmark-*.json | ||
!eslint.config.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
/tap-snapshots | ||
/.nyc_output | ||
/coverage | ||
/benchmark | ||
/benchmark/old-rimraf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
const { writeFile: writeFile_ } = require('fs') | ||
const writeFile = async (path, data) => new Promise((res, rej) => | ||
writeFile_(path, data, er => er ? rej(er) : res())) | ||
const { mkdirp } = require('mkdirp') | ||
const { resolve } = require('path') | ||
import { writeFile as writeFile_ } from 'fs' | ||
const writeFile = async (path, data) => | ||
new Promise((res, rej) => | ||
writeFile_(path, data, er => (er ? rej(er) : res())), | ||
) | ||
import { mkdirp } from 'mkdirp' | ||
import { resolve } from 'path' | ||
|
||
const create = async (path, start, end, maxDepth, depth = 0) => { | ||
await mkdirp(path) | ||
const promises = [] | ||
for (let i = start; i <= end; i++) { | ||
const c = String.fromCharCode(i) | ||
if (depth < maxDepth && (i-start >= depth)) | ||
if (depth < maxDepth && i - start >= depth) | ||
await create(resolve(path, c), start, end, maxDepth, depth + 1) | ||
else | ||
promises.push(writeFile(resolve(path, c), c)) | ||
else promises.push(writeFile(resolve(path, c), c)) | ||
} | ||
await Promise.all(promises) | ||
return path | ||
} | ||
|
||
module.exports = async ({ start, end, depth, name }) => { | ||
const path = resolve(__dirname, 'fixtures', name, 'test') | ||
export default async ({ start, end, depth, name }) => { | ||
const path = resolve(import.meta.dirname, 'fixtures', name, 'test') | ||
return await create(path, start.charCodeAt(0), end.charCodeAt(0), depth) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,132 @@ | ||
const cases = require('./rimrafs.js') | ||
const runTest = require('./run-test.js') | ||
const print = require('./print-results.js') | ||
import rimrafs, { names as rimrafNames } from './rimrafs.js' | ||
import runTest, { names as runTestNames } from './run-test.js' | ||
import parse from './parse-results.js' | ||
import { sync as rimrafSync } from '../dist/esm/index.js' | ||
import { parseArgs } from 'util' | ||
import assert from 'assert' | ||
import { readFileSync, writeFileSync } from 'fs' | ||
|
||
const parseOptions = () => { | ||
const { values } = parseArgs({ | ||
options: { | ||
cases: { | ||
type: 'string', | ||
short: 'c', | ||
multiple: true, | ||
}, | ||
'omit-cases': { | ||
type: 'string', | ||
short: 'o', | ||
multiple: true, | ||
}, | ||
'start-char': { | ||
type: 'string', | ||
default: 'a', | ||
}, | ||
'end-char': { | ||
type: 'string', | ||
default: 'f', | ||
}, | ||
depth: { | ||
type: 'string', | ||
default: '5', | ||
}, | ||
iterations: { | ||
type: 'string', | ||
default: '7', | ||
}, | ||
compare: { | ||
type: 'string', | ||
}, | ||
save: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
}) | ||
|
||
if (values.compare) { | ||
const { results, options } = JSON.parse( | ||
readFileSync(values.compare, 'utf8'), | ||
) | ||
return { | ||
...options, | ||
save: false, | ||
compare: results, | ||
} | ||
} | ||
|
||
const allNames = new Set([...rimrafNames, ...runTestNames]) | ||
const partition = (name, defaults = [new Set(), new Set()]) => { | ||
const options = values[name] ?? [] | ||
assert( | ||
options.every(c => allNames.has(c)), | ||
new TypeError(`invalid ${name}`, { | ||
cause: { | ||
found: options, | ||
wanted: [...allNames], | ||
}, | ||
}), | ||
) | ||
const found = options.reduce( | ||
(acc, k) => { | ||
acc[rimrafNames.has(k) ? 0 : 1].add(k) | ||
return acc | ||
}, | ||
[new Set(), new Set()], | ||
) | ||
return [ | ||
found[0].size ? found[0] : defaults[0], | ||
found[1].size ? found[1] : defaults[1], | ||
] | ||
} | ||
|
||
const cases = partition('cases', [rimrafNames, runTestNames]) | ||
for (const [i, omitCase] of Object.entries(partition('omit-cases'))) { | ||
for (const o of omitCase) { | ||
cases[i].delete(o) | ||
} | ||
} | ||
|
||
return { | ||
rimraf: [...cases[0]], | ||
runTest: [...cases[1]], | ||
start: values['start-char'], | ||
end: values['end-char'], | ||
depth: +values.depth, | ||
iterations: +values.iterations, | ||
save: values.save, | ||
compare: null, | ||
} | ||
} | ||
|
||
const rimraf = require('../') | ||
const main = async () => { | ||
// cleanup first. since the windows impl works on all platforms, | ||
// use that. it's only relevant if the folder exists anyway. | ||
rimraf.sync(__dirname + '/fixtures') | ||
const results = {} | ||
for (const name of Object.keys(cases)) { | ||
results[name] = await runTest(name) | ||
rimrafSync(import.meta.dirname + '/fixtures') | ||
const data = {} | ||
const { save, compare, ...options } = parseOptions() | ||
for (const [name, rimraf] of Object.entries(rimrafs)) { | ||
if (options.rimraf.includes(name)) { | ||
data[name] = await runTest(name, rimraf, options) | ||
} | ||
} | ||
rimrafSync(import.meta.dirname + '/fixtures') | ||
const { results, entries } = parse(data, compare) | ||
if (save) { | ||
const f = `benchmark-${Date.now()}.json` | ||
writeFileSync(f, JSON.stringify({ options, results }, 0, 2)) | ||
console.log(`results saved to ${f}`) | ||
} else { | ||
console.log(JSON.stringify(results, null, 2)) | ||
} | ||
rimraf.sync(__dirname + '/fixtures') | ||
return results | ||
console.table( | ||
entries | ||
.sort(([, { mean: a }], [, { mean: b }]) => a - b) | ||
.reduce((set, [key, val]) => { | ||
set[key] = val | ||
return set | ||
}, {}), | ||
) | ||
} | ||
|
||
main().then(print) | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const sum = list => list.reduce((a, b) => a + b) | ||
const mean = list => sum(list) / list.length | ||
const median = list => list.sort()[Math.floor(list.length / 2)] | ||
const max = list => list.sort()[list.length - 1] | ||
const min = list => list.sort()[0] | ||
const sqrt = n => Math.pow(n, 0.5) | ||
const variance = list => { | ||
const m = mean(list) | ||
return mean(list.map(n => Math.pow(n - m, 2))) | ||
} | ||
const stddev = list => { | ||
const v = variance(list) | ||
if (isNaN(v)) { | ||
throw new Error('wat?', { cause: { list, v } }) | ||
} | ||
return sqrt(variance(list)) | ||
} | ||
const comp = (v1, v2) => { | ||
if (v1 === undefined) { | ||
return {} | ||
} | ||
return { | ||
'old mean': v1.mean, | ||
'% +/-': round(((v2.mean - v1.mean) / v1.mean) * 100), | ||
} | ||
} | ||
|
||
const round = n => Math.round(n * 1e3) / 1e3 | ||
|
||
const nums = list => ({ | ||
mean: round(mean(list)), | ||
median: round(median(list)), | ||
stddev: round(stddev(list)), | ||
max: round(max(list)), | ||
min: round(min(list)), | ||
}) | ||
|
||
const printEr = er => `${er.code ? er.code + ': ' : ''}${er.message}` | ||
|
||
const parseResults = (data, compare) => { | ||
const results = {} | ||
const table = {} | ||
|
||
for (const [rimrafName, rimrafData] of Object.entries(data)) { | ||
results[rimrafName] = {} | ||
for (const [runTestName, { times, fails }] of Object.entries(rimrafData)) { | ||
const result = nums(times) | ||
const failures = fails.map(printEr) | ||
results[rimrafName][runTestName] = { ...result, times, failures } | ||
table[`${rimrafName} ${runTestName}`] = { | ||
...result, | ||
...comp(compare?.[rimrafName]?.[runTestName], result), | ||
...(failures.length ? { failures: failures.join('\n') } : {}), | ||
} | ||
} | ||
} | ||
|
||
return { | ||
results, | ||
entries: Object.entries(table), | ||
} | ||
} | ||
|
||
export default parseResults |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.