Created
July 28, 2017 21:43
-
-
Save stevenhao/6d73ded92ee0f2fa2a252274500519e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printDBInfo() { | |
print(''); | |
print('===================================================================='); | |
print('DB Version: ' + db.version()); | |
print('Storage Engine: ' + JSON.stringify(db.serverStatus().storageEngine)); | |
print('Replication Info: ' + JSON.stringify(db.getReplicationInfo())); | |
print(''); | |
} | |
printDBInfo(); | |
var id, tokenCount, size, trials; | |
var startTime, endTime; | |
id = 'testid7'; | |
//print('clearing tokens...'); | |
db.users.update({_id: id}, { set: { 'services': null }}, { upsert: true }); | |
//print('cleared tokens'); | |
tokenCount = 0; | |
function randomString() { | |
return Math.random().toString(); | |
} | |
time = 0 | |
function generateToken() { | |
var when, hashedToken; | |
when = new Date() + time; | |
time += 1 | |
hashedToken = id + randomString() + '#' + tokenCount; // this should never collide, within this run | |
tokenCount += 1; | |
return { when: when, hashedToken: hashedToken }; | |
} | |
function makeTokens(num) { | |
tokens = []; | |
// generate the tokenlist | |
for(i = 0; i < num; i += 1) { | |
token = generateToken(i); | |
tokens.push(token); | |
} | |
return tokens; | |
} | |
numUsers = 1000; | |
tokensPerUser = 10; | |
print('Adding ' + numUsers + ' users with ' + tokensPerUser + ' tokens each...'); | |
startTime = new Date(); | |
function populateUsers(num, numTokens) { | |
for (var i = 0; i < num; i += 1) { | |
tokens = makeTokens(numTokens); | |
db.users.update( | |
{ | |
_id: 'randomguy' + i, | |
}, | |
{ | |
$set: { | |
'services.resume.loginTokens': tokens | |
} | |
}, | |
{ | |
upsert: true | |
} | |
); | |
} | |
} | |
//populateUsers(numUsers, tokensPerUser); | |
endTime = new Date(); | |
//print('Took ' + (endTime - startTime) + 'ms'); | |
size = 7000; | |
print('Populating services.resume.loginTokens with ' + size + ' tokens... '); | |
startTime = new Date(); | |
function populate() { | |
var done = 0; | |
var CHUNK_SIZE = 50000; | |
while (done < size) { | |
chunk = Math.min(size - done, CHUNK_SIZE) | |
//print('Adding ', chunk, ' tokens'); | |
done += chunk | |
tokens = makeTokens(chunk); | |
// set the tokenlist | |
db.users.update( | |
{ | |
_id: id, | |
}, | |
{ | |
$push: { | |
'services.resume.loginTokens': { | |
$each: tokens | |
} | |
} | |
} | |
); | |
} | |
}; | |
populate(); | |
endTime = new Date(); | |
//print('Took ' + (endTime - startTime) + 'ms'); | |
trials = 5; | |
operation = '$addToSet'; | |
print('Calling ' + operation + ' ' + trials + ' times '); | |
startTime = new Date(); | |
function runTrial() { | |
for(i = 0; i < trials; i += 1) { | |
token = generateToken(i); | |
update = {}; | |
update[operation] = { // operation is either $push or $addToSet | |
'services.resume.loginTokens': token | |
}; | |
db.users.update( | |
{ | |
_id: id, | |
}, | |
update | |
); | |
} | |
}; | |
runTrial(); | |
endTime = new Date(); | |
print('Took ' + (endTime - startTime)/trials + 'ms on average'); | |
print('Done running benchmark'); | |
print('===================================================================='); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment