UNPKG

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