Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created August 28, 2013 03:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheckmann/6361689 to your computer and use it in GitHub Desktop.
Save aheckmann/6361689 to your computer and use it in GitHub Desktop.
tweak various mongoose settings to observe cpu impact.
/**
* configuration
*/
// connection pool size
var poolSize = 5;
// number of parallel queries
var n = 60;
// size of each document
var size = 1024;
// milliseconds between each test
var time = 50;
// exclude the "name" property
var excludeName = false;
// skip mongoose hydration overhead
var lean = false;
/**
* dependencies
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
/**
* uri
*/
var uri = process.env.MONGOOSE_SET_TEST_URI || 'mongodb://localhost';
var dbname = 'goosetest-1666';
mongoose.connect(uri + '/' + dbname, { server: { poolSize: poolSize }});
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
/**
* Model
*/
var schema = new Schema({
name: String
, _id: Number
});
var A = mongoose.model('A', schema);
// create dummy docs at configured size
function makeDocs (cb) {
var docs = [];
for (var i = 0; i < n; ++i) (function(i){
docs.push({ _id: i, name: new Array(size*1024).join('x') });
})(i);
A.create(docs, cb);
docs = null;
}
mongoose.connection.on('open', function () {
mongoose.connection.db.dropDatabase(function () {
makeDocs(function (err) {
if (err) return close(err);
console.log('created %d docs', arguments.length-1);
function test () {
var start = new Date;
var count = n;
function done () {
--count || next();
}
function next () {
console.log('read of %d docs finished in %d seconds', n, (new Date - start)/1000);
setTimeout(test, time);
}
for (var i = 0; i < n; ++i) {
var query = A.findById(i)
if (excludeName) query.select('-name');
if (lean) query.lean();
query.exec(done);
}
}
test();
})
});
});
function close (err) {
console.log(err);
mongoose.disconnect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment