From c7ce49c800780be173eccf5c17042b2990c29d6e Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 20 Jan 2014 17:43:18 -0700 Subject: [PATCH 1/4] Basic benchmarks --- benchmarks/run.js | 79 +++++++++++++++++++++++ benchmarks/tests/read-integration-mock.js | 25 +++++++ benchmarks/tests/read-integration-real.js | 28 ++++++++ benchmarks/tests/read-mock.js | 39 +++++++++++ benchmarks/tests/read-real.js | 43 ++++++++++++ package.json | 6 +- 6 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 benchmarks/run.js create mode 100644 benchmarks/tests/read-integration-mock.js create mode 100644 benchmarks/tests/read-integration-real.js create mode 100644 benchmarks/tests/read-mock.js create mode 100644 benchmarks/tests/read-real.js diff --git a/benchmarks/run.js b/benchmarks/run.js new file mode 100644 index 00000000..880a865b --- /dev/null +++ b/benchmarks/run.js @@ -0,0 +1,79 @@ +var fs = require('fs'); +var path = require('path'); + +function noop(done) { + process.nextTick(done); +} + + +function measure(bench, duration, callback) { + var beforeEach = bench.beforeEach || noop; + var afterEach = bench.afterEach || noop; + + var cumulative = 0; + + function iterate(completed) { + if (cumulative / 1e9 > duration) { + return callback(null, completed, cumulative); + } + beforeEach(function(beforeEachErr) { + if (beforeEachErr) { + return callback(beforeEachErr); + } + try { + var time = process.hrtime(); + bench.test(function(testErr) { + var diff = process.hrtime(time); + cumulative += diff[0] * 1e9 + diff[1]; + ++completed; + if (testErr) { + return callback(testErr); + } + afterEach(function(afterEachErr) { + if (afterEachErr) { + return callback(afterEachErr); + } + setImmediate(function() { + iterate(completed); + }); + }); + }); + } catch (testErr) { + return callback(testErr); + } + }); + } + iterate(0); +} + +function run(paths, num) { + var name = paths[num]; + if (!name) { + process.exit(0); + } + var bench = require(name); + measure(bench, 1, function(err, completed, cumulative) { + if (err) { + console.error(err); + process.exit(1); + } + console.log('%s %d ops/sec', + path.basename(name), Math.floor(completed / (cumulative / 1e9))); + ++num; + run(paths, num); + }); +} + +// run all .js in the tests dir +fs.readdir(path.join(__dirname, 'tests'), function(err, files) { + if (err) { + console.error(err); + process.exit(1); + } + var paths = files.filter(function(name) { + return /\.js$/.test(name); + }).map(function(name) { + return path.join(__dirname, 'tests', name); + }); + run(paths, 0); +}); diff --git a/benchmarks/tests/read-integration-mock.js b/benchmarks/tests/read-integration-mock.js new file mode 100644 index 00000000..ddbffcc9 --- /dev/null +++ b/benchmarks/tests/read-integration-mock.js @@ -0,0 +1,25 @@ +var assert = require('assert'); +var fs = require('fs'); + +var mock = require('../..'); + + +/** + * Timed test. This includes the mock setup and teardown as part of the overall + * test time. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + mock({ + 'foo-mock.txt': 'foo' + }); + + fs.readFile('foo-mock.txt', 'utf8', function(err, str) { + assert.ifError(err); + assert.equal(str, 'foo'); + + mock.restore(); + done(); + }); + +}; diff --git a/benchmarks/tests/read-integration-real.js b/benchmarks/tests/read-integration-real.js new file mode 100644 index 00000000..05d8573d --- /dev/null +++ b/benchmarks/tests/read-integration-real.js @@ -0,0 +1,28 @@ +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); + +var rimraf = require('rimraf'); + +var tmpPath = '.tmp'; + + +/** + * Timed test. This includes the setup and teardown as part of the overall + * test time. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.mkdir(tmpPath, function(mkdirErr) { + assert.ifError(mkdirErr); + var tmpFile = path.join(tmpPath, 'foo-real.txt'); + fs.writeFile(tmpFile, 'foo', function(writeErr) { + assert.ifError(writeErr); + fs.readFile(tmpFile, 'utf8', function(readErr, str) { + assert.ifError(readErr); + assert.equal(str, 'foo'); + rimraf(tmpPath, done); + }); + }); + }); +}; diff --git a/benchmarks/tests/read-mock.js b/benchmarks/tests/read-mock.js new file mode 100644 index 00000000..83dd98b9 --- /dev/null +++ b/benchmarks/tests/read-mock.js @@ -0,0 +1,39 @@ +var assert = require('assert'); +var fs = require('fs'); + +var mock = require('../..'); + + +/** + * Test setup. Not timed. + * @param {function(Error)} done Callback. + */ +exports.beforeEach = function(done) { + mock({ + 'foo-mock.txt': 'foo' + }); + done(); +}; + + +/** + * Timed test. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.readFile('foo-mock.txt', 'utf8', function(err, str) { + assert.ifError(err); + assert.equal(str, 'foo'); + done(); + }); +}; + + +/** + * Test teardown. Not timed. + * @param {function(Error)} done Callback. + */ +exports.afterEach = function(done) { + mock.restore(); + done(); +}; diff --git a/benchmarks/tests/read-real.js b/benchmarks/tests/read-real.js new file mode 100644 index 00000000..2b1067ae --- /dev/null +++ b/benchmarks/tests/read-real.js @@ -0,0 +1,43 @@ +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); + +var rimraf = require('rimraf'); + +var tmpPath = '.tmp'; + + +/** + * Test setup. Not timed. + * @param {function(Error)} done Callback. + */ +exports.beforeEach = function(done) { + fs.mkdir(tmpPath, function(err) { + if (err) { + return done(err); + } + fs.writeFile(path.join(tmpPath, 'foo-real.txt'), 'foo', done); + }); +}; + + +/** + * Timed test. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.readFile(path.join(tmpPath, 'foo-real.txt'), 'utf8', function(err, str) { + assert.ifError(err); + assert.equal(str, 'foo'); + done(); + }); +}; + + +/** + * Test teardown. Not timed. + * @param {function(Error)} done Callback. + */ +exports.afterEach = function(done) { + rimraf(tmpPath, done); +}; diff --git a/package.json b/package.json index d2e96eca..38f3dd0c 100644 --- a/package.json +++ b/package.json @@ -31,13 +31,15 @@ ], "scripts": { "debug": "node --debug-brk ./tasks.js test", - "test": "node ./tasks.js lint test" + "test": "node ./tasks.js lint test", + "bench": "node benchmarks/run.js" }, "devDependencies": { "chai": "~1.8.1", "jshint": "~2.4.1", "mocha": "~1.17.0", - "glob": "~3.2.8" + "glob": "~3.2.8", + "rimraf": "~2.2.6" }, "dependencies": { "rewire": "~2.0.0" From 446114ca54f9fcf133989df9a364eeb6d124d831 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 1 Feb 2014 21:23:04 -0700 Subject: [PATCH 2/4] Use bench-it --- .../{tests => }/read-integration-mock.js | 2 +- .../{tests => }/read-integration-real.js | 0 benchmarks/{tests => }/read-mock.js | 2 +- benchmarks/{tests => }/read-real.js | 0 benchmarks/run.js | 79 ------------------- package.json | 5 +- 6 files changed, 5 insertions(+), 83 deletions(-) rename benchmarks/{tests => }/read-integration-mock.js (93%) rename benchmarks/{tests => }/read-integration-real.js (100%) rename benchmarks/{tests => }/read-mock.js (95%) rename benchmarks/{tests => }/read-real.js (100%) delete mode 100644 benchmarks/run.js diff --git a/benchmarks/tests/read-integration-mock.js b/benchmarks/read-integration-mock.js similarity index 93% rename from benchmarks/tests/read-integration-mock.js rename to benchmarks/read-integration-mock.js index ddbffcc9..1fdd26ef 100644 --- a/benchmarks/tests/read-integration-mock.js +++ b/benchmarks/read-integration-mock.js @@ -1,7 +1,7 @@ var assert = require('assert'); var fs = require('fs'); -var mock = require('../..'); +var mock = require('..'); /** diff --git a/benchmarks/tests/read-integration-real.js b/benchmarks/read-integration-real.js similarity index 100% rename from benchmarks/tests/read-integration-real.js rename to benchmarks/read-integration-real.js diff --git a/benchmarks/tests/read-mock.js b/benchmarks/read-mock.js similarity index 95% rename from benchmarks/tests/read-mock.js rename to benchmarks/read-mock.js index 83dd98b9..54ebb6bd 100644 --- a/benchmarks/tests/read-mock.js +++ b/benchmarks/read-mock.js @@ -1,7 +1,7 @@ var assert = require('assert'); var fs = require('fs'); -var mock = require('../..'); +var mock = require('..'); /** diff --git a/benchmarks/tests/read-real.js b/benchmarks/read-real.js similarity index 100% rename from benchmarks/tests/read-real.js rename to benchmarks/read-real.js diff --git a/benchmarks/run.js b/benchmarks/run.js deleted file mode 100644 index 880a865b..00000000 --- a/benchmarks/run.js +++ /dev/null @@ -1,79 +0,0 @@ -var fs = require('fs'); -var path = require('path'); - -function noop(done) { - process.nextTick(done); -} - - -function measure(bench, duration, callback) { - var beforeEach = bench.beforeEach || noop; - var afterEach = bench.afterEach || noop; - - var cumulative = 0; - - function iterate(completed) { - if (cumulative / 1e9 > duration) { - return callback(null, completed, cumulative); - } - beforeEach(function(beforeEachErr) { - if (beforeEachErr) { - return callback(beforeEachErr); - } - try { - var time = process.hrtime(); - bench.test(function(testErr) { - var diff = process.hrtime(time); - cumulative += diff[0] * 1e9 + diff[1]; - ++completed; - if (testErr) { - return callback(testErr); - } - afterEach(function(afterEachErr) { - if (afterEachErr) { - return callback(afterEachErr); - } - setImmediate(function() { - iterate(completed); - }); - }); - }); - } catch (testErr) { - return callback(testErr); - } - }); - } - iterate(0); -} - -function run(paths, num) { - var name = paths[num]; - if (!name) { - process.exit(0); - } - var bench = require(name); - measure(bench, 1, function(err, completed, cumulative) { - if (err) { - console.error(err); - process.exit(1); - } - console.log('%s %d ops/sec', - path.basename(name), Math.floor(completed / (cumulative / 1e9))); - ++num; - run(paths, num); - }); -} - -// run all .js in the tests dir -fs.readdir(path.join(__dirname, 'tests'), function(err, files) { - if (err) { - console.error(err); - process.exit(1); - } - var paths = files.filter(function(name) { - return /\.js$/.test(name); - }).map(function(name) { - return path.join(__dirname, 'tests', name); - }); - run(paths, 0); -}); diff --git a/package.json b/package.json index 38f3dd0c..1e9656d1 100644 --- a/package.json +++ b/package.json @@ -32,14 +32,15 @@ "scripts": { "debug": "node --debug-brk ./tasks.js test", "test": "node ./tasks.js lint test", - "bench": "node benchmarks/run.js" + "bench": "bench benchmarks" }, "devDependencies": { "chai": "~1.8.1", "jshint": "~2.4.1", "mocha": "~1.17.0", "glob": "~3.2.8", - "rimraf": "~2.2.6" + "rimraf": "~2.2.6", + "bench-it": "~0.1.0" }, "dependencies": { "rewire": "~2.0.0" From 615dfe2a72235f40c61ab29e8bea946550651df9 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 1 Feb 2014 21:25:39 -0700 Subject: [PATCH 3/4] Sync setup/teardown --- benchmarks/read-mock.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/benchmarks/read-mock.js b/benchmarks/read-mock.js index 54ebb6bd..80d7ddf5 100644 --- a/benchmarks/read-mock.js +++ b/benchmarks/read-mock.js @@ -6,13 +6,11 @@ var mock = require('..'); /** * Test setup. Not timed. - * @param {function(Error)} done Callback. */ -exports.beforeEach = function(done) { +exports.beforeEach = function() { mock({ 'foo-mock.txt': 'foo' }); - done(); }; @@ -31,9 +29,7 @@ exports.test = function(done) { /** * Test teardown. Not timed. - * @param {function(Error)} done Callback. */ -exports.afterEach = function(done) { +exports.afterEach = function() { mock.restore(); - done(); }; From 33f7d9511e80eb5ddd85dd9bf6ed4188f9b3ea00 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 3 Feb 2014 22:06:02 -0700 Subject: [PATCH 4/4] Some more benchmarks --- benchmarks/write-integration-mock.js | 22 +++++++++++++++++++ benchmarks/write-integration-real.js | 23 +++++++++++++++++++ benchmarks/write-mock.js | 29 ++++++++++++++++++++++++ benchmarks/write-real.js | 33 ++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 benchmarks/write-integration-mock.js create mode 100644 benchmarks/write-integration-real.js create mode 100644 benchmarks/write-mock.js create mode 100644 benchmarks/write-real.js diff --git a/benchmarks/write-integration-mock.js b/benchmarks/write-integration-mock.js new file mode 100644 index 00000000..0505c5ed --- /dev/null +++ b/benchmarks/write-integration-mock.js @@ -0,0 +1,22 @@ +var assert = require('assert'); +var fs = require('fs'); + +var mock = require('..'); + + +/** + * Timed test. This includes the mock setup and teardown as part of the overall + * test time. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + mock(); + + fs.writeFile('foo-mock.txt', 'foo', function(err) { + assert.ifError(err); + + mock.restore(); + done(); + }); + +}; diff --git a/benchmarks/write-integration-real.js b/benchmarks/write-integration-real.js new file mode 100644 index 00000000..f36f4b45 --- /dev/null +++ b/benchmarks/write-integration-real.js @@ -0,0 +1,23 @@ +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); + +var rimraf = require('rimraf'); + +var tmpPath = '.tmp'; + + +/** + * Timed test. This includes the setup and teardown as part of the overall + * test time. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.mkdir(tmpPath, function(mkdirErr) { + assert.ifError(mkdirErr); + fs.writeFile(path.join(tmpPath, 'foo-real.txt'), 'foo', function(err) { + assert.ifError(err); + rimraf(tmpPath, done); + }); + }); +}; diff --git a/benchmarks/write-mock.js b/benchmarks/write-mock.js new file mode 100644 index 00000000..c7838389 --- /dev/null +++ b/benchmarks/write-mock.js @@ -0,0 +1,29 @@ +var fs = require('fs'); +var path = require('path'); + +var mock = require('..'); + + +/** + * Test setup. Not timed. + */ +exports.beforeEach = function() { + mock(); +}; + + +/** + * Timed test. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.writeFile('foo-mock.txt', 'foo', done); +}; + + +/** + * Test teardown. Not timed. + */ +exports.afterEach = function() { + mock.restore(); +}; diff --git a/benchmarks/write-real.js b/benchmarks/write-real.js new file mode 100644 index 00000000..68534ff2 --- /dev/null +++ b/benchmarks/write-real.js @@ -0,0 +1,33 @@ +var fs = require('fs'); +var path = require('path'); + +var rimraf = require('rimraf'); + +var tmpPath = '.tmp'; + + +/** + * Test setup. Not timed. + * @param {function(Error)} done Callback. + */ +exports.beforeEach = function(done) { + fs.mkdir(tmpPath, done); +}; + + +/** + * Timed test. + * @param {function(Error)} done Callback. + */ +exports.test = function(done) { + fs.writeFile(path.join(tmpPath, 'foo-real.txt'), 'foo', done); +}; + + +/** + * Test teardown. Not timed. + * @param {function(Error)} done Callback. + */ +exports.afterEach = function(done) { + rimraf(tmpPath, done); +};