Skip to content

Commit

Permalink
add then-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf authored and petkaantonov committed May 5, 2014
1 parent 6ae5c25 commit f187d26
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 4 deletions.
65 changes: 65 additions & 0 deletions benchmark/doxbee-sequential-errors/promises-then-promise.js
@@ -0,0 +1,65 @@
global.useThenPromise = true;

var promise = require("promise");

require('../lib/fakesP');


module.exports = function upload(stream, idOrPath, tag, done) {
var blob = blobManager.create(account);
var tx = db.begin();
var blobIdP = blob.put(stream);
var fileP = self.byUuidOrPath(idOrPath).get();
var version, fileId, file;
promise.all([blobIdP, fileP]).then(function(all) {
var blobId = all[0], fileV = all[1];
file = fileV;
var previousId = file ? file.version : null;
version = {
userAccountId: userAccount.id,
date: new Date(),
blobId: blobId,
creatorId: userAccount.id,
previousId: previousId,
};
version.id = Version.createHash(version);
return Version.insert(version).execWithin(tx);
}).then(function() {
triggerIntentionalError();
if (!file) {
var splitPath = idOrPath.split('/');
var fileName = splitPath[splitPath.length - 1];
var newId = uuid.v1();
return self.createQueryCtxless(idOrPath, {
id: newId,
userAccountId: userAccount.id,
name: fileName,
version: version.id
}).then(function(q) {
return q.execWithin(tx);
}).then(function() {
return newId;
});
} else {
return file.id;
}
}).then(function(fileIdV) {
triggerIntentionalError();
fileId = fileIdV;
return FileVersion.insert({
fileId: fileId,
versionId: version.id
}).execWithin(tx);
}).then(function() {
triggerIntentionalError();
return File.whereUpdate({id: fileId}, {version: version.id})
.execWithin(tx);
}).then(function() {
triggerIntentionalError();
tx.commit();
return done();
}).then(null, function(err) {
tx.rollback();
return done(err);
});
}
61 changes: 61 additions & 0 deletions benchmark/doxbee-sequential/promises-then-promise.js
@@ -0,0 +1,61 @@
global.useThenPromise = true;

var promise = require("promise");

require('../lib/fakesP');


module.exports = function upload(stream, idOrPath, tag, done) {
var blob = blobManager.create(account);
var tx = db.begin();
var blobIdP = blob.put(stream);
var fileP = self.byUuidOrPath(idOrPath).get();
var version, fileId, file;
promise.all([blobIdP, fileP]).then(function(all) {
var blobId = all[0], fileV = all[1];
file = fileV;
var previousId = file ? file.version : null;
version = {
userAccountId: userAccount.id,
date: new Date(),
blobId: blobId,
creatorId: userAccount.id,
previousId: previousId,
};
version.id = Version.createHash(version);
return Version.insert(version).execWithin(tx);
}).then(function() {
if (!file) {
var splitPath = idOrPath.split('/');
var fileName = splitPath[splitPath.length - 1];
var newId = uuid.v1();
return self.createQueryCtxless(idOrPath, {
id: newId,
userAccountId: userAccount.id,
name: fileName,
version: version.id
}).then(function(q) {
return q.execWithin(tx);
}).then(function() {
return newId;
});
} else {
return file.id;
}
}).then(function(fileIdV) {
fileId = fileIdV;
return FileVersion.insert({
fileId: fileId,
versionId: version.id
}).execWithin(tx);
}).then(function() {
return File.whereUpdate({id: fileId}, {version: version.id})
.execWithin(tx);
}).then(function() {
tx.commit();
return done();
}, function(err) {
tx.rollback();
return done(err);
});
}
38 changes: 36 additions & 2 deletions benchmark/lib/fakesP.js
Expand Up @@ -25,8 +25,42 @@ else if (global.useKew) {
}
}
}
else if( global.useLiar) {
var lifter = require("liar").denodify;
else if(global.useLie) {
var Promise = require('lie');
var slicer = [].slice;
var lifter = function lifter(nodefn) {
return function() {
var p = new Promise(function (resolve, reject){
arguments[arguments.length++] = function(err, res) {
if (err) reject(err);
else resolve(res)
};
nodefn.apply(this, arguments);
});
return p;
}
}
}
else if(global.useThenPromise) {
var Promise = require('promise');
var slicer = [].slice;
var lifter = function lifter(nodefn) {
return function() {
var p = new Promise(function (resolve, reject){
arguments[arguments.length++] = function(err, res) {
if (err) reject(err);
else resolve(res)
};
try {
nodefn.apply(this, arguments);
}
catch (e) {
reject(e);
}
});
return p;
}
}
}
else if( global.useRSVP ) {
var lifter = require("rsvp").denodeify;
Expand Down
22 changes: 22 additions & 0 deletions benchmark/madeup-parallel/promises-then-promise.js
@@ -0,0 +1,22 @@
global.useThenPromise = true;

var Promise = require("promise");

require('../lib/fakesP');

module.exports = function upload(stream, idOrPath, tag, done) {
var queries = new Array(global.parallelQueries);
var tx = db.begin();

for( var i = 0, len = queries.length; i < len; ++i ) {
queries[i] = FileVersion.insert({index: i}).execWithin(tx);
}

Promise.all(queries).then().then(function() {
tx.commit();
done();
}, function(err) {
tx.rollback();
done(err);
});
}
5 changes: 3 additions & 2 deletions benchmark/package.json
Expand Up @@ -7,14 +7,15 @@
"async": "~0.7.0",
"deferred": "~0.7.0",
"kew": "~0.4.0",
"liar": "~0.6.0",
"optimist": "~0.6.0",
"q": "~1.0.0",
"rsvp": "~3.0.0",
"text-table": "~0.2.0",
"when": "~3.1.0",
"vow": "~0.4.1",
"davy": "~0.2.0"
"davy": "~0.2.0",
"lie": "^2.7.2",
"promise": "^5.0.0"
},
"devDependencies": {},
"scripts": {
Expand Down

0 comments on commit f187d26

Please sign in to comment.