|
| 1 | +/** |
| 2 | + * This stress test is designed to test effectiveness of throttled saves in |
| 3 | + * worst-case scenarios. |
| 4 | + * |
| 5 | + * In order to stress overlapping saves we will use an async adapter along |
| 6 | + * with synchronous logic with many calls to save in which we do not wait |
| 7 | + * for the save to complete before attempting to save again. This usage |
| 8 | + * pattern is not recommended but lokijs throttled saves is intended to |
| 9 | + * safeguard against it. |
| 10 | + * |
| 11 | + * The test will verify that nothing is lost when this usage pattern is |
| 12 | + * used by comparing the database when we are done with the copy in memory. |
| 13 | + * |
| 14 | + * We are forced to consider async adapter behavior on final save and reload, |
| 15 | + * since throttled saves protect only within a single loki object instance. |
| 16 | + * You must still wait after for the throttled queue to drain after finishing |
| 17 | + * before you can attempt to reload it and you must wait for the database |
| 18 | + * to finish loading before you can access its contents. |
| 19 | + */ |
| 20 | + |
| 21 | +var crypto = require("crypto"); // for random string generation |
| 22 | +var loki = require('../src/lokijs.js'); |
| 23 | + |
| 24 | +const INITIALCOUNT = 2000; |
| 25 | +const ITERATIONS = 2000; |
| 26 | +const RANGE = 1000; |
| 27 | + |
| 28 | +// synchronous adapter using LokiMemoryAdapter |
| 29 | +var memAdapterSync = new loki.LokiMemoryAdapter(); |
| 30 | + |
| 31 | +// simulate async adapter with 100ms save/load times |
| 32 | +var memAdapterAsync = new loki.LokiMemoryAdapter({ |
| 33 | + asyncResponses: true, |
| 34 | + asyncTimeout: 100 |
| 35 | +}); |
| 36 | + |
| 37 | +var db, db2; |
| 38 | +var maxThrottledCalls = 0; |
| 39 | + |
| 40 | +// less memory 'leaky' way to generate random strings (node specific) |
| 41 | +function genRandomString() { |
| 42 | + return crypto.randomBytes(50).toString('hex'); |
| 43 | +} |
| 44 | + |
| 45 | +function genRandomObject() { |
| 46 | + var av = Math.floor(Math.random() * RANGE); |
| 47 | + var cv = Math.floor(Math.random() * RANGE); |
| 48 | + |
| 49 | + return { "a": av, "b": genRandomString(), "c": cv }; |
| 50 | +} |
| 51 | + |
| 52 | +function setupDatabaseSync() { |
| 53 | + var newDatabase = new loki("throttle-test.db", { adapter: memAdapterSync }); |
| 54 | + |
| 55 | + // since our memory adapter is by default synchronous (unless simulating async), |
| 56 | + // we can assume any load will complete before our next statement executes. |
| 57 | + newDatabase.loadDatabase(); |
| 58 | + |
| 59 | + // initialize collections |
| 60 | + if (!newDatabase.getCollection("items")) { |
| 61 | + newDatabase.addCollection("items"); |
| 62 | + } |
| 63 | + |
| 64 | + return newDatabase; |
| 65 | +} |
| 66 | + |
| 67 | +function setupDatabaseAsync(callback) { |
| 68 | + var newDatabase = new loki("throttle-test.db", { adapter: memAdapterAsync }); |
| 69 | + |
| 70 | + // database won't exist on first pass, but let's use forced |
| 71 | + // async syntax in case is did |
| 72 | + newDatabase.loadDatabase({}, function(err) { |
| 73 | + if (err) { |
| 74 | + callback(err); |
| 75 | + } |
| 76 | + |
| 77 | + // initialize collections |
| 78 | + if (!newDatabase.getCollection("items")) { |
| 79 | + newDatabase.addCollection("items"); |
| 80 | + |
| 81 | + // bad practice, stress test |
| 82 | + newDatabase.saveDatabase(); |
| 83 | + } |
| 84 | + |
| 85 | + callback(err); |
| 86 | + }); |
| 87 | + |
| 88 | + return newDatabase; |
| 89 | +} |
| 90 | + |
| 91 | +function performStressedOps() { |
| 92 | + var items = db.getCollection("items"); |
| 93 | + var idx, op; |
| 94 | + |
| 95 | + for(idx=0;idx<INITIALCOUNT;idx++) { |
| 96 | + items.insert(genRandomObject()); |
| 97 | + |
| 98 | + // bad practice, stress test |
| 99 | + db.saveDatabase(); |
| 100 | + |
| 101 | + if (db.throttledCallbacks.length > maxThrottledCalls) { |
| 102 | + maxThrottledCalls = db.throttledCallbacks.length; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for(idx=0;idx<ITERATIONS;idx++) { |
| 107 | + // randomly determine if this permutation will be insert/update/remove |
| 108 | + op = Math.floor(Math.random() * 3); |
| 109 | + switch(op) { |
| 110 | + // insert |
| 111 | + case 0: items.insert(genRandomObject()); |
| 112 | + break; |
| 113 | + // update |
| 114 | + case 1: rnd = Math.floor(Math.random() * RANGE); |
| 115 | + items.chain().find({a:rnd}).update(function(obj) { |
| 116 | + obj.a = Math.floor(Math.random() * RANGE); |
| 117 | + obj.c = Math.floor(Math.random() * RANGE); |
| 118 | + obj.b = genRandomString(); |
| 119 | + }); |
| 120 | + break; |
| 121 | + // remove 2 matches of a single value in our range |
| 122 | + case 2: rnd = Math.floor(Math.random() * RANGE); |
| 123 | + items.chain().find({a:rnd}).limit(1).remove(); |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + db.saveDatabase(); |
| 128 | + if (db.throttledCallbacks.length > maxThrottledCalls) { |
| 129 | + maxThrottledCalls = db.throttledCallbacks.length; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function compareDatabases() { |
| 135 | + var c1 = db.getCollection("items"); |
| 136 | + var c2 = db2.getCollection("items"); |
| 137 | + var idx; |
| 138 | + |
| 139 | + var count = c1.count(); |
| 140 | + if (count !== c2.count()) return false; |
| 141 | + |
| 142 | + for(idx=0; idx < count; idx++) { |
| 143 | + if (c1.data[idx].a !== c2.data[idx].a) return false; |
| 144 | + if (c1.data[idx].b !== c2.data[idx].b) return false; |
| 145 | + if (c1.data[idx].c !== c2.data[idx].c) return false; |
| 146 | + if (c1.data[idx].$loki !== c2.data[idx].$loki) return false; |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +console.log(""); |
| 153 | + |
| 154 | +// let's test in truly sync manner |
| 155 | +var start = process.hrtime(); |
| 156 | +db = setupDatabaseSync(); |
| 157 | +performStressedOps(); |
| 158 | +db.saveDatabase(); |
| 159 | +db2 = setupDatabaseSync(); |
| 160 | +var end = process.hrtime(start); |
| 161 | +var result = compareDatabases(); |
| 162 | +console.log("## Fully synchronous operations with excessive saving after each operation ##"); |
| 163 | +console.log("Database are " + (result?"the same.":"NOT the same!")); |
| 164 | +console.log("Execution time (hr): %ds %dms", end[0], end[1]/1000000); |
| 165 | +console.log("maxThrottledCalls: " + maxThrottledCalls); |
| 166 | + |
| 167 | +console.log(""); |
| 168 | + |
| 169 | +// now let's test with simulated async adpater |
| 170 | +// first pass setup will create in memory |
| 171 | +start = process.hrtime(); |
| 172 | +db = setupDatabaseAsync(function() { |
| 173 | + performStressedOps(); |
| 174 | + |
| 175 | + // go ahead and do a final save (even though we save after every op) |
| 176 | + // and then wait for queue to drain before trying to reload it. |
| 177 | + db.saveDatabase(); |
| 178 | + db.throttledSaveDrain(function () { |
| 179 | + db2 = setupDatabaseAsync(function () { |
| 180 | + var end = process.hrtime(start); |
| 181 | + var result = compareDatabases(); |
| 182 | + |
| 183 | + console.log("## Asynchronous operations with excessive saving after each operation ##"); |
| 184 | + console.log("Async database are " + (result?"the same.":"NOT the same!")); |
| 185 | + console.log("Execution time (hr): %ds %dms", end[0], end[1]/1000000); |
| 186 | + console.log("maxThrottledCalls: " + maxThrottledCalls); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
0 commit comments