Permalink
Browse files

Beginnings of a performance testing suite.

Use `npm run benchmark`.
  • Loading branch information...
1 parent 681f6d2 commit 6eedcd9058833ee3695ada91181ed9ea5c5d1c28 @domenic domenic committed Jun 6, 2013
Showing with 110 additions and 1 deletion.
  1. +71 −0 benchmark/compare-with-callbacks.js
  2. +36 −0 benchmark/scenarios.js
  3. +3 −1 package.json
@@ -0,0 +1,71 @@
+"use strict";
+
+var Q = require("../q");
+var fs = require("fs");
+
+suite("A single simple async operation", function () {
+ bench("with an immediately-fulfilled promise", function (done) {
+ Q().then(done);
+ });
+
+ bench("with direct setImmediate usage", function (done) {
+ setImmediate(done);
+ });
+
+ bench("with direct setTimeout(…, 0)", function (done) {
+ setTimeout(done, 0);
+ });
+});
+
+suite("A fs.readFile", function () {
+ var denodeified = Q.denodeify(fs.readFile);
+
+ set("iterations", 1000);
+ set("delay", 1000);
+
+ bench("directly, with callbacks", function (done) {
+ fs.readFile(__filename, done);
+ });
+
+ bench("with Q.nfcall", function (done) {
+ Q.nfcall(fs.readFile, __filename).then(done);
+ });
+
+ bench("with a Q.denodeify'ed version", function (done) {
+ denodeified(__filename).then(done);
+ });
+
+ bench("with manual usage of deferred.makeNodeResolver", function (done) {
+ var deferred = Q.defer();
+ fs.readFile(__filename, deferred.makeNodeResolver());
+ deferred.promise.then(done);
+ });
+});
+
+suite("1000 operations in parallel", function () {
+ function makeCounter(desiredCount, ultimateCallback) {
+ var soFar = 0;
+ return function () {
+ if (++soFar === desiredCount) {
+ ultimateCallback();
+ }
+ };
+ }
+ var numberOfOps = 1000;
+
+ bench("with immediately-fulfilled promises", function (done) {
+ var counter = makeCounter(numberOfOps, done);
+
+ for (var i = 0; i < numberOfOps; ++i) {
+ Q().then(counter);
+ }
+ });
+
+ bench("with direct setImmediate usage", function (done) {
+ var counter = makeCounter(numberOfOps, done);
+
+ for (var i = 0; i < numberOfOps; ++i) {
+ setImmediate(counter);
+ }
+ });
+});
@@ -0,0 +1,36 @@
+"use strict";
+
+var Q = require("../q");
+
+suite("Chaining", function () {
+ var numberToChain = 1000;
+
+ bench("Chaining many already-fulfilled promises together", function (done) {
+ var currentPromise = Q();
+ for (var i = 0; i < numberToChain; ++i) {
+ currentPromise = currentPromise.then(function () {
+ return Q();
+ });
+ }
+
+ currentPromise.then(done);
+ });
+
+ bench("Chaining and then fulfilling the end of the chain", function (done) {
+ var deferred = Q.defer();
+
+ var currentPromise = deferred.promise;
+ for (var i = 0; i < numberToChain; ++i) {
+ (function () {
+ var promiseToReturn = currentPromise;
+ currentPromise = Q().then(function () {
+ return promiseToReturn;
+ });
+ }());
+ }
+
+ currentPromise.then(done);
+
+ deferred.resolve();
+ });
+});
View
@@ -51,11 +51,13 @@
"promises-aplus-tests": "1.x",
"grunt": "~0.4.1",
"grunt-cli": "~0.1.9",
- "grunt-contrib-uglify": "~0.2.2"
+ "grunt-contrib-uglify": "~0.2.2",
+ "matcha": "~0.2.0"
},
"scripts": {
"test": "jasmine-node spec && promises-aplus-tests spec/aplus-adapter",
"test-browser": "opener spec/q-spec.html",
+ "benchmark": "matcha",
"lint": "jshint q.js",
"cover": "cover run node_modules/jasmine-node/bin/jasmine-node spec && cover report html && opener cover_html/index.html",
"minify": "grunt",

0 comments on commit 6eedcd9

Please sign in to comment.