Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Improve performance especially of validate and clone #11298

Merged
merged 4 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions benchmarks/benchjs/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();

const Schema = mongoose.Schema;
const mongo = require('mongodb');
const mongoClient = require('mongodb').MongoClient;
const utils = require('../../lib/utils.js');
const ObjectId = Schema.Types.ObjectId;

Expand All @@ -20,11 +20,11 @@ const ObjectId = Schema.Types.ObjectId;
*/


mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
if (err) {
throw err;
}
mongo.connect('mongodb://localhost', function(err, client) {
mongoClient.connect('mongodb://localhost/mongoose-bench', function(err, client) {
if (err) {
throw err;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
defer: true,
fn: function(deferred) {
const nData = utils.clone(data);
user.insert(nData, function(err) {
user.insertOne(nData, function(err) {
if (err) {
throw err;
}
Expand All @@ -149,7 +149,7 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
defer: true,
fn: function(deferred) {
const bp = utils.clone(blogData);
blogpost.insert(bp, function(err) {
blogpost.insertOne(bp, function(err) {
if (err) {
throw err;
}
Expand Down
46 changes: 30 additions & 16 deletions benchmarks/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

const mongoose = require('../');
const Schema = mongoose.Schema;
const Benchmark = require('benchmark');

const DocSchema = new Schema({
title: String
});

const SimpleSchema = new Schema({
string: { type: String, required: true },
number: { type: Number, min: 10 },
});

const AllSchema = new Schema({
string: {type: String, required: true},
number: {type: Number, min: 10},
string: { type: String, required: true },
number: { type: Number, min: 10 },
date: Date,
bool: Boolean,
buffer: Buffer,
Expand All @@ -22,11 +28,11 @@ const AllSchema = new Schema({
buffers: [Buffer],
objectids: [Schema.ObjectId],
docs: {
type: [DocSchema], validate: function() {
type: [DocSchema], validate: function () {
return true;
}
},
s: {nest: String}
s: { nest: String }
});

const A = mongoose.model('A', AllSchema);
Expand All @@ -44,21 +50,29 @@ const a = new A({
bools: [true, false, false, true, true],
buffers: [Buffer.from([33]), Buffer.from([12])],
objectids: [new mongoose.Types.ObjectId],
docs: [{title: 'yo'}, {title: 'nowafasdi0fas asjkdfla fa'}],
s: {nest: 'hello there everyone!'}
docs: [{ title: 'yo' }, { title: 'nowafasdi0fas asjkdfla fa' }],
s: { nest: 'hello there everyone!' }
});

const start = new Date;
const total = 100000;
let i = total;
let len;

for (i = 0, len = total; i < len; ++i) {
a.toObject({depopulate: true});
}
const Simple = mongoose.model('Simple', SimpleSchema);
const simple = new Simple({
string: 'hello world',
number: 444848484,
});

const time = (new Date - start) / 1000;
console.error('took %d seconds for %d docs (%d dps)', time, total, total / time);
new Benchmark.Suite()
.add('Simple', function () {
simple.toObject({ depopulate: true });
})
.add('AllSchema', function () {
a.toObject({ depopulate: true });
})
.on('cycle', function (evt) {
if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
console.log(String(evt.target));
}
})
.run();
process.memoryUsage();

// --trace-opt --trace-deopt --trace-bailout
46 changes: 29 additions & 17 deletions benchmarks/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,48 @@
'use strict';

const mongoose = require('../../mongoose');
const fs = require('fs');
const Benchmark = require('benchmark');

const Schema = mongoose.Schema;

const CheckItem = new Schema({
name: {type: String},
type: {type: String},
pos: {type: Number}
let CheckItem = new Schema({
name: { type: String },
type: { type: String },
pos: { type: Number }
});

const Checklist = new Schema({
name: {type: String},
checkItems: {type: [CheckItem]}
name: { type: String },
checkItems: { type: [CheckItem] }
});

let Board = new Schema({
checklists: {type: [Checklist]}
checklists: { type: [Checklist] }
});

// const start1 = new Date();
Board = mongoose.model('Board', Board);
const BoardModel = mongoose.model('Board', Board);
const CheckItemModel = mongoose.model('CheckItem', CheckItem);
// const Cl = mongoose.model('Checklist', Checklist);
const doc = JSON.parse(fs.readFileSync(__dirname + '/bigboard.json'));
const doc = require('./bigboard.json');
// const time1 = (new Date - start1);
// console.error('reading from disk and parsing JSON took %d ms', time1);

const start2 = new Date();
const iterations = 1000;
for (let i = 0; i < iterations; ++i) {
new Board(doc);
}
const time2 = (new Date - start2);
console.error('creation of large object took %d ms, %d ms per object', time2, time2 / iterations);
new Benchmark.Suite()
.add('CheckItem', function () {
const test = new CheckItemModel({
"_id": "4daee8a2aae47fe55305eabf",
"pos": 32768,
"type": "check",
"name": "delete checklists"
});
})
.add('Board', function () {
const test = new BoardModel(doc);
})
.on('cycle', function (evt) {
if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
console.log(String(evt.target));
}
})
.run();
8 changes: 4 additions & 4 deletions benchmarks/mem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mongoose = require('../');

const Schema = mongoose.Schema;

mongoose.connect('localhost', 'testing_bench');
mongoose.connect('mongodb://localhost/mongoose-bench');

const DocSchema = new Schema({
title: String
Expand Down Expand Up @@ -57,10 +57,10 @@ methods.push(function(a, cb) {
A.where('date', a.date).where('array').in(3).limit(10).exec(cb);
}); // 1.82 MB
methods.push(function(a, cb) {
A.update({_id: a._id}, {$addToset: {array: 'heeeeello'}}, cb);
A.updateOne({_id: a._id}, {$addToset: {array: 'heeeeello'}}, cb);
}); // 3.32 MB
methods.push(function(a, cb) {
A.remove({_id: a._id}, cb);
A.deleteOne({_id: a._id}, cb);
}); // 3.32 MB
methods.push(function(a, cb) {
A.find().where('objectids').exists().select('dates').limit(10).exec(cb);
Expand All @@ -75,7 +75,7 @@ methods.push(function(a, cb) {
a.bool = false;
a.array.push(3);
a.dates.push(new Date);
a.bools.push([true, false]);
// a.bools.push([true, false]);
a.docs.addToSet({title: 'woot'});
a.strings.remove('three');
a.numbers.pull(72);
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const B = mongoose.model('B', Schema({
let start;
let count = 0;

mongoose.connect('localhost', 'benchmark-populate', function(err) {
mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
if (err) {
return done(err);
}
Expand Down
70 changes: 70 additions & 0 deletions benchmarks/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// require('nodetime').profile();

'use strict';

const mongoose = require('../../mongoose');
const Benchmark = require('benchmark');

const Schema = mongoose.Schema;
const breakfastSchema = new Schema({
eggs: {
type: Number,
min: [6, 'Too few eggs'],
max: 12
},
bacon: {
type: Number,
required: [true, 'Why no bacon?']
},
drink: {
type: String,
enum: ['Coffee', 'Tea'],
required: function () {
return this.bacon > 3;
}
}
});
const Breakfast = mongoose.model('Breakfast', breakfastSchema);
// const time1 = (new Date - start1);
// console.error('reading from disk and parsing JSON took %d ms', time1);
const badBreakfast = new Breakfast({
eggs: 2,
bacon: 0,
drink: 'Milk'
});

const goodBreakfast = new Breakfast({
eggs: 6,
bacon: 1,
drink: 'Tea'
})
// const start = new Date;
// const total = 10000000;
// let i = total;
// let len;

// for (i = 0, len = total; i < len; ++i) {

// const goodBreakfast = new Breakfast({
// eggs: 6,
// bacon: 1,
// drink: 'Tea'
// })
// goodBreakfast.validateSync();
// }

// const time = (new Date - start) / 1000;
// console.error('took %d seconds for %d docs (%d dps)', time, total, total / time);
new Benchmark.Suite()
.add('invalid', function () {
badBreakfast.validateSync();
})
.add('valid', function () {
goodBreakfast.validateSync();
})
.on('cycle', function (evt) {
if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
console.log(String(evt.target));
}
})
.run();
Loading