Skip to content
Browse files

Tests to illustrate how slow populate and subpopulate are

  • Loading branch information...
1 parent c4e5b6a commit 7aa25494c8ab10f0069d167950a39fa1081bf466 @JoshuaGross committed Nov 4, 2012
Showing with 67 additions and 0 deletions.
  1. +2 −0 lib/mongoose-subpopulate.js
  2. +1 −0 test/fixtures/model.js
  3. +64 −0 test/mongoose.js
View
2 lib/mongoose-subpopulate.js
@@ -199,6 +199,7 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
// TODO: sub-sub populate
var postExec = function postExec (err, result) {
if (result) {
+ console.time('postExec');
async.forEach(schema.populateStack, function (path, iter) {
var splitPath = path.split('.');
var cursor = result;
@@ -224,6 +225,7 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
}
iter();
}, function () {
+ console.timeEnd('postExec');
schema.populateStack = {};
wrapCallback(err, result);
});
View
1 test/fixtures/model.js
@@ -25,6 +25,7 @@ var defineModels = function defineModels (mongoose, callback) {
var UserSchema = new Schema({
'username': { type: String, required: true },
'email': { type: String, index: {unique: true}, required: true },
+ 'favorite_document': { type: ObjectId, ref: 'Document', required: false },
'password': { type: String }
});
mongoose.model('User', UserSchema);
View
64 test/mongoose.js
@@ -371,6 +371,21 @@ describe('mongoose additions bugs', function () {
});
});
describe('mongoose large record handling', function () {
+ var doc;
+ var project;
+ it('should set up multiple-level populate', function (done) {
+ var m = common.db();
+ project = new m.Project();
+ project.owner = new m.User();
+ project.title = 'The Coolest Project';
+ project.save(function () {
+ doc = new m.Document();
+ doc.project = project;
+ doc.save(function () {
+ done();
+ });
+ });
+ });
it('should be able to create large amounts of records', function (done) {
this.timeout(0);
var m = common.db();
@@ -382,6 +397,7 @@ describe('mongoose large record handling', function () {
var u = new m.User();
u.username = 'username__'+i;
u.email = 'test'+(500+i)+'@test.edu';
+ u.favorite_document = doc;
u.save(function () {
iter(null);
u = null;
@@ -393,16 +409,64 @@ describe('mongoose large record handling', function () {
it('should be able to fetch large amounts of records', function (done) {
this.timeout(0);
var m = common.db();
+ console.time('get users');
m.User.find().exec(function (users) {
+ console.timeEnd('get users');
expect(users.length).to.be.greaterThan(1000);
+ console.time('resave users');
+ async.map(users, function (u, iterate) {
+ var newEmail = u.email+'.uk';
+ u.email = newEmail;
+ u.save(function (err, newU) {
+ expect(newU.email).to.be(newEmail);
+ iterate(null, null);
+ });
+ }, function () {
+ console.timeEnd('resave users');
+ done();
+ });
+ });
+ });
+ it('should be able to fetch and populate large amounts of records', function (done) {
+ this.timeout(0);
+ var m = common.db();
+ console.time('get users');
+ m.User.find({ favorite_document: { $ne: null } }).populate('favorite_document').exec(function (users) {
+ console.timeEnd('get users');
+ expect(users.length).to.be(1000);
+ console.time('resave users');
+ async.map(users, function (u, iterate) {
+ expect(!!u.favorite_document).to.be(true);
+ var newEmail = u.email+'.uk';
+ u.email = newEmail;
+ u.save(function (err, newU) {
+ expect(newU.email).to.be(newEmail);
+ iterate(null, null);
+ });
+ }, function () {
+ console.timeEnd('resave users');
+ done();
+ });
+ });
+ });
+ it('should be able to fetch and sub-populate large amounts of records', function (done) {
+ this.timeout(0);
+ var m = common.db();
+ console.time('get users');
+ m.User.find({ favorite_document: { $ne: null } }).populate('favorite_document').populate('favorite_document.project').exec(function (users) {
+ console.timeEnd('get users');
+ expect(users.length).to.be(1000);
+ console.time('resave users');
async.map(users, function (u, iterate) {
+ expect(!!u.favorite_document.project).to.be(true);
var newEmail = u.email+'.uk';
u.email = newEmail;
u.save(function (err, newU) {
expect(newU.email).to.be(newEmail);
iterate(null, null);
});
}, function () {
+ console.timeEnd('resave users');
done();
});
});

0 comments on commit 7aa2549

Please sign in to comment.
Something went wrong with that request. Please try again.