From fe3d46b647bd00bb72280ccf25f8007d12c8cee3 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Wed, 22 Apr 2015 21:24:31 +0200 Subject: [PATCH] Updated the benchmark --- benchmarks/mongodb.js | 237 +++++++++++++++----------------------- benchmarks/mongodb.js.map | 1 + benchmarks/mongodb.ts | 108 +++++++++++++++++ package.json | 5 +- 4 files changed, 208 insertions(+), 143 deletions(-) create mode 100644 benchmarks/mongodb.js.map create mode 100644 benchmarks/mongodb.ts diff --git a/benchmarks/mongodb.js b/benchmarks/mongodb.js index 99db7f6..5f72fb6 100644 --- a/benchmarks/mongodb.js +++ b/benchmarks/mongodb.js @@ -1,143 +1,98 @@ -var async = require('async'), - MongoClient = require('mongodb').MongoClient, - Iridium = require('../'); - +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +/// +var Iridium = require('../index'); +var Promise = require('bluebird'); var objects = []; -for(var i = 0; i < 10000; i++) - objects.push({ - name: 'John', - surname: 'Doe', - birthday: new Date() - }); - -var iDB = new Iridium({ - database: 'iridium_bench' -}); - -var model = new Iridium.Model(iDB, 'iridium', { - name: String, - surname: String, - birthday: Date -}); - -var modelWrap = new Iridium.Model(iDB, 'iridiumWrap', { - name: String, - surname: String, - birthday: Date -}); - -iDB.register('model', model); - -function printTime(format, start) { - var ms = (new Date()).getTime() - start.getTime(); - console.log(format, ms.toString() + 'ms'); +for (var i = 0; i < 10000; i++) + objects.push({ name: 'John', surname: 'Doe', birthday: new Date() }); +var User = (function () { + function User() { + } + return User; +})(); +var WrappedUser = (function (_super) { + __extends(WrappedUser, _super); + function WrappedUser() { + _super.apply(this, arguments); + } + return WrappedUser; +})(Iridium.Instance); +var IridiumDB = (function (_super) { + __extends(IridiumDB, _super); + function IridiumDB() { + _super.call(this, { database: 'test' }); + this.User = new Iridium.Model(this, User, 'iridium', { + name: String, + surname: String, + birthday: Date + }); + this.UserWrapped = new Iridium.Model(this, WrappedUser, 'iridiumWrapped', { + name: String, + surname: String, + birthday: Date + }); + } + return IridiumDB; +})(Iridium.Core); +var baseline = null; +function benchmark(format, action, compareTo) { + var start = new Date(); + return action().then(function (result) { + var ms = (new Date()).getTime() - start.getTime(); + if (compareTo) { + var speedUp = ''; + if ((Math.abs(ms - compareTo) / compareTo) < 0.2) + speedUp = '(about the same)'; + else if (ms > compareTo) + speedUp = '(' + (ms / compareTo).toPrecision(2) + 'x slower)'; + else + speedUp = '(' + (compareTo / ms).toPrecision(2) + 'x faster)'; + console.log(format, ms.toString() + 'ms ' + speedUp); + } + else { + console.log(format, ms.toString() + 'ms'); + baseline = ms; + } + return ms; + }); } - -MongoClient.connect('mongodb://localhost/iridium_bench', function(err, mDB) { - if(err) throw err; - - iDB.connect(function(err) { - if(err) throw err; - - // Both databases are ready, let's start testing... - - var mDBcol = mDB.collection('mongo'); - async.series([ - function(done) { - mDBcol.remove({}, { w: 1 }, function(err, removed) { - if(err) return done(err); - return done(); - }); - }, - function(done) { - model.remove({}, function(err, removed) { - if(err) return done(err); - return done(); - }); - }, - function(done) { - console.log('MongoDB 10000 Inserts { w: 1 }'); - var start = new Date(); - mDBcol.insert(objects, { w: 1 }, function(err, inserted) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('Iridium 10000 Inserts { w: 1, wrap: false }'); - var start = new Date(); - model.insert(objects, { w: 1, wrap: false }, function(err, inserted) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('Iridium 10000 Inserts { w: 1, wrap: true }'); - var start = new Date(); - modelWrap.insert(objects, { w: 1, wrap: true }, function(err, inserted) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('MongoDB find()'); - var start = new Date(); - mDBcol.find({}).toArray(function(err, results) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('Iridium find() { wrap: false }'); - var start = new Date(); - model.find({}, { wrap: false }, function(err, results) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('Iridium find() { wrap: true }'); - var start = new Date(); - modelWrap.find({}, { wrap: true }, function(err, results) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('MongoDB remove()'); - var start = new Date(); - mDBcol.remove({}, function(err, results) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - console.log('Iridium remove()'); - var start = new Date(); - model.remove(function(err, results) { - if(err) return done(err); - printTime(' => %s', start); - return done(); - }); - }, - function(done) { - modelWrap.remove(function(err, results) { - if(err) return done(err); - return done(); - }); - } - ], function(err) { - if(err) throw err; - - mDB.close(); - iDB.disconnect(); - }); - }); -}); \ No newline at end of file +var iDB = new IridiumDB(); +iDB.connect().then(function () { return iDB.User.remove(); }).then(function () { return iDB.UserWrapped.remove(); }).then(function () { + return new Promise(function (resolve, reject) { + iDB.connection.collection('mongodb').remove(function (err) { + if (err) + return reject(err); + return resolve(null); + }); + }); +}).then(function () { return benchmark("MongoDB inserting 10 000 documents: %s", function () { + return new Promise(function (resolve, reject) { + iDB.connection.collection('mongodb').insert(objects, function (err, objects) { + if (err) + return reject(err); + return resolve(objects); + }); + }); +}); }).then(function () { return benchmark("Iridium Instances inserting 10 000 documents: %s", function () { return iDB.UserWrapped.insert(objects); }, baseline); }).then(function () { return benchmark("Iridium inserting 10 000 documents: %s", function () { return iDB.User.insert(objects); }, baseline); }).then(function () { return benchmark("MongoDB finding 10 000 documents: %s", function () { + return new Promise(function (resolve, reject) { + iDB.connection.collection('mongodb').find().toArray(function (err, objects) { + if (err) + return reject(err); + return resolve(objects); + }); + }); +}); }).then(function () { return benchmark("Iridium Instances finding 10 000 documents: %s", function () { return iDB.UserWrapped.find(); }, baseline); }).then(function () { return benchmark("Iridium finding 10 000 documents: %s", function () { return iDB.User.find(); }, baseline); }).then(function () { return benchmark("MongoDB removing 10 000 documents: %s", function () { + return new Promise(function (resolve, reject) { + iDB.connection.collection('mongodb').remove(function (err, objects) { + if (err) + return reject(err); + return resolve(objects); + }); + }); +}); }).then(function () { return benchmark("Iridium Instances removing 10 000 documents: %s", function () { return iDB.UserWrapped.remove(); }, baseline); }).then(function () { return benchmark("Iridium removing 10 000 documents: %s", function () { return iDB.User.remove(); }, baseline); }).then(function () { return iDB.close(); }).catch(function (err) { return console.error(err); }); +//# sourceMappingURL=mongodb.js.map \ No newline at end of file diff --git a/benchmarks/mongodb.js.map b/benchmarks/mongodb.js.map new file mode 100644 index 0000000..81dd249 --- /dev/null +++ b/benchmarks/mongodb.js.map @@ -0,0 +1 @@ +{"version":3,"file":"mongodb.js","sourceRoot":"","sources":["mongodb.ts"],"names":["User","User.constructor","WrappedUser","WrappedUser.constructor","IridiumDB","IridiumDB.constructor","benchmark"],"mappings":";;;;;;AACA,AADA,4CAA4C;AAC5C,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AACrC,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AAGrC,IAAI,OAAO,GAAG,EAAE,CAAC;AACjB,GAAG,CAAA,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE;IACzB,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;AAEzE,IAAM,IAAI;IAAVA,SAAMA,IAAIA;IAKVC,CAACA;IAADD,WAACA;AAADA,CAACA,AALD,IAKC;AAED,IAAM,WAAW;IAASE,UAApBA,WAAWA,UAA4CA;IAA7DA,SAAMA,WAAWA;QAASC,8BAAmCA;IAK7DA,CAACA;IAADD,kBAACA;AAADA,CAACA,AALD,EAA0B,OAAO,CAAC,QAAQ,EAKzC;AAED,IAAM,SAAS;IAASE,UAAlBA,SAASA,UAAqBA;IAChCA,SADEA,SAASA;QAEP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AC,CAAC"} \ No newline at end of file diff --git a/benchmarks/mongodb.ts b/benchmarks/mongodb.ts new file mode 100644 index 0000000..d06df40 --- /dev/null +++ b/benchmarks/mongodb.ts @@ -0,0 +1,108 @@ +/// +import Iridium = require('../index'); +import Promise = require('bluebird'); +import MongoDB = require('mongodb'); + +var objects = []; +for(var i = 0; i < 10000; i++) + objects.push({ name: 'John', surname: 'Doe', birthday: new Date() }); + +class User { + id: string; + name: string; + surname: string; + birthday: Date; +} + +class WrappedUser extends Iridium.Instance { + id: string; + name: string; + surname: string; + birthday: Date; +} + +class IridiumDB extends Iridium.Core { + constructor() { + super({ database: 'test' }); + } + + User = new Iridium.Model(this, User, 'iridium', { + name: String, + surname: String, + birthday: Date + }); + + UserWrapped = new Iridium.Model(this, WrappedUser, 'iridiumWrapped', { + name: String, + surname: String, + birthday: Date + }); +} + +var baseline = null; +function benchmark(format, action: () => Promise, compareTo?: number): Promise { + var start = new Date(); + return action().then((result) => { + var ms = (new Date()).getTime() - start.getTime(); + if(compareTo) { + var speedUp = ''; + if((Math.abs(ms - compareTo) / compareTo) < 0.2) speedUp = '(about the same)'; + else if(ms > compareTo) speedUp = '(' + (ms / compareTo).toPrecision(2) + 'x slower)'; + else speedUp = '(' + (compareTo / ms).toPrecision(2) + 'x faster)'; + console.log(format, ms.toString() + 'ms ' + speedUp); + } + else { + console.log(format, ms.toString() + 'ms'); + baseline = ms; + } + return ms; + }); +} + +var iDB = new IridiumDB(); +iDB.connect() + .then(() => iDB.User.remove()) + .then(() => iDB.UserWrapped.remove()) + .then(() => { + return new Promise((resolve, reject) => { + iDB.connection.collection('mongodb').remove((err) => { + if (err) return reject(err); + return resolve(null); + }); + }); + }) + .then(() => benchmark("MongoDB inserting 10 000 documents: %s", () => { + return new Promise((resolve, reject) => { + iDB.connection.collection('mongodb').insert(objects, (err, objects) => { + if(err) return reject(err); + return resolve(objects); + }); + }); + })) + .then(() => benchmark("Iridium Instances inserting 10 000 documents: %s", () => iDB.UserWrapped.insert(objects), baseline)) + .then(() => benchmark("Iridium inserting 10 000 documents: %s", () => iDB.User.insert(objects), baseline)) + + .then(() => benchmark("MongoDB finding 10 000 documents: %s", () => { + return new Promise((resolve, reject) => { + iDB.connection.collection('mongodb').find().toArray((err, objects: any) => { + if(err) return reject(err); + return resolve(objects); + }); + }); + })) + .then(() => benchmark("Iridium Instances finding 10 000 documents: %s", () => iDB.UserWrapped.find(), baseline)) + .then(() => benchmark("Iridium finding 10 000 documents: %s", () => iDB.User.find(), baseline)) + + .then(() => benchmark("MongoDB removing 10 000 documents: %s", () => { + return new Promise((resolve, reject) => { + iDB.connection.collection('mongodb').remove((err, objects: any) => { + if(err) return reject(err); + return resolve(objects); + }); + }); + })) + .then(() => benchmark("Iridium Instances removing 10 000 documents: %s", () => iDB.UserWrapped.remove(), baseline)) + .then(() => benchmark("Iridium removing 10 000 documents: %s", () => iDB.User.remove(), baseline)) + + .then(() => iDB.close()) + .catch((err) => console.error(err)); \ No newline at end of file diff --git a/package.json b/package.json index 36e06f8..6b60c01 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,9 @@ "chai": "1.x", "chai-as-promised": "4.x", "chai-fuzzy": "1.x", - "typescript-require": "^0.2.8", - "underscore": "^1.8.3" + "typescript-require": "^0.2.8", + "underscore": "^1.8.3", + "tick": "^0.1.1" }, "keywords": [ "mongodb",