Created
January 17, 2015 17:25
-
-
Save alan-mushi/f41362ada94883c88817 to your computer and use it in GitHub Desktop.
Files used to benchmark MongoDB random document in a collection.
This file contains 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
// Preparation for method 1 | |
use benchmark; | |
print("Method 1, preparation start."); | |
db.col.ensureIndex( { random_point: '2d' } ); | |
// This feels ugly... | |
db.col.find().map(function(u) { | |
db.col.update( | |
{ _id: u._id }, | |
{ $set: { random_point: [ Math.random(), 0 ] } } | |
); | |
}); | |
print("Method 1, preparation end."); |
This file contains 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
// Run for method 1 | |
use benchmark; | |
var N = 10000; | |
var start_date = new Date(); | |
for (var i = 0; i < N; i++) { | |
db.col.findOne({random_point : { $near : [ Math.random(), 0 ] } } ); | |
} | |
var end_date = new Date(); | |
print("Method 1, run time for ", N, " picks: ", (end_date - start_date), " ms"); |
This file contains 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
// Preparation for method 2 | |
use benchmark; | |
print("Method 2, preparation start."); | |
// This feels ugly... | |
db.col.find().map(function(u) { | |
db.col.update( | |
{ _id: u._id }, | |
{ $set: { random_point: Math.random() } } | |
); | |
}); | |
print("Method 2, preparation end."); |
This file contains 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
// Run for method 2 | |
use benchmark; | |
var N = 10000; | |
var start_date = new Date(); | |
for (var i = 0; i < N; i++) { | |
db.col.findOne({random_point : { $gte : Math.random() } } ); | |
} | |
var end_date = new Date(); | |
print("Method 2, run time for ", N, " picks: ", (end_date - start_date), " ms"); |
This file contains 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
// Run for method 3 | |
use benchmark; | |
var N = 10000; | |
function random_id(obj_id_slice1, obj_id_slice2, count) { | |
var v = obj_id_slice2 + Math.floor(Math.random() * (count + 1)); | |
return ObjectId(obj_id_slice1 + v.toString(16)); | |
} | |
var count = db.col.count(); | |
var first_obj_id = db.col.find().sort({ _id: 1 }).limit(1)[0]._id.valueOf(); | |
var first_obj_id_slice1 = first_obj_id.slice(0, 11); | |
var first_obj_id_slice2 = parseInt(first_obj_id.slice(11, 24), 16); | |
var start_date = new Date(); | |
for (var i = 0; i < N; i++) { | |
db.col.findOne({ _id: { $gte: random_id(first_obj_id_slice1, first_obj_id_slice2, count) } }); | |
} | |
var end_date = new Date(); | |
print("Method 3, run time for ", N, " picks: ", (end_date - start_date), " ms"); |
This file contains 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
// Run for method 4a | |
use benchmark; | |
var N = 10000; | |
var start_date = new Date(); | |
for (var i = 0; i < N; i++) { | |
db.col.find().limit(-1).skip(Math.random() * db.col.count()); | |
} | |
var end_date = new Date(); | |
print("Method 4a, run time for ", N, " picks: ", (end_date - start_date), " ms"); |
This file contains 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
// Run for method 4b | |
use benchmark; | |
var N = 10000; | |
var nb = db.col.count(); | |
var start_date = new Date(); | |
for (var i = 0; i < N; i++) { | |
db.col.find().limit(-1).skip(Math.random() * nb); | |
} | |
var end_date = new Date(); | |
print("Method 4b, run time for ", N, " picks: ", (end_date - start_date), " ms"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment