Created
July 25, 2020 03:02
-
-
Save HelveticaScenario/79027fb2708748c9134dfb6b7a4cb673 to your computer and use it in GitHub Desktop.
Demonstation of how sharded db can reduce load
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
function roundRobin(count, shardCount) { | |
const shards = new Array(shardCount).fill(0); | |
function hitShard(shard, key) { | |
shards[shard]++; | |
const mod = key % shards.length; | |
if (mod !== shard) { | |
hitShard(mod, key); | |
} | |
} | |
let idx = 0; | |
for (let i = 0; i < count; i++) { | |
const num = Math.round(Math.random() * 100); | |
hitShard(idx++ % shards.length, num); | |
} | |
const total = shards.reduce((accum, curr) => accum + curr, 0); | |
return { | |
shards, | |
total, | |
max: shards.reduce((last, curr) => (curr > last ? curr : last)), | |
average: total / shardCount | |
}; | |
} | |
function test(count, shardCount, iterations) { | |
const results = new Array(iterations); | |
for (let i = 0; i < iterations; i++) { | |
results[i] = roundRobin(count, shardCount); | |
} | |
return { | |
max: results.reduce((accum, curr) => accum + curr.max, 0) / iterations, | |
average: | |
results.reduce((accum, curr) => accum + curr.average, 0) / iterations, | |
total: results.reduce((accum, curr) => accum + curr.total, 0) / iterations | |
}; | |
} | |
const testCases = [[10, 1], [10, 2], [10, 5], [100, 3], [100, 5], [100, 15]]; | |
for (const testCase of testCases) { | |
console.log(`count: ${testCase[0]}`, `shardCount: ${testCase[1]}`, test(...testCase, 100)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
results