1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var loki = require('../src/lokijs.js'),
|
22 | db = new loki('binary index perf'),
|
23 | samplecoll = null,
|
24 | uniquecoll = null,
|
25 | arraySize = 5000,
|
26 | totalIterations = 20000,
|
27 | results = [],
|
28 | getIterations = 2000000;
|
29 |
|
30 | function genRandomVal() {
|
31 | var text = "";
|
32 | var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
33 |
|
34 | for (var i = 0; i < 20; i++)
|
35 | text += possible.charAt(Math.floor(Math.random() * possible.length));
|
36 |
|
37 | return text;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function createDatabaseUnindexed() {
|
46 | db = new loki('binary index perf');
|
47 |
|
48 | samplecoll = db.addCollection('samplecoll');
|
49 | }
|
50 |
|
51 | function createDatabaseIndexed(adaptive) {
|
52 | db = new loki('binary index perf');
|
53 |
|
54 | samplecoll = db.addCollection('samplecoll', {
|
55 | adaptiveBinaryIndices: (adaptive === true),
|
56 | indices: ['customId']
|
57 | });
|
58 | }
|
59 |
|
60 |
|
61 | function initializeDatabase(silent, docCount) {
|
62 | var start, end, totalTime;
|
63 | var totalTimes = [];
|
64 | var totalMS = 0.0;
|
65 |
|
66 | if (typeof docCount === "undefined") {
|
67 | docCount = 5000;
|
68 | }
|
69 |
|
70 | arraySize = docCount;
|
71 |
|
72 | for (var idx = 0; idx < arraySize; idx++) {
|
73 | var v1 = genRandomVal();
|
74 | var v2 = genRandomVal();
|
75 |
|
76 | start = process.hrtime();
|
77 | samplecoll.insert({
|
78 | customId: idx,
|
79 | val: v1,
|
80 | val2: v2,
|
81 | val3: "more data 1234567890"
|
82 | });
|
83 | end = process.hrtime(start);
|
84 | totalTimes.push(end);
|
85 | }
|
86 |
|
87 | if (silent === true) {
|
88 | return;
|
89 | }
|
90 |
|
91 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
92 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
93 | }
|
94 |
|
95 |
|
96 | start = process.hrtime();
|
97 | samplecoll.find({customIdx: 50});
|
98 | end = process.hrtime(start);
|
99 | totalTimes.push(end);
|
100 |
|
101 |
|
102 | totalMS = totalMS.toFixed(2);
|
103 | var rate = arraySize * 1000 / totalMS;
|
104 | rate = rate.toFixed(2);
|
105 | console.log("load (individual inserts) : " + totalMS + "ms (" + rate + ") ops/s (" + arraySize + " documents)");
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | function initializeDatabaseBatch(silent, docCount) {
|
111 | var start, end, totalTime;
|
112 | var totalTimes = [];
|
113 | var totalMS = 0.0;
|
114 | var batch = [];
|
115 |
|
116 | if (typeof docCount === "undefined") {
|
117 | docCount = 5000;
|
118 | }
|
119 |
|
120 | arraySize = docCount;
|
121 |
|
122 | for (var idx = 0; idx < arraySize; idx++) {
|
123 | var v1 = genRandomVal();
|
124 | var v2 = genRandomVal();
|
125 |
|
126 | batch.push({
|
127 | customId: idx,
|
128 | val: v1,
|
129 | val2: v2,
|
130 | val3: "more data 1234567890"
|
131 | });
|
132 | }
|
133 |
|
134 | start = process.hrtime();
|
135 | samplecoll.insert(batch);
|
136 | end = process.hrtime(start);
|
137 | totalTimes.push(end);
|
138 |
|
139 | if (silent === true) {
|
140 | return;
|
141 | }
|
142 |
|
143 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
144 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
145 | }
|
146 |
|
147 |
|
148 | totalMS = totalMS.toFixed(2);
|
149 | var rate = arraySize* 1000 / totalMS;
|
150 | rate = rate.toFixed(2);
|
151 | console.log("load (batch insert) : " + totalMS + "ms (" + rate + ") ops/s (" + arraySize + " documents)");
|
152 | }
|
153 |
|
154 | function perfFind(multiplier) {
|
155 | var start, end;
|
156 | var totalTimes = [];
|
157 | var totalMS = 0;
|
158 |
|
159 | var loopIterations = arraySize;
|
160 | if (typeof (multiplier) != "undefined") {
|
161 | loopIterations = loopIterations * multiplier;
|
162 | }
|
163 |
|
164 | for (var idx = 0; idx < loopIterations; idx++) {
|
165 | var customidx = Math.floor(Math.random() * arraySize) + 1;
|
166 |
|
167 | start = process.hrtime();
|
168 | var results = samplecoll.find({
|
169 | 'customId': customidx
|
170 | });
|
171 | end = process.hrtime(start);
|
172 | totalTimes.push(end);
|
173 | }
|
174 |
|
175 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
176 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
177 | }
|
178 |
|
179 | totalMS = totalMS.toFixed(2);
|
180 | var rate = loopIterations * 1000 / totalMS;
|
181 | rate = rate.toFixed(2);
|
182 | console.log("contiguous coll.find() : " + totalMS + "ms (" + rate + " ops/s) " + loopIterations + " iterations");
|
183 | }
|
184 |
|
185 |
|
186 | function perfFindInterlacedInserts(multiplier) {
|
187 | var start, end;
|
188 | var totalTimes = [];
|
189 | var totalMS = 0;
|
190 |
|
191 | var loopIterations = arraySize;
|
192 | if (typeof (multiplier) != "undefined") {
|
193 | loopIterations = loopIterations * multiplier;
|
194 | }
|
195 |
|
196 | for (var idx = 0; idx < loopIterations; idx++) {
|
197 | var customidx = Math.floor(Math.random() * arraySize) + 1;
|
198 |
|
199 | start = process.hrtime();
|
200 | var results = samplecoll.find({
|
201 | 'customId': customidx
|
202 | });
|
203 |
|
204 | var obj = samplecoll.insert({
|
205 | customId: 999,
|
206 | val: 999,
|
207 | val2: 999,
|
208 | val3: "more data 1234567890"
|
209 | });
|
210 |
|
211 | end = process.hrtime(start);
|
212 | totalTimes.push(end);
|
213 |
|
214 | }
|
215 |
|
216 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
217 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
218 | }
|
219 |
|
220 | totalMS = totalMS.toFixed(2);
|
221 | var rate = loopIterations * 1000 / totalMS;
|
222 | rate = rate.toFixed(2);
|
223 | console.log("interlaced inserts + coll.find() : " + totalMS + "ms (" + rate + " ops/s) " + loopIterations + " iterations");
|
224 |
|
225 | }
|
226 |
|
227 |
|
228 | function perfFindInterlacedRemoves() {
|
229 | var start, end;
|
230 | var totalTimes = [];
|
231 | var totalMS = 0;
|
232 |
|
233 | for (var idx = 0; idx < arraySize; idx++) {
|
234 |
|
235 |
|
236 | start = process.hrtime();
|
237 | var result = samplecoll.findOne({
|
238 | 'customId': idx
|
239 | });
|
240 |
|
241 | samplecoll.remove(result);
|
242 |
|
243 | end = process.hrtime(start);
|
244 | totalTimes.push(end);
|
245 | }
|
246 |
|
247 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
248 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
249 | }
|
250 |
|
251 | totalMS = totalMS.toFixed(2);
|
252 | var rate = arraySize * 1000 / totalMS;
|
253 | rate = rate.toFixed(2);
|
254 | console.log("interlaced removes + coll.find() : " + totalMS + "ms (" + rate + " ops/s) " + arraySize + " iterations");
|
255 | }
|
256 |
|
257 |
|
258 | function perfFindInterlacesUpdates() {
|
259 | var start, end;
|
260 | var totalTimes = [];
|
261 | var totalMS = 0;
|
262 |
|
263 | for (var idx = 0; idx < arraySize; idx++) {
|
264 |
|
265 |
|
266 | start = process.hrtime();
|
267 | var results = samplecoll.find({
|
268 | 'customId': idx
|
269 | });
|
270 |
|
271 | samplecoll.update(results[0]);
|
272 |
|
273 | end = process.hrtime(start);
|
274 | totalTimes.push(end);
|
275 | }
|
276 |
|
277 | for (var idx = 0; idx < totalTimes.length; idx++) {
|
278 | totalMS += totalTimes[idx][0] * 1e3 + totalTimes[idx][1] / 1e6;
|
279 | }
|
280 |
|
281 | totalMS = totalMS.toFixed(2);
|
282 | var rate = arraySize * 1000 / totalMS;
|
283 | rate = rate.toFixed(2);
|
284 | console.log("interlaced updates + coll.find() : " + totalMS + "ms (" + rate + " ops/s) " + arraySize + " iterations");
|
285 | }
|
286 |
|
287 | console.log("");
|
288 | console.log("Perf: Unindexed Inserts---------------------");
|
289 | createDatabaseUnindexed();
|
290 | initializeDatabase(false, 100000);
|
291 |
|
292 | createDatabaseUnindexed();
|
293 | initializeDatabaseBatch(false, 100000);
|
294 |
|
295 | console.log("");
|
296 | console.log("Perf: Indexed Inserts (Lazy) ------------------------");
|
297 | createDatabaseIndexed();
|
298 | initializeDatabase(false, 100000);
|
299 |
|
300 | createDatabaseIndexed();
|
301 | initializeDatabaseBatch(false, 100000);
|
302 |
|
303 | console.log("");
|
304 | console.log("Perf: Indexed Inserts (Adaptive) ------------------------");
|
305 | createDatabaseIndexed(true);
|
306 | initializeDatabase(false, 100000);
|
307 |
|
308 | createDatabaseIndexed(true);
|
309 | initializeDatabaseBatch(false, 100000);
|
310 |
|
311 | console.log("");
|
312 | console.log("Perf: Unindexed finds ------------------------");
|
313 |
|
314 | createDatabaseUnindexed();
|
315 | initializeDatabase(true, 10000);
|
316 | perfFind();
|
317 |
|
318 | createDatabaseUnindexed();
|
319 | initializeDatabase(true, 10000);
|
320 | perfFindInterlacedInserts();
|
321 |
|
322 | createDatabaseUnindexed();
|
323 | initializeDatabase(true, 10000);
|
324 | perfFindInterlacedRemoves();
|
325 |
|
326 | createDatabaseUnindexed();
|
327 | initializeDatabase(true, 10000);
|
328 | perfFindInterlacesUpdates();
|
329 |
|
330 | console.log("");
|
331 | console.log("Perf: Indexed finds (Nightmare Lazy Index Thrashing Test) ------");
|
332 |
|
333 | createDatabaseIndexed(false);
|
334 | initializeDatabase(true, 80000);
|
335 | perfFind();
|
336 |
|
337 | createDatabaseIndexed(false);
|
338 | initializeDatabase(true, 3000);
|
339 | perfFindInterlacedInserts(1);
|
340 |
|
341 | createDatabaseIndexed(false);
|
342 | initializeDatabase(true, 3000);
|
343 | perfFindInterlacedRemoves();
|
344 |
|
345 | createDatabaseIndexed(false);
|
346 | initializeDatabase(true, 3000);
|
347 | perfFindInterlacesUpdates();
|
348 |
|
349 | console.log("");
|
350 | console.log("Perf: Indexed finds (Nightmare Adaptive Index Thrashing Test) ---");
|
351 |
|
352 | createDatabaseIndexed(true);
|
353 | initializeDatabase(true, 80000);
|
354 | perfFind();
|
355 |
|
356 | createDatabaseIndexed(true);
|
357 | initializeDatabase(true, 10000);
|
358 | perfFindInterlacedInserts(1);
|
359 |
|
360 | createDatabaseIndexed(true);
|
361 | initializeDatabase(true, 10000);
|
362 | perfFindInterlacedRemoves();
|
363 |
|
364 | createDatabaseIndexed(true);
|
365 | initializeDatabase(true, 10000);
|
366 | perfFindInterlacesUpdates(); |
\ | No newline at end of file |