Skip to content

Commit

Permalink
Merge pull request #12027 from lpizzinidev/fix-delete-bench
Browse files Browse the repository at this point in the history
fix benchmark
  • Loading branch information
Uzlopak committed Jul 1, 2022
2 parents a73234f + 901c24b commit 4681d47
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions benchmarks/benchjs/delete.js
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;

// to make things work in the way the are normally described online...
/*
Expand All @@ -17,12 +17,11 @@ const mongo = require('mongodb');
* These are all the benchmark tests for deleting data
*/


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', function (err, client) {
if (err) {
throw err;
}
Expand All @@ -33,7 +32,7 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
name: String,
age: Number,
likes: [String],
address: String
address: String,
});

const User = mongoose.model('User', UserSchema);
Expand All @@ -46,13 +45,13 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
name: 'name',
age: 0,
likes: ['dogs', 'cats', 'pizza'],
address: ' Nowhere-ville USA'
address: ' Nowhere-ville USA',
};

// insert all of the data here
let count = 1000;
for (let i = 0; i < 500; i++) {
User.create(data, function(err, u) {
User.create(data, function (err, u) {
if (err) {
throw err;
}
Expand All @@ -64,20 +63,20 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
nData.age = data.age;
nData.likes = data.likes;
nData.address = data.address;
user.insert(nData, function(err, res) {
dIds.push(res.insertedIds[0]);
user.insertOne(nData, function (err, res) {
dIds.push(res.insertedIds);
--count || next();
});
}

function closeDB() {
User.count(function(err, res) {
User.count(function (err, res) {
if (res !== 0) {
console.log('Still mongoose entries left...');
}
mongoose.disconnect();
});
user.count({}, function(err, res) {
user.count({}, function (err, res) {
if (res !== 0) {
console.log('Still driver entries left...');
}
Expand All @@ -88,36 +87,39 @@ mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
});
}

suite.add('Delete - Mongoose', {
defer: true,
fn: function(deferred) {
User.remove({ _id: mIds.pop() }, function(err) {
if (err) {
throw err;
}
deferred.resolve();
});
}
}).add('Delete - Driver', {
defer: true,
fn: function(deferred) {
user.remove({ _id: dIds.pop() }, function(err) {
if (err) {
throw err;
}
deferred.resolve();
});
}
})
.on('cycle', function(evt) {
suite
.add('Delete - Mongoose', {
defer: true,
fn: function (deferred) {
User.remove({ _id: mIds.pop() }, function (err) {
if (err) {
throw err;
}
deferred.resolve();
});
},
})
.add('Delete - Driver', {
defer: true,
fn: function (deferred) {
user.deleteOne({ _id: dIds.pop() }, function (err) {
if (err) {
throw err;
}
deferred.resolve();
});
},
})
.on('cycle', function (evt) {
if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
console.log(String(evt.target));
}
}).on('complete', function() {
})
.on('complete', function () {
closeDB();
if (!process.env.MONGOOSE_DEV && !process.env.PULL_REQUEST) {
const outObj = {};
this.forEach(function(item) {
this.forEach(function (item) {
const out = {};
out.stats = item.stats;
delete out.stats.sample;
Expand Down

0 comments on commit 4681d47

Please sign in to comment.