|
| 1 | +var random = require('pandemonium/random'); |
| 2 | +var Benchmark = require('benchmark') |
| 3 | +var Keymaster = require('./key-distributions.js'); |
| 4 | + |
| 5 | +var TEST_CAP = 30000 |
| 6 | + |
| 7 | +function makeStandardKeys() { |
| 8 | + var StrKeys = {} |
| 9 | + var NumKeys = {} |
| 10 | + // |
| 11 | + // 400k entries with approx 42k distinct values btwn 0 and 60k, distributed 300k/65k/23k/10k/5k/3k (~97% in the top 30k) |
| 12 | + NumKeys.gen97 = Keymaster.longTailIntGen(60000, -0.4); |
| 13 | + NumKeys.arr97 = Keymaster.longTailArr(400000, 60000, -0.4); |
| 14 | + StrKeys.arr97 = Keymaster.stringifyArr(NumKeys.arr97); |
| 15 | + StrKeys.gen97 = Keymaster.longTailStrGen(60000, -0.4); |
| 16 | + NumKeys.arr97.note = 'Long-tail pool of 42,000 distinct numeric values, 97% in the top 30k, 75% in the top 10k'; StrKeys.arr97.note = NumKeys.arr97.note.replace(/numeric/, 'string'); |
| 17 | + // |
| 18 | + // 400k entries with approx 50k distinct values btwn 0 and 60k, distributed 230k/80k/40k/22k/15k/10k (~88% in the top 30k) |
| 19 | + // var NumKeys.arr88 = Keymaster.longTailArr(400000, 60000, -0.7) |
| 20 | + // |
| 21 | + // 400k entries with approx 60k distinct values btwn 0 and 60k, distributed 135k/85k/61k/48k/39k/33k (~70% in the top 30k) |
| 22 | + NumKeys.gen70 = Keymaster.longTailIntGen(60000, -10); |
| 23 | + NumKeys.arr70 = Keymaster.longTailArr(400000, 60000, -10); |
| 24 | + StrKeys.arr70 = Keymaster.stringifyArr(NumKeys.arr70); |
| 25 | + StrKeys.gen70 = Keymaster.longTailStrGen(60000, -10); |
| 26 | + NumKeys.arr70.note = 'Long-tail pool of ~60,000 distinct numeric values, 70% in the top 30k, 33% in the top 10k'; StrKeys.arr70.note = NumKeys.arr70.note.replace(/numeric/, 'string'); |
| 27 | + // |
| 28 | + // 120k entries with approx 52k distinct values btwn 0 and 60k, distributed evenly |
| 29 | + NumKeys.arrFlat = Keymaster.flatDistArr(120000, 60000); |
| 30 | + StrKeys.arrFlat = Keymaster.stringifyArr(NumKeys.arrFlat); |
| 31 | + // |
| 32 | + // 31k entries running 0-31k in order |
| 33 | + NumKeys.arrOrd = Keymaster.ascendingArr(31000, 31000); |
| 34 | + StrKeys.arrOrd = Keymaster.stringifyArr(NumKeys.arrOrd); |
| 35 | + // |
| 36 | + return { StrKeys, NumKeys } |
| 37 | +} |
| 38 | + |
| 39 | +function read1(cache, arrA) { |
| 40 | + var count = arrA.length; |
| 41 | + for (var ii = 0; ii < count; ii++) { |
| 42 | + cache.get(arrA[ii % arrA.length]) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function readFetch(cache, arrA, rng) { |
| 47 | + var count = arrA.length; |
| 48 | + for (var ii = 0; ii < count; ii++) { |
| 49 | + var keyA = arrA[ii % arrA.length]; |
| 50 | + var result = cache.get(keyA); |
| 51 | + if (! result) { |
| 52 | + cache.set(keyA, rng()); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function readFetch2(cache, [arrA, arrB], rng) { |
| 58 | + var count = arrA.length; |
| 59 | + for (var ii = 0; ii < count; ii++) { |
| 60 | + var keyA = arrA[ii % arrA.length]; |
| 61 | + var keyB = arrB[ii % arrB.length]; |
| 62 | + var result; |
| 63 | + result = cache.get(keyA) |
| 64 | + if (! result) { cache.set(keyA, rng()); } |
| 65 | + result = cache.get(keyB) |
| 66 | + if (! result) { cache.set(keyB, rng()); } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function write1(cache, arrA) { |
| 71 | + var count = arrA.length; |
| 72 | + for (var ii = 0; ii < count; ii++) { |
| 73 | + var storeme = arrA[ii % arrA.length] |
| 74 | + cache.set(storeme, storeme) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function write1Read1(cache, [arrA, arrB], count) { |
| 79 | + var blen = arrB.length; |
| 80 | + if (! count) { count = arrA.length; } |
| 81 | + for (var ii = 0; ii < count; ii++) { |
| 82 | + var storeme = arrA[ii % arrA.length] |
| 83 | + cache.set(storeme, storeme) |
| 84 | + cache.get(arrB[ii % blen]) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function write1Read4(cache, [arrA, arrB], count) { |
| 89 | + var blen = arrB.length; |
| 90 | + var boff0 = 0, boff1 = blen * 0.25, boff2 = blen * 0.50, boff3 = blen * 0.75; |
| 91 | + if (! count) { count = arrA.length; } |
| 92 | + for (var ii = 0; ii < count; ii++) { |
| 93 | + var storeme = arrA[ii % arrA.length] |
| 94 | + cache.set(storeme, storeme) |
| 95 | + cache.get(arrB[(ii + boff0) % blen]) |
| 96 | + cache.get(arrB[(ii + boff1) % blen]) |
| 97 | + cache.get(arrB[(ii + boff2) % blen]) |
| 98 | + cache.get(arrB[(ii + boff3) % blen]) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function writeSome(cache, arrA, frac = 0.2) { |
| 103 | + var count = arrA.length; |
| 104 | + for (var ii = 0; ii < count; ii++) { |
| 105 | + if (Math.random() > frac) { continue; } |
| 106 | + var storeme = arrA[ii % arrA.length]; |
| 107 | + cache.set(storeme, storeme); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function delete1(cache, [arrA], count) { |
| 112 | + if (! count) { count = arrA.length; } |
| 113 | + for (var ii = 0; ii < count; ii++) { |
| 114 | + var delme = arrA[ii % arrA.length] |
| 115 | + cache.delete(delme, delme) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function makeLoadedCaches(CacheFactories, arrA, count, capacity = TEST_CAP, options) { |
| 120 | + var caches = CacheFactories.map((CacheFactory) => makeLoadedCache(CacheFactory, arrA, count, capacity, options)); |
| 121 | + caches.note = `${capacity}-capacity caches${arrA.note ? ' preloaded with ' + arrA.note : ''}` |
| 122 | + return caches |
| 123 | +} |
| 124 | + |
| 125 | +function makeCaches(CacheFactories, capacity = TEST_CAP, options = {}) { |
| 126 | + var caches = CacheFactories.map((CacheFactory) => { |
| 127 | + var cache = new CacheFactory(null, null, capacity, options); |
| 128 | + cache.name = CacheFactory.name; |
| 129 | + return cache; |
| 130 | + }) |
| 131 | + caches.note = `${capacity}-capacity caches` |
| 132 | + return caches |
| 133 | +} |
| 134 | + |
| 135 | +function makeLoadedCache(CacheFactory, arrA, count, capacity = TEST_CAP, options) { |
| 136 | + if (! count) { count = arrA.length; } |
| 137 | + var cache = new CacheFactory(null, null, capacity, options); |
| 138 | + cache.name = CacheFactory.name; |
| 139 | + write1(cache, arrA, count); |
| 140 | + var capK = Math.round(capacity / 1000); |
| 141 | + cache.note = `Pre-loaded ${cache.name}@${capK}k`; |
| 142 | + return cache; |
| 143 | +} |
| 144 | + |
| 145 | +function times(count, func, ...args) { |
| 146 | + for (var ii = 0; ii < count; ii++) { |
| 147 | + func(ii, count, ...args); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +async function promisedTimes(count, func, ...args) { |
| 152 | + var results = []; |
| 153 | + for (var ii = 0; ii < count; ii++) { |
| 154 | + var result = await func(ii, count, ...args); |
| 155 | + results.push(result); |
| 156 | + } |
| 157 | + return Promise.all(results); |
| 158 | +} |
| 159 | + |
| 160 | +function round(val, decimals) { |
| 161 | + chunk = Math.round(Math.pow(10, decimals)); |
| 162 | + return Math.round(val * chunk) / chunk; |
| 163 | +} |
| 164 | + |
| 165 | +function sleep(millis) { return new Promise((yay) => setTimeout(yay, millis)); } |
| 166 | + |
| 167 | +module.exports = { |
| 168 | + read1, readFetch, write1, write1Read1, write1Read4, delete1, writeSome, |
| 169 | + makeStandardKeys, makeLoadedCaches, makeLoadedCache, makeCaches, |
| 170 | + times, promisedTimes, round, sleep, |
| 171 | +} |
0 commit comments