UNPKG

12.1 kBJavaScriptView Raw
1describe('performance', function () {
2
3 this.timeout(5000);
4
5 var expect = require('expect.js');
6
7 var random = require('./__fixtures/random');
8
9 var PareTree = require('..');
10
11 var VERBOSE = true;
12
13 var async = require('async');
14
15 var testLog = function (message, object) {
16 if (VERBOSE) {
17 console.log(message);
18 if (object) console.log(JSON.stringify(object, null, 2));
19 }
20 };
21
22 var SUBSCRIPTION_COUNT = 1000;
23
24 var DUPLICATE_KEYS = 3;
25
26 var CLIENT_COUNT = 10;
27
28 it('adds and verifies random wildcard subscriptions, glob mode', function (done) {
29
30 this.timeout(300000);
31
32 var subscriptions = random.randomPaths({
33 duplicate: DUPLICATE_KEYS,
34 count: SUBSCRIPTION_COUNT
35 });
36
37 var clients = random.string({
38 count: CLIENT_COUNT
39 });
40
41 var subscriptionTree = new PareTree();
42
43 var inserts = 0;
44
45 var searched = 0;
46
47 var startedInsert = Date.now();
48
49 subscriptions.forEach(function (subscriptionPath) {
50
51 clients.forEach(function (sessionId) {
52
53 subscriptionPath = subscriptionPath.substring(0, subscriptionPath.length - 1) + '*';
54
55 subscriptionTree.add(subscriptionPath, {
56 key: sessionId,
57 data: {
58 test: "data"
59 }
60 });
61 inserts++;
62 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
63 });
64 });
65
66 var endedInsert = Date.now();
67
68 var startedSearches = Date.now();
69
70 var results = 0;
71
72 subscriptions.forEach(function (subscriptionPath) {
73
74 searched++;
75 results += subscriptionTree.search(subscriptionPath, {decouple:true}).length;
76 });
77
78 expect(results / (DUPLICATE_KEYS * CLIENT_COUNT)).to.be(subscriptions.length);
79
80 var endedSearches = Date.now();
81
82 testLog('did ' + inserts + ' wildcard inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
83
84 testLog('did ' + searched + ' glob searches in ' + (endedSearches - startedSearches) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
85
86 var perMillisecond = searched / (endedSearches - startedSearches);
87
88 testLog(perMillisecond + ' per millisecond');
89
90 testLog(perMillisecond * 1000 + ' per second');
91
92 return done();
93 });
94
95 it('adds and verifies random wildcard subscriptions, wildstring mode', function (done) {
96
97 this.timeout(300000);
98
99 var subscriptions = random.randomPaths({
100 duplicate: DUPLICATE_KEYS,
101 count: SUBSCRIPTION_COUNT
102 });
103
104 var clients = random.string({
105 count: CLIENT_COUNT
106 });
107
108 var subscriptionTree = new PareTree({mode:'wildstring'});
109
110 var inserts = 0;
111
112 var searched = 0;
113
114 var startedInsert = Date.now();
115
116 subscriptions.forEach(function (subscriptionPath) {
117
118 clients.forEach(function (sessionId) {
119
120 subscriptionPath = subscriptionPath.substring(0, subscriptionPath.length - 1) + '*';
121
122 subscriptionTree.add(subscriptionPath, {
123 key: sessionId,
124 data: {
125 test: "data"
126 }
127 });
128 inserts++;
129 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
130 });
131 });
132
133 var endedInsert = Date.now();
134
135 var startedSearches = Date.now();
136
137 var results = 0;
138
139 subscriptions.forEach(function (subscriptionPath) {
140
141 searched++;
142 results += subscriptionTree.search(subscriptionPath, {decouple:true}).length;
143 });
144
145 expect(results / (DUPLICATE_KEYS * CLIENT_COUNT)).to.be(subscriptions.length);
146
147 var endedSearches = Date.now();
148
149 testLog('did ' + inserts + ' wildcard inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
150
151 testLog('did ' + searched + ' wildcard searches in ' + (endedSearches - startedSearches) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
152
153 var perMillisecond = searched / (endedSearches - startedSearches);
154
155 testLog(perMillisecond + ' per millisecond');
156
157 testLog(perMillisecond * 1000 + ' per second');
158
159 return done();
160 });
161
162 it('adds and verifies random precise subscriptions, non-wildcard query path', function (done) {
163
164 this.timeout(300000);
165
166 var subscriptions = random.randomPaths({
167 duplicate: DUPLICATE_KEYS,
168 count: SUBSCRIPTION_COUNT
169 });
170
171 var clients = random.string({
172 count: CLIENT_COUNT
173 });
174
175 var subscriptionTree = new PareTree();
176
177 var inserts = 0;
178
179 var searched = 0;
180
181 var startedInsert = Date.now();
182
183 subscriptions.forEach(function (subscriptionPath) {
184
185 clients.forEach(function (sessionId) {
186
187 subscriptionTree.add(subscriptionPath, {
188 key: sessionId,
189 data: {
190 test: "data"
191 }
192 });
193 inserts++;
194 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
195 });
196 });
197
198 var endedInsert = Date.now();
199
200 var startedSearches = Date.now();
201
202 var results = 0;
203
204 subscriptions.forEach(function (subscriptionPath) {
205
206 searched++;
207 results += subscriptionTree.search(subscriptionPath, {decouple:true}).length;
208 });
209
210 expect(results / (DUPLICATE_KEYS * CLIENT_COUNT)).to.be(subscriptions.length);
211
212 var endedSearches = Date.now();
213
214 testLog('did ' + inserts + ' precise inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
215
216 testLog('did ' + searched + ' precise searches in ' + (endedSearches - startedSearches) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
217
218 var perMillisecond = searched / (endedSearches - startedSearches);
219
220 testLog(perMillisecond + ' per millisecond');
221
222 testLog(perMillisecond * 1000 + ' per second');
223
224 done();
225 });
226
227 it('adds and verifies random precise subscriptions, non-wildcard query path, wildstring mode', function (done) {
228
229 this.timeout(300000);
230
231 var subscriptions = random.randomPaths({
232 duplicate: DUPLICATE_KEYS,
233 count: SUBSCRIPTION_COUNT
234 });
235
236 var clients = random.string({
237 count: CLIENT_COUNT
238 });
239
240 var subscriptionTree = new PareTree({mode:'wildstring'});
241
242 var inserts = 0;
243
244 var searched = 0;
245
246 var startedInsert = Date.now();
247
248 subscriptions.forEach(function (subscriptionPath) {
249
250 clients.forEach(function (sessionId) {
251
252 subscriptionTree.add(subscriptionPath, {
253 key: sessionId,
254 data: {
255 test: "data"
256 }
257 });
258 inserts++;
259 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
260 });
261 });
262
263 var endedInsert = Date.now();
264
265 var startedSearches = Date.now();
266
267 var results = 0;
268
269 subscriptions.forEach(function (subscriptionPath) {
270
271 searched++;
272 results += subscriptionTree.search(subscriptionPath, {decouple:true}).length;
273 });
274
275 expect(results / (DUPLICATE_KEYS * CLIENT_COUNT)).to.be(subscriptions.length);
276
277 var endedSearches = Date.now();
278
279 testLog('did ' + inserts + ' precise inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
280
281 testLog('did ' + searched + ' precise searches in ' + (endedSearches - startedSearches) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
282
283 var perMillisecond = searched / (endedSearches - startedSearches);
284
285 testLog(perMillisecond + ' per millisecond');
286
287 testLog(perMillisecond * 1000 + ' per second');
288
289 done();
290 });
291
292 it('adds, verifies and removes by path random precise subscriptions', function (done) {
293
294 this.timeout(300000);
295
296 var subscriptions = random.randomPaths({
297 count: SUBSCRIPTION_COUNT
298 });
299
300 var clients = random.string({
301 count: CLIENT_COUNT
302 });
303
304 var subscriptionTree = new PareTree();
305
306 var inserts = 0;
307
308 var searched = 0;
309
310 var startedInsert = Date.now();
311
312 subscriptions.forEach(function (subscriptionPath) {
313
314 clients.forEach(function (sessionId) {
315
316 subscriptionTree.add(subscriptionPath, {
317 key: sessionId,
318 data: {
319 test: "data"
320 }
321 });
322 inserts++;
323 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
324 });
325 });
326
327 var endedInsert = Date.now();
328
329 var startedRemoves = Date.now();
330
331 var results = 0;
332
333 subscriptions.forEach(function (subscriptionPath) {
334
335 searched++;
336 //console.log('removing:::', subscriptionPath);
337 var result = subscriptionTree.remove(subscriptionPath).length;
338 //console.log('result:::', result);
339
340 results += result;
341 });
342
343 expect(results / CLIENT_COUNT).to.be(subscriptions.length);
344
345 var endedRemoves = Date.now();
346
347 testLog('did ' + inserts + ' precise inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
348
349 testLog('did ' + searched + ' precise removes in ' + (endedRemoves - startedRemoves) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
350
351 var perMillisecond = searched / (endedRemoves - startedRemoves);
352
353 testLog(perMillisecond + ' per millisecond');
354
355 testLog(perMillisecond * 1000 + ' per second');
356
357 done();
358 });
359
360 it('adds, verifies and removes by id random precise subscriptions', function (done) {
361
362 this.timeout(300000);
363
364 var subscriptions = random.randomPaths({
365 count: SUBSCRIPTION_COUNT
366 });
367
368 var clients = random.string({
369 count: CLIENT_COUNT
370 });
371
372 var subscriptionTree = new PareTree();
373
374 var inserts = 0;
375
376 var searched = 0;
377
378 var startedInsert = Date.now();
379
380 var toRemove = [];
381
382 subscriptions.forEach(function (subscriptionPath) {
383
384 clients.forEach(function (sessionId) {
385
386 toRemove.push(subscriptionTree.add(subscriptionPath, {
387 key: sessionId,
388 data: {
389 test: "data"
390 }
391 }));
392 inserts++;
393 if (inserts % 1000 == 0) console.log(inserts + ' inserted.');
394 });
395 });
396
397 var endedInsert = Date.now();
398
399 var startedRemoves = Date.now();
400
401 var results = 0;
402
403 toRemove.forEach(function (reference) {
404
405 searched++;
406 //console.log('removing:::', subscriptionPath);
407 var result = subscriptionTree.remove(reference).length;
408 //console.log('result:::', result);
409
410 results += result;
411 });
412
413 expect(results / CLIENT_COUNT).to.be(subscriptions.length);
414
415 var endedRemoves = Date.now();
416
417 testLog('did ' + inserts + ' precise inserts in ' + (endedInsert - startedInsert) + ' milliseconds');
418
419 testLog('did ' + searched + ' precise removes in ' + (endedRemoves - startedRemoves) + ' milliseconds, in a tree with += ' + inserts + ' nodes.');
420
421 var perMillisecond = searched / (endedRemoves - startedRemoves);
422
423 testLog(perMillisecond + ' per millisecond');
424
425 testLog(perMillisecond * 1000 + ' per second');
426
427 done();
428 });
429
430 var N_SUBSCRIPTION_COUNT = 10000;
431
432 var SEARCHES = 10;
433
434 it('searches ' + N_SUBSCRIPTION_COUNT + ' subscriptions,' + SEARCHES + ' times, no wildcard in search path', function (done) {
435
436 this.timeout(60000);
437
438 var subscriptions = random.randomPaths({
439 duplicate: DUPLICATE_KEYS,
440 count: N_SUBSCRIPTION_COUNT
441 });
442
443 var clients = random.string({
444 count: CLIENT_COUNT
445 });
446
447 var subscriptionTree = new PareTree();
448
449 var subscriptionResults = {};
450
451 subscriptions.forEach(function (subscriptionPath) {
452
453 clients.forEach(function (sessionId) {
454
455 subscriptionTree.add(subscriptionPath.substring(0, subscriptionPath.length - 1) + '*', {
456 key: sessionId,
457 data: {
458 test: "data"
459 }
460 });
461
462 if (!subscriptionResults[sessionId]) subscriptionResults[sessionId] = {
463 paths: {}
464 };
465
466 subscriptionResults[sessionId].paths[subscriptionPath] = true;
467 });
468 });
469
470 var startedSearches = Date.now();
471
472 var searched = 0;
473
474 subscriptions.every(function (subscriptionPath) {
475
476 searched++;
477
478 subscriptionTree.search(subscriptionPath);
479
480 return (searched != SEARCHES);
481 });
482
483 var endedSearches = Date.now();
484
485 testLog('searched through ' + N_SUBSCRIPTION_COUNT * CLIENT_COUNT + ' subscriptions ' + SEARCHES + ' times, in ' + (endedSearches - startedSearches) + ' milliseconds');
486
487 return done();
488
489 });
490});