Skip to content

Commit

Permalink
Updated the benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Apr 22, 2015
1 parent 2d5929b commit fe3d46b
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 143 deletions.
237 changes: 96 additions & 141 deletions benchmarks/mongodb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benchmarks/mongodb.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions benchmarks/mongodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/// <reference path="../_references.d.ts" />
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<User, WrappedUser> {
id: string;
name: string;
surname: string;
birthday: Date;
}

class IridiumDB extends Iridium.Core {
constructor() {
super({ database: 'test' });
}

User = new Iridium.Model<User, User>(this, User, 'iridium', {
name: String,
surname: String,
birthday: Date
});

UserWrapped = new Iridium.Model<User, WrappedUser>(this, WrappedUser, 'iridiumWrapped', {
name: String,
surname: String,
birthday: Date
});
}

var baseline = null;
function benchmark<T>(format, action: () => Promise<T>, compareTo?: number): Promise<number> {
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<any>((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<any>((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<any>((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<any>((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));
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fe3d46b

Please sign in to comment.