Skip to content

Instantly share code, notes, and snippets.

@adamchel
Last active November 3, 2017 04:06
Show Gist options
  • Save adamchel/637c9e28b8ec180f1154921a6ce51b3b to your computer and use it in GitHub Desktop.
Save adamchel/637c9e28b8ec180f1154921a6ce51b3b to your computer and use it in GitHub Desktop.
MongoDB Field Bucketing Benchmarks
function logMessage(message) {
var d = new Date()
print(`${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}: ${message}`)
}
coll_size = 50000 // Number of documents in the collection we're aggregating
subdoc_size = 2500 // Number of subfields in the columns subdocument
subdocument_targets = [10, 25, 50, 100, 250, 500, 1000, 1500, 2500]
// Non-bucketed tests
logMessage("Dropping bucket collection.")
db.bucket.drop()
// Create the columns subdocument.
columns_subdoc = {}
for (var i = 0; i < subdoc_size; ++i) {
columns_subdoc[`col${i + 1}`] = 5;
}
logMessage(`Inserting ${coll_size} non-bucketed documents.`)
for (var i = 0; i < coll_size; i++) {
db.bucket.insert({
_id: i,
columns: columns_subdoc
})
}
logMessage("Finished inserting non-bucketed documents.")
for (var subdoc_target_idx = 0; subdoc_target_idx < subdocument_targets.length; ++subdoc_target_idx) {
subdoc_target = subdocument_targets[subdoc_target_idx]
//logMessage(`Using collection with ${coll_size} documents and targeting subfield ${subdoc_target}.`)
//logMessage("Running non-bucketed benchmark 15 times.")
var non_bucketed_benchmark_total = 0;
for (var i = 0; i < 15; i++) {
//logMessage(`Aggregating non-bucketed documents with sum of subfield ${subdoc_target}`)
var before = new Date()
db.bucket.aggregate([{
$group: {_id: null, total: {$sum : `$columns.col${subdoc_target}`}}
}])
var after = new Date()
//logMessage(`Finished aggregating non-bucketed documents with sum of subfield ${subdoc_target}. Took ${after - before}ms`)
non_bucketed_benchmark_total += (after - before)
}
logMessage(`----- RESULTS FOR NON-BUCKETED COLLECTION WITH ${coll_size} DOCUMENTS AND TARGETING SUBFIELD ${subdoc_target} -----`)
logMessage(`----- Average aggregation time ${non_bucketed_benchmark_total / 15}ms ---------------------`)
logMessage("-------------------------------------------------------------------------------------------")
}
// Bucketed tests
logMessage("Starting bucketed tests.")
bucket_sizes = [16, 32, 64, 128]
for (var bucket_size_idx = 0; bucket_size_idx < bucket_sizes.length; ++bucket_size_idx) {
bucket_size = bucket_sizes[bucket_size_idx]
logMessage(`Using buckets with ${bucket_size} subfields.`)
logMessage("Dropping bucket collection.")
db.bucket.drop()
// Create the bucketed columns subdocument.
var bucket_idx = 0
columns_subdoc = {}
for (var i = 0; i < subdoc_size; ++i) {
if (i % bucket_size == 0) {
++bucket_idx;
columns_subdoc[`bucket${bucket_idx}`] = {}
}
columns_subdoc[`bucket${bucket_idx}`][`col${i + 1}`] = 5;
}
logMessage("Inserting bucketed documents")
for (var i = 0; i < coll_size; i++) {
db.bucket.insert({
_id: i,
columns: columns_subdoc
})
}
logMessage("Finished inserting bucketed documents")
for (var subdoc_target_idx = 0; subdoc_target_idx < subdocument_targets.length; ++subdoc_target_idx) {
subdoc_target = subdocument_targets[subdoc_target_idx]
//logMessage("Running bucketed benchmark 15 times.")
target_bucket = Math.floor(subdoc_target / bucket_size) + 1
var bucketed_benchmark_total = 0
for (var i = 0; i < 15; i++) {
//logMessage(`Aggregating bucketed documents with sum of subfield ${subdoc_target}`)
var before = new Date()
db.bucket.aggregate([{
$group: {_id: null, total: {$sum : `$columns.bucket${target_bucket}.col${subdoc_target}`}}
}])
var after = new Date()
//logMessage(`Finished aggregating bucketed documents with sum of subfield ${subdoc_target} Took ${after - before}ms`)
bucketed_benchmark_total += (after - before)
}
logMessage(`----- RESULTS FOR BUCKETED COLLECTION WITH ${coll_size} DOCUMENTS,\n----- ${bucket_size}-FIELD BUCKETS AND TARGETING SUBFIELD ${subdoc_target}`)
logMessage(`----- Average aggregation time ${bucketed_benchmark_total / 15}ms`)
logMessage(`--------------------------------`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment