Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ jobs:
run: npm install

- name: benchmark
run: node benchmark/index.js
env:
RIMRAF_TEST_START_CHAR: a
RIMRAF_TEST_END_CHAR: f
RIMRAF_TEST_DEPTH: 5
run: node benchmark/index.js --start-char=a --end-char=f --depth=5
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
!/typedoc.json
!/tsconfig-*.json
!/.prettierignore
/benchmark-*.json
!eslint.config.mjs
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
/tap-snapshots
/.nyc_output
/coverage
/benchmark
/benchmark/old-rimraf
21 changes: 11 additions & 10 deletions benchmark/create-fixture.js
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)
}
136 changes: 125 additions & 11 deletions benchmark/index.js
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()
64 changes: 64 additions & 0 deletions benchmark/parse-results.js
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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return sqrt(variance(list))
return sqrt(v)

}
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
65 changes: 0 additions & 65 deletions benchmark/print-results.js

This file was deleted.

34 changes: 19 additions & 15 deletions benchmark/rimrafs.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// just disable the glob option, and promisify it, for apples-to-apples comp
import { promisify } from 'util'
import { createRequire } from 'module'
const oldRimraf = () => {
const {promisify} = require('util')
const oldRimraf = require('./old-rimraf')
const oldRimraf = createRequire(import.meta.filename)('./old-rimraf')
const pOldRimraf = promisify(oldRimraf)
const rimraf = path => pOldRimraf(path, { disableGlob: true })
const sync = path => oldRimraf.sync(path, { disableGlob: true })
return Object.assign(rimraf, { sync })
}

const { spawn, spawnSync } = require('child_process')
import { spawn, spawnSync } from 'child_process'
const systemRmRf = () => {
const rimraf = path => new Promise((res, rej) => {
const proc = spawn('rm', ['-rf', path])
proc.on('close', (code, signal) => {
if (code || signal)
rej(Object.assign(new Error('command failed'), { code, signal }))
else
res()
const rimraf = path =>
new Promise((res, rej) => {
const proc = spawn('rm', ['-rf', path])
proc.on('close', (code, signal) => {
if (code || signal)
rej(Object.assign(new Error('command failed'), { code, signal }))
else res()
})
})
})
rimraf.sync = path => {
const result = spawnSync('rm', ['-rf', path])
if (result.status || result.signal) {
Expand All @@ -31,10 +32,13 @@ const systemRmRf = () => {
return rimraf
}

module.exports = {
native: require('../').native,
posix: require('../').posix,
windows: require('../').windows,
import { native, posix, windows } from 'rimraf'
const cases = {
native,
posix,
windows,
old: oldRimraf(),
system: systemRmRf(),
}
export const names = new Set(Object.keys(cases))
export default cases
Loading