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

benchmark: convert to esm #319

Closed
wants to merge 2 commits into from
Closed
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
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)
}
12 changes: 6 additions & 6 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const cases = require('./rimrafs.js')
const runTest = require('./run-test.js')
const print = require('./print-results.js')
import cases from './rimrafs.js'
import runTest from './run-test.js'
import print from './print-results.js'

const rimraf = require('../')
import * as rimraf from '../dist/esm/index.js'
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')
rimraf.sync(import.meta.dirname + '/fixtures')
const results = {}
for (const name of Object.keys(cases)) {
results[name] = await runTest(name)
}
rimraf.sync(__dirname + '/fixtures')
rimraf.sync(import.meta.dirname + '/fixtures')
return results
}

Expand Down
10 changes: 5 additions & 5 deletions benchmark/print-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const variance = list => {
const stddev = list => {
const v = variance(list)
if (isNaN(v)) {
console.error({list, v})
console.error({ list, v })
throw new Error('wat?')
}
return sqrt(variance(list))
Expand All @@ -28,8 +28,8 @@ const nums = list => ({
})

const printEr = er => `${er.code ? er.code + ': ' : ''}${er.message}`
const failures = list => list.length === 0 ? {}
: { failures: list.map(er => printEr(er)).join('\n') }
const failures = list =>
list.length === 0 ? {} : { failures: list.map(er => printEr(er)).join('\n') }

const table = results => {
const table = {}
Expand All @@ -49,7 +49,7 @@ const table = results => {
}
// sort by mean time
return Object.entries(table)
.sort(([, {mean:a}], [, {mean:b}]) => a - b)
.sort(([, { mean: a }], [, { mean: b }]) => a - b)
.reduce((set, [key, val]) => {
set[key] = val
return set
Expand All @@ -62,4 +62,4 @@ const print = results => {
console.table(table(results))
}

module.exports = print
export default print
32 changes: 17 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,11 @@ const systemRmRf = () => {
return rimraf
}

module.exports = {
native: require('../').native,
posix: require('../').posix,
windows: require('../').windows,
import { native, posix, windows } from 'rimraf'
export default {
native,
posix,
windows,
old: oldRimraf(),
system: systemRmRf(),
}
35 changes: 20 additions & 15 deletions benchmark/run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ const END = process.env.RIMRAF_TEST_END_CHAR || 'f'
const DEPTH = +process.env.RIMRAF_TEST_DEPTH || 5
const N = +process.env.RIMRAF_TEST_ITERATIONS || 7

const cases = require('./rimrafs.js')
import cases from './rimrafs.js'

const create = require('./create-fixture.js')
import create from './create-fixture.js'

const hrToMS = hr => Math.round(hr[0]*1e9 + hr[1]) / 1e6
const hrToMS = hr => Math.round(hr[0] * 1e9 + hr[1]) / 1e6

const runTest = async (type) => {
const runTest = async type => {
const rimraf = cases[type]
if (!rimraf)
throw new Error('unknown rimraf type: ' + type)
if (!rimraf) throw new Error('unknown rimraf type: ' + type)

const opt = {
start: START,
Expand Down Expand Up @@ -62,10 +61,12 @@ const runTest = async (type) => {
const startAsync = process.hrtime()
for (const path of asyncPaths) {
const start = process.hrtime()
await rimraf(path).then(
() => asyncTimes.push(hrToMS(process.hrtime(start))),
er => asyncFails.push(er)
).then(() => process.stderr.write('.'))
await rimraf(path)
.then(
() => asyncTimes.push(hrToMS(process.hrtime(start))),
er => asyncFails.push(er),
)
.then(() => process.stderr.write('.'))
}
const asyncTotal = hrToMS(process.hrtime(startAsync))
console.error('done! (%j ms, %j failed)', asyncTotal, asyncFails.length)
Expand All @@ -77,10 +78,14 @@ const runTest = async (type) => {
const paraRuns = []
for (const path of paraPaths) {
const start = process.hrtime()
paraRuns.push(rimraf(path).then(
() => paraTimes.push(hrToMS(process.hrtime(start))),
er => paraFails.push(er)
).then(() => process.stderr.write('.')))
paraRuns.push(
rimraf(path)
.then(
() => paraTimes.push(hrToMS(process.hrtime(start))),
er => paraFails.push(er),
)
.then(() => process.stderr.write('.')),
)
}
await Promise.all(paraRuns)
const paraTotal = hrToMS(process.hrtime(startPara))
Expand All @@ -97,4 +102,4 @@ const runTest = async (type) => {
}))
}

module.exports = runTest
export default runTest