Skip to content

Commit

Permalink
Fix bench script (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 19, 2022
1 parent 09171d9 commit 93f83f3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 38 deletions.
99 changes: 63 additions & 36 deletions bench.js
@@ -1,62 +1,59 @@
/* global after, before, bench, suite */
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import Benchmark from 'benchmark';
import rimraf from 'rimraf';
import globbyMainBranch from 'globby';
import * as globbyMainBranch from 'globby';
import gs from 'glob-stream';
import fastGlob from 'fast-glob';
import {globby, globbySync} from './index.js';
import {globby, globbySync, globbyStream} from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const BENCH_DIR = 'bench';

const runners = [
{
name: 'globby async (working directory)',
run: async (patterns, callback) => {
await globby(patterns);
callback();
},
run: globby,
},
{
name: 'globby async (upstream/main)',
run: async (patterns, callback) => {
await globbyMainBranch(patterns);
callback();
},
run: globbyMainBranch.globby,
},
{
name: 'globby sync (working directory)',
run: patterns => {
globbySync(patterns);
},
run: globbySync,
},
{
name: 'globby sync (upstream/main)',
run: patterns => {
globbyMainBranch.sync(patterns);
},
run: globbyMainBranch.globbySync,
},
{
name: 'globby stream (working directory)',
run: patterns => new Promise(resolve => {
globbyStream(patterns).on('data', () => {}).on('end', resolve);
}),
},
{
name: 'globby stream (upstream/main)',
run: patterns => new Promise(resolve => {
globbyMainBranch.globbyStream(patterns).on('data', () => {}).on('end', resolve);
}),
},
{
name: 'glob-stream',
run: (patterns, cb) => {
gs(patterns).on('data', () => {}).on('end', cb);
},
run: patterns => new Promise(resolve => {
gs(patterns).on('data', () => {}).on('end', resolve);
}),
},
{
name: 'fast-glob async',
run: async (patterns, callback) => {
await fastGlob(patterns);
callback();
},
run: fastGlob,
},
{
name: 'fast-glob sync',
run: patterns => {
fastGlob.sync(patterns);
},
run: fastGlob.sync,
},
];

Expand Down Expand Up @@ -84,7 +81,7 @@ const benchs = [
},
];

before(() => {
const before = () => {
process.chdir(__dirname);
rimraf.sync(BENCH_DIR);
fs.mkdirSync(BENCH_DIR);
Expand All @@ -99,17 +96,47 @@ before(() => {
fs.writeFileSync(directory + (i < 100 ? 'c' : 'd') + i, '');
}
}
});
};

after(() => {
const after = () => {
process.chdir(__dirname);
rimraf.sync(BENCH_DIR);
});
};

for (const benchmark of benchs) {
suite(benchmark.name, () => {
for (const runner of runners) {
bench(runner.name, runner.run.bind(null, benchmark.patterns));
}
const suites = [];
for (const {name, patterns} of benchs) {
const suite = new Benchmark.Suite(name, {
onStart() {
before();

console.log(`[*] Started Benchmarks "${this.name}"`);
},
onCycle(event) {
console.log(`[+] ${String(event.target)}`);
},
onComplete() {
after();

console.log(`\nFastest is ${this.filter('fastest').map('name')} \n`);
},
});

for (const {name, run} of runners) {
suite.add(name, run.bind(undefined, patterns));
}

suites.push(suite);
}

let index = 0;
const run = suite => {
suite.on('complete', () => {
const next = suites[++index];
if (next) {
run(next);
}
});
suite.run({async: true});
};

run(suites[0]);
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -16,7 +16,7 @@
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"bench": "npm update glob-stream fast-glob && matcha bench.js",
"bench": "npm update globby glob-stream fast-glob && node bench.js",
"test": "xo && ava && tsd"
},
"files": [
Expand Down Expand Up @@ -68,10 +68,10 @@
"devDependencies": {
"@types/node": "^16.11.11",
"ava": "^3.15.0",
"benchmark": "2.1.4",
"get-stream": "^6.0.1",
"glob-stream": "^7.0.0",
"globby": "sindresorhus/globby#main",
"matcha": "^0.7.0",
"rimraf": "^3.0.2",
"tsd": "^0.19.0",
"typescript": "^4.5.2",
Expand Down

0 comments on commit 93f83f3

Please sign in to comment.