UNPKG

9.8 kBJavaScriptView Raw
1// var loki = require('../src/lokijs.js'),
2var db = new loki('perftest'),
3 samplecoll = null,
4 arraySize = 5000, // how large of a dataset to generate
5 totalIterations = 20000, // how many times we search it
6 results = [],
7 getIterations = 2000000; // get is crazy fast due to binary search so this needs separate scale
8
9var domElement = null;
10
11
12function genRandomVal()
13{
14 var text = "";
15 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
16
17 for( var i=0; i < 20; i++ )
18 text += possible.charAt(Math.floor(Math.random() * possible.length));
19
20 return text;
21}
22
23// in addition to the loki id we will create a key of our own
24// (customId) which is number from 1- totalIterations
25// we will later perform find() queries against customId with and
26// without an index
27
28function initializeDB() {
29 db = new loki('perftest');
30
31 var start, end, totalTime;
32 var totalTimes = [];
33 var totalMS = 0.0;
34
35 samplecoll = db.addCollection('samplecoll');
36
37 for (var idx=0; idx < arraySize; idx++) {
38 var v1 = genRandomVal();
39 var v2 = genRandomVal();
40
41 start = performance.now();
42 samplecoll.insert({
43 customId: idx,
44 val: v1,
45 val2: v2,
46 val3: "more data 1234567890"
47 });
48 end = performance.now() - start;
49 totalTimes.push([0, end * 1e6]);
50 }
51
52 for(idx=0; idx < totalTimes.length; idx++) {
53 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
54 }
55
56 totalMS = totalMS.toFixed(2);
57 var rate = arraySize * 1000 / totalMS;
58 rate = rate.toFixed(2);
59 trace("load (insert) : " + totalMS + "ms (" + rate + ") ops/s");
60}
61
62/**
63 * initializeUnique : to support benchUniquePerf, we will set up another collection
64 * where our customId is enforced as 'unique' using unique index feature of loki.
65 */
66function initializeUnique() {
67 uniquecoll = db.addCollection('uniquecoll', {
68 unique: ['customId']
69 });
70
71 for (var idx = 0; idx < arraySize; idx++) {
72 var v1 = genRandomVal();
73 var v2 = genRandomVal();
74
75 uniquecoll.insert({
76 customId: (arraySize - idx),
77 val: v1,
78 val2: v2,
79 val3: "more data 1234567890"
80 });
81 }
82}
83
84/**
85 * initializeWithEval : repeat of insert bench with a dynamic view registered.
86 * All inserts will be passed into the view's evaluateDocument() method.
87 * This test is an attempt to gauge the level of impact of that overhead.
88 */
89function initializeWithEval() {
90 var dbTest = new loki('perfInsertWithEval');
91
92 var start, end, totalTime;
93 var totalTimes = [];
94 var totalMS = 0.0;
95
96 var coll = dbTest.addCollection('samplecoll',
97 {
98 indices: ['customId'],
99 asyncListeners: false,
100 disableChangesApi: true,
101 transactional: false,
102 clone: false
103 }
104 );
105
106 var dv = coll.addDynamicView('test');
107 dv.applyFind({'customId': {'$lt': arraySize/4 }});
108
109 for (var idx=0; idx < arraySize; idx++) {
110 var v1 = genRandomVal();
111 var v2 = genRandomVal();
112
113 start = performance.now();
114 coll.insert({
115 customId: idx,
116 val: v1,
117 val2: v2,
118 val3: "more data 1234567890"
119 });
120 end = performance.now() - start;
121 totalTimes.push([0, end * 1e6]);
122 }
123
124 for(idx=0; idx < totalTimes.length; idx++) {
125 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
126 }
127
128 totalMS = totalMS.toFixed(2);
129 var rate = arraySize * 1000 / totalMS;
130 rate = rate.toFixed(2);
131 trace("load (insert with dynamic view registered) : " + totalMS + "ms (" + rate + ") ops/s");
132}
133
134function benchGet() {
135 var start, end;
136 var totalTimes = [];
137 var totalMS = 0.0;
138
139 for (var idx=0; idx < getIterations; idx++) {
140 var customidx = Math.floor(Math.random() * arraySize) + 1;
141
142 start = performance.now();
143 var results = samplecoll.get(customidx);
144 end = performance.now() - start;
145 totalTimes.push([0, end * 1e6]);
146 }
147
148 for(idx=0; idx < totalTimes.length; idx++) {
149 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
150 }
151
152 totalMS = totalMS.toFixed(2);
153 var rate = getIterations * 1000 / totalMS;
154 rate = rate.toFixed(2);
155 trace("coll.get() : " + totalMS + "ms (" + rate + ") ops/s");
156}
157
158function benchUniquePerf() {
159 var start, end;
160 var totalTimes = [];
161 var totalMS = 0.0;
162
163 for (var idx = 0; idx < getIterations; idx++) {
164 var customidx = Math.floor(Math.random() * arraySize) + 1;
165
166 start = performance.now();
167 var results = uniquecoll.by('customId', customidx);
168 end = performance.now() - start;
169 totalTimes.push([0, end * 1e6]);
170 }
171
172 for (idx = 0; idx < totalTimes.length; idx++) {
173 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
174 }
175
176 totalMS = totalMS.toFixed(2);
177 var rate = getIterations * 1000 / totalMS;
178 rate = rate.toFixed(2);
179 trace("coll.by() : " + totalMS + "ms (" + rate + ") ops/s");
180}
181
182function benchFind(multiplier) {
183 var start, end;
184 var totalTimes = [];
185 var totalMS = 0;
186
187 var loopIterations = totalIterations;
188 if (typeof(multiplier) != "undefined") {
189 loopIterations = loopIterations * multiplier;
190 }
191
192 for (var idx=0; idx < loopIterations; idx++) {
193 var customidx = Math.floor(Math.random() * arraySize) + 1;
194
195 start = performance.now();
196 var results = samplecoll.find({ 'customId': customidx });
197 end = performance.now() - start;
198 totalTimes.push([0, end * 1e6]);
199 }
200
201 for(idx=0; idx < totalTimes.length; idx++) {
202 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
203 }
204
205 totalMS = totalMS.toFixed(2);
206 var rate = loopIterations * 1000 / totalMS;
207 rate = rate.toFixed(2);
208 trace("coll.find() : " + totalMS + "ms (" + rate + " ops/s) " + loopIterations + " iterations");
209}
210
211function benchRS(multiplier) {
212 var start, end;
213 var totalTimes = [];
214 var totalMS = 0;
215
216 var loopIterations = totalIterations;
217 if (typeof(multiplier) != "undefined") {
218 loopIterations = loopIterations * multiplier;
219 }
220
221 for (var idx=0; idx < loopIterations; idx++) {
222 var customidx = Math.floor(Math.random() * arraySize) + 1;
223
224 start = performance.now();
225 var results = samplecoll.chain().find({ 'customId': customidx }).data();
226 end = performance.now() - start;
227 totalTimes.push([0, end * 1e6]);
228 }
229
230 for(idx=0; idx < totalTimes.length; idx++) {
231 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
232 }
233
234 totalMS = totalMS.toFixed(2);
235 var rate = loopIterations * 1000 / totalMS;
236 rate = rate.toFixed(2);
237 trace("resultset chained find() : " + totalMS + "ms (" + rate + " ops/s) " + loopIterations + " iterations");
238}
239
240function benchDV(multiplier) {
241 var start, end;
242 var start2, end2, totalTime2 = 0.0;
243 var totalTimes = [];
244 var totalTimes2 = [];
245 var totalMS = 0;
246 var totalMS2 = 0;
247 var loopIterations = totalIterations;
248
249 if (typeof(multiplier) != "undefined") {
250 loopIterations = loopIterations * multiplier;
251 }
252
253 for (var idx=0; idx < loopIterations; idx++) {
254 var customidx = Math.floor(Math.random() * arraySize) + 1;
255
256 start = performance.now();
257 var dv = samplecoll.addDynamicView("perfview");
258 dv.applyFind({ 'customId': customidx });
259 var results = dv.data();
260 end = performance.now() - start;
261 totalTimes.push([0, end * 1e6]);
262
263 // test speed of repeated query on an already set up dynamicview
264 start2 = performance.now();
265 results = dv.data();
266 end2 = performance.now() - start2;
267 totalTimes2.push([0, end2 * 1e6]);
268
269 samplecoll.removeDynamicView("perfview");
270 }
271
272 for(idx=0; idx < totalTimes.length; idx++) {
273 totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1]/1e6;
274 totalMS2 += totalTimes2[idx][0] * 1e3 + totalTimes2[idx][1]/1e6;
275 }
276
277 totalMS = totalMS.toFixed(2);
278 totalMS2 = totalMS2.toFixed(2);
279 var rate = loopIterations * 1000 / totalMS;
280 var rate2 = loopIterations * 1000 / totalMS2;
281 rate = rate.toFixed(2);
282 rate2 = rate2.toFixed(2);
283
284 trace("loki dynamic view first find : " + totalMS + "ms (" + rate + " ops/s) " + loopIterations + " iterations");
285 trace("loki dynamic view subsequent finds : " + totalMS2 + "ms (" + rate2 + " ops/s) " + loopIterations + " iterations");
286}
287
288function trace(string)
289{
290 domElement.innerHTML = domElement.innerHTML + string + "<br/>";
291}
292
293// async yielding after each step to avoid browser warnings of long running scripts
294function runStep(step)
295{
296 switch (step) {
297 case 1 :
298 trace("-- Beginning benchmark --");
299 initializeDB();
300 initializeUnique();
301 setTimeout(function() { runStep(2); }, 100);
302 break;
303 case 2 :
304 benchGet();
305 setTimeout(function() { runStep(3); }, 100);
306 break;
307 case 3 :
308 benchUniquePerf();
309 setTimeout(function() { runStep(4); }, 100);
310 trace("");
311 trace("-- Benchmarking query on non-indexed column --");
312 break;
313 case 4 :
314 benchFind(); // find benchmark on unindexed customid field
315 setTimeout(function() { runStep(5); }, 100);
316 break;
317 case 5 :
318 benchRS(); // resultset find benchmark on unindexed customid field
319 setTimeout(function() { runStep(6); }, 100);
320 break;
321 case 6 :
322 benchDV();
323 setTimeout(function() { runStep(7); }, 100);
324 trace("");
325 trace("-- Adding Binary Index to query column and repeating benchmarks --");
326 break;
327 case 7 :
328 samplecoll.ensureIndex("customId");
329 setTimeout(function() { runStep(8); }, 100);
330 break;
331 case 8 :
332 benchFind(20);
333 setTimeout(function() { runStep(9); }, 100);
334 break;
335 case 9 :
336 benchRS(15);
337 setTimeout(function() { runStep(10); }, 100);
338 break;
339 case 10 :
340 benchDV(15);
341 trace("");
342 trace("done.");
343 break;
344 }
345}
346
347function startBenchmark(divElement)
348{
349 domElement = divElement;
350
351 // async yielding after each step to avoid browser warnings of long running scripts
352 runStep(1);
353}
\No newline at end of file