diff --git a/benchmarks/read-integration-mock.js b/benchmarks/read-integration-mock.js new file mode 100644 index 00000000..1fdd26ef --- /dev/null +++ b/benchmarks/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/read-integration-real.js b/benchmarks/read-integration-real.js new file mode 100644 index 00000000..05d8573d --- /dev/null +++ b/benchmarks/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/read-mock.js b/benchmarks/read-mock.js new file mode 100644 index 00000000..80d7ddf5 --- /dev/null +++ b/benchmarks/read-mock.js @@ -0,0 +1,35 @@ +var assert = require('assert'); +var fs = require('fs'); + +var mock = require('..'); + + +/** + * Test setup. Not timed. + */ +exports.beforeEach = function() { + mock({ + 'foo-mock.txt': 'foo' + }); +}; + + +/** + * 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. + */ +exports.afterEach = function() { + mock.restore(); +}; diff --git a/benchmarks/read-real.js b/benchmarks/read-real.js new file mode 100644 index 00000000..2b1067ae --- /dev/null +++ b/benchmarks/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/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); +}; diff --git a/package.json b/package.json index d2e96eca..1e9656d1 100644 --- a/package.json +++ b/package.json @@ -31,13 +31,16 @@ ], "scripts": { "debug": "node --debug-brk ./tasks.js test", - "test": "node ./tasks.js lint test" + "test": "node ./tasks.js lint test", + "bench": "bench benchmarks" }, "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", + "bench-it": "~0.1.0" }, "dependencies": { "rewire": "~2.0.0"