-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
45 lines (38 loc) · 1.17 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
var sfs = require('./')
, fs = require('fs');
/**
* Simple benchmark utility.
*
* @param {String} method Name of the method we want to execute.
* @param {String} path Path of the file we want to use.
* @param {Number} requests Amount of requests we want to execute.
* @param {Function} done Completion callback.
* @api public
*/
function bench(method, path, requests, done) {
function run(library, next) {
var synchronous = ~method.toLowerCase().indexOf('sync')
, name = library === sfs ? 'sfs(mine)' : 'fs(node)'
, now = Date.now()
, completed = 0
, i = 0;
function callback() { if (++completed === requests) {
console.log(name +'#'+ method +' took: '+ (Date.now() - now) +'ms');
next();
}}
for (; i < requests; i++) {
if (!synchronous) library[method](path, callback);
else callback(library[method](path));
}
}
return function runner() {
console.log('');
console.log('Starting benchmark: '+ method +', against path:'+ path);
console.log('');
run(fs, function next() {
run(sfs, done || function () {});
});
};
}
bench('exists', __dirname +'/bench.js-gone', 25000)();