Skip to content

Instantly share code, notes, and snippets.

@ngocdaothanh
Created January 15, 2015 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngocdaothanh/5a2c22b7dce70f65b7a7 to your computer and use it in GitHub Desktop.
Save ngocdaothanh/5a2c22b7dce70f65b7a7 to your computer and use it in GitHub Desktop.
MongoDB's DB creation benchmark
// MongoDB's DB creation benchmark:
// * データベースを作る前にデータベース用のユーザを作成。
// * そのユーザ権限でデータベースを作成。
// * コレクションを10個、ドキュメントを10個、フィールドを10個ずつ作成。
// * 作成データベース数は10000
//
// 1.
//
// Start MongoDB server without "--auth" option, and create a bootstrap admin user:
// use admin
// db.createUser({user: 'admin', pwd: 'admin', roles: ['root']})
//
// You may run "db.dropAllUsers()" to clean up.
//
// 2.
//
// Run with "mongo" command: mongo many_user.js
var HOST = 'localhost'
var PORT = 27017
var ADMIN_USER = 'admin'
var ADMIN_PWD = 'admin'
var NUM_DBS = 10
var NUM_COLS = 10
var NUM_DOCS = 10
var NUM_FLDS = 10
print('HOST: ' + HOST);
print('PORT: ' + PORT);
print('NUM_DBS: ' + NUM_DBS);
print('NUM_COLS: ' + NUM_COLS);
print('NUM_DOCS: ' + NUM_DOCS);
print('NUM_FLDS: ' + NUM_FLDS);
var conn = new Mongo(HOST + ':' + PORT);
// Create all users first, then use the users to create DBs
print('Create users...');
var db = conn.getDB('admin');
db.auth(ADMIN_USER, ADMIN_PWD);
for (var iU = 1; iU <= NUM_DBS; iU++) {
db.createUser({user: 'u' + iU, pwd: 'p' + iU, roles: ["root"]})
}
// Initialize a doc that will be inserted to all DBs
var doc = {};
for (var iFld = 1; iFld <= NUM_FLDS; iFld++) {
var fldName = 'fld' + iFld;
doc[fldName] = iFld;
}
// Print in CSV format so that the result can be easily graphed later
print('No,"DB creation time","Time from start"');
var startTime = new Date().getTime();
for (var iDb = 1; iDb <= NUM_DBS; iDb++) {
var t1 = new Date().getTime();
// Authenticate to DB "admin"
var db = conn.getDB('admin');
db.auth('u' + iDb, 'p' + iDb);
// From the above DB, create another DB
var dbName = 'db' + iDb;
var db = conn.getDB(dbName);
for (var iCol = 1; iCol <= NUM_COLS; iCol++) {
var colName = 'col' + iCol;
for (var iDoc = 1; iDoc <= NUM_DOCS; iDoc++) {
db[colName].insert(doc, {writeConcern: {j: true}});
}
}
var t2 = new Date().getTime();
print('' + iDb + ',' + (t2 - t1) + ',' + (t2 - startTime));
}
// MongoDB's DB creation benchmark:
// * データベースを作る際は専用のユーザで作成
// * コレクションを10個、ドキュメントを10個、フィールドを10個ずつ作成
// * 作成データベース数は10000
//
// Run with "mongo" command: mongo one_user.js
var HOST = 'localhost'
var PORT = 27017
var NUM_DBS = 10
var NUM_COLS = 10
var NUM_DOCS = 10
var NUM_FLDS = 10
print('HOST: ' + HOST);
print('PORT: ' + PORT);
print('NUM_DBS: ' + NUM_DBS);
print('NUM_COLS: ' + NUM_COLS);
print('NUM_DOCS: ' + NUM_DOCS);
print('NUM_FLDS: ' + NUM_FLDS);
// Print in CSV format so that the result can be easily graphed later
print('No,"DB creation time","Time from start"');
// Initialize a doc that will be inserted to all DBs
var doc = {};
for (var iFld = 1; iFld <= NUM_FLDS; iFld++) {
var fldName = 'fld' + iFld;
doc[fldName] = iFld;
}
var conn = new Mongo(HOST + ':' + PORT);
var startTime = new Date().getTime();
for (var iDb = 1; iDb <= NUM_DBS; iDb++) {
var t1 = new Date().getTime();
var dbName = 'db' + iDb;
var db = conn.getDB(dbName);
for (var iCol = 1; iCol <= NUM_COLS; iCol++) {
var colName = 'col' + iCol;
for (var iDoc = 1; iDoc <= NUM_DOCS; iDoc++) {
db[colName].insert(doc, {writeConcern: {j: true}});
}
}
var t2 = new Date().getTime();
print('' + iDb + ',' + (t2 - t1) + ',' + (t2 - startTime));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment