deno.land / x / mongoose@6.7.5 / benchmarks / benchjs / insert.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190'use strict';const mongoose = require('../../lib');const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
const Schema = mongoose.Schema;const mongoClient = require('mongodb').MongoClient;const utils = require('../../lib/utils.js');const ObjectId = Schema.Types.ObjectId;
// to make things work in the way the are normally described online.../* *global.Schema = Schema; *global.mongoose = mongoose; */
/** * These are all the benchmark tests for inserting data */
mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { if (err) { throw err; } mongoClient.connect( 'mongodb://localhost/mongoose-bench', function (err, client) { if (err) { throw err; }
const db = client.db('mongoose-bench');
const Comments = new Schema(); Comments.add({ title: String, date: Date, body: String, comments: [Comments], });
let BlogPost = new Schema({ title: String, author: String, slug: String, date: Date, meta: { date: Date, visitors: Number, }, published: Boolean, mixed: {}, numbers: [Number], tags: [String], owners: [ObjectId], comments: [Comments], def: { type: String, default: 'kandinsky', }, });
const blogData = { title: 'dummy post', author: 'somebody', slug: 'test.post', date: new Date(), meta: { date: new Date(), visitors: 9001 }, published: true, mixed: { thisIsRandom: true }, numbers: [1, 2, 7, 10, 23432], tags: ['test', 'BENCH', 'things', 'more things'], def: 'THANGS!!!', comments: [], }; const commentData = { title: 'test comment', date: new Date(), body: 'this be some crazzzyyyyy text that would go in a comment', comments: [ { title: 'second level', date: new Date(), body: 'texttt', }, ], }; for (let i = 0; i < 5; i++) { blogData.comments.push(commentData); } const data = { name: 'name', age: 0, likes: ['dogs', 'cats', 'pizza'], address: ' Nowhere-ville USA', };
const UserSchema = new Schema({ name: String, age: Number, likes: [String], address: String, });
const User = mongoose.model('User', UserSchema); BlogPost = mongoose.model('BlogPost', BlogPost); const user = db.collection('user'); const blogpost = db.collection('blogpost');
function closeDB() { mongoose.connection.db.dropDatabase(function () { mongoose.disconnect(); process.exit(); }); }
suite .add('Insert - Mongoose - Basic', { defer: true, fn: function (deferred) { const nData = utils.clone(data); User.create(nData, function (err) { if (err) { throw err; } deferred.resolve(); }); }, }) .add('Insert - Driver - Basic', { defer: true, fn: function (deferred) { const nData = utils.clone(data); user.insertOne(nData, function (err) { if (err) { throw err; } deferred.resolve(); }); }, }) .add('Insert - Mongoose - Embedded Docs', { defer: true, fn: function (deferred) { const bp = utils.clone(blogData); BlogPost.create(bp, function (err) { if (err) { throw err; } deferred.resolve(); }); }, }) .add('Insert - Driver - Embedded Docs', { defer: true, fn: function (deferred) { const bp = utils.clone(blogData); blogpost.insertOne(bp, 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 () { closeDB(); if (!process.env.MONGOOSE_DEV && !process.env.PULL_REQUEST) { const outObj = {}; this.forEach(function (item) { const out = {}; out.stats = item.stats; delete out.stats.sample; out.ops = item.hz; outObj[item.name.replace(/\s/g, '')] = out; }); console.dir(outObj, { depth: null, colors: true }); } }) .run({ async: true }); } );});
Version Info