forked from gaperton/Type-R
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcomplex.js
More file actions
636 lines (526 loc) · 20.3 KB
/
complex.js
File metadata and controls
636 lines (526 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
define( function( require, exports, module ){
var Nested = require( '../../../dist/index.js' ),
Model = Nested.Model;
/********************
* Model definitions
*/
if( Nested.tools ){
Nested.tools.log.level = 1;
}
describe( 'Events ', function(){
this.timeout( 100000 );
var FlatModel = Model.extend({
attributes : {
a0 : Number.has.watcher( 'watcher' ),
a1 : Number.has.watcher( 'watcher' ),
a2 : Number.has.watcher( 'watcher' ),
a3 : Number.has.watcher( 'watcher' ),
a4 : Number.has.watcher( 'watcher' ),
a5 : Number.has.watcher( 'watcher' ),
a6 : Number.has.watcher( 'watcher' ),
a7 : Number.has.watcher( 'watcher' ),
a8 : Number.has.watcher( 'watcher' ),
a9 : Number.has.watcher( 'watcher' )
},
_couter : 0,
watcher : function(){ this._counter++; }
});
describe( 'five lisneners', function(){
it( 'simple on/off', function(){
var source = new FlatModel(),
d1 = new FlatModel(), d2 = new FlatModel(),
d3 = new FlatModel(), d4 = new FlatModel(), d5 = new FlatModel();
function callback(){ this._counter++; }
for( var i = 0; i < 1000000; i++ ){
source.on( 'change', callback, d1 )
.on( 'change', callback, d2 )
.on( 'change', callback, d3 )
.on( 'change', callback, d4 )
.on( 'change', callback, d5 );
source.off( 'change', callback, d1 )
.off( 'change', callback, d2 )
.off( 'change', callback, d3 )
.off( 'change', callback, d4 )
.off( 'change', callback, d5 );
}
});
it( 'trigger', function(){
var source = new FlatModel(),
d1 = new FlatModel(), d2 = new FlatModel(),
d3 = new FlatModel(), d4 = new FlatModel(), d5 = new FlatModel();
function callback(){ this._counter++; }
source.on( 'change', callback, d1 )
.on( 'change', callback, d2 )
.on( 'change', callback, d3 )
.on( 'change', callback, d4 )
.on( 'change', callback, d5 );
for( var i = 0; i < 1000000; i++ ){
source.trigger( 'change', source, {} );
}
});
it( 'listentTo/stopListening', function(){
var source = new FlatModel(),
d1 = new FlatModel(), d2 = new FlatModel(),
d3 = new FlatModel(), d4 = new FlatModel(), d5 = new FlatModel();
function callback(){ this._counter++; }
for( var i = 0; i < 1000000; i++ ){
d1.listenTo( source, 'change', callback );
d2.listenTo( source, 'change', callback );
d3.listenTo( source, 'change', callback );
d4.listenTo( source, 'change', callback );
d5.listenTo( source, 'change', callback );
d1.stopListening( source );
d2.stopListening( source );
d3.stopListening( source );
d4.stopListening( source );
d5.stopListening( source );
}
});
} );
describe( 'Watchers', function(){
it( 'Create and dispose 500K models', function(){
for( var i = 0; i < 500000; i++ ){
var model = new FlatModel();
model.stopListening();
}
} );
it( 'Make 5M changes', function(){
var model = new FlatModel();
for( var i = 0; i < 5000000; i++ ){
model.a0 = i;
}
} );
it( 'Fire 5M events', function(){
var model = new FlatModel();
for( var i = 0; i < 5000000; i++ ){
model.trigger( 'change:a0', model, i, {} );
}
} );
it( 'Make 5M subscribption', function(){
var model = new FlatModel(), m = new FlatModel();
function callback(){}
for( var i = 0; i < 5000000; i++ ){
model.on( 'hello', callback, m );
model.on( 'hello', callback, m );
model.on( 'hello', callback, m );
model.on( 'hello', callback, m );
model.on( 'hello', callback, m );
model.off( 'hello' );
}
});
it( 'Make 5M listenTo subscribption', function(){
var model = new FlatModel(), m = new FlatModel();
function callback(){}
for( var i = 0; i < 1000000; i++ ){
m.listenTo( model, 'hello', callback );
m.listenTo( model, 'hello', callback );
m.listenTo( model, 'hello', callback );
m.listenTo( model, 'hello', callback );
m.listenTo( model, 'hello', callback );
m.stopListening( model );
}
});
});
});
describe( 'Collections of flat models', function(){
this.timeout( 100000 );
var SmallFlatModel = Model.extend({
attributes : {
a : 0
}
});
var LargeFlatModel = Model.extend({
attributes : {
a0 : 0, a1 : 1, a2 : 2, a3 : 3, a4: 4, a5 : 5, a6: 6, a7: 7, a8: 8, a9: 9,
b0 : 0, b1 : 1, b2 : 2, b3 : 3, b4: 4, b5 : 5, b6: 6, b7: 7, b8: 8, b9: 9
}
});
var CollectionWithEvents = LargeFlatModel.Collection.extend({
itemEvents : {
'change:a0' : true,
'change:a1' : true,
'change:a2' : true,
'change:a3' : true,
'change:a4' : true,
'change:a5' : true,
'change:a6' : true,
'change:a7' : true,
'change:a8' : true,
'change:a9' : true
}
});
var smallData = [],
largeData = [];
for( var i = 0; i < 50000; i++ ){
smallData.push({ id : i, a : i });
largeData.push({ id : i,
a0 : i, a1 : i, a2 : i, a3 : i, a4: i, a5 : i, a6: i, a7: i, a8: i, a9: i,
b0 : i, b1 : i, b2 : i, b3 : i, b4: i, b5 : i, b6: i, b7: i, b8: i, b9: i
});
}
smallData = JSON.stringify( smallData );
largeData = JSON.stringify( largeData );
var small, large;
beforeEach( function(){
small = JSON.parse( smallData );
large = JSON.parse( largeData );
});
describe( 'Create 50K collection', function(){
it( '1 attribute model', function(){
var smallCollection = new SmallFlatModel.Collection( small );
});
it( '1 attribute model, Collection.Refs', function(){
var smallCollection = new SmallFlatModel.Collection.Refs( small );
});
it( '20 attribute model', function(){
var largeCollection = new LargeFlatModel.Collection( large );
});
it( '20 attribute model, Collection.Refs', function(){
var largeCollection = new LargeFlatModel.Collection.Refs( large );
});
it( '20 attribute model with events subscribption', function(){
var largeCollection = new CollectionWithEvents( large );
});
});
describe( 'Fetch 50K collection', function(){
it( '1 attribute model', function(){
var smallCollection = new SmallFlatModel.Collection();
smallCollection.set( small );
});
it( '1 attribute model, Collection.Refs', function(){
var smallCollection = new SmallFlatModel.Collection.Refs();
smallCollection.set( small );
});
it( '20 attribute model', function(){
var largeCollection = new LargeFlatModel.Collection();
largeCollection.set( large );
});
it( '20 attribute model, Collection.Refs', function(){
var largeCollection = new LargeFlatModel.Collection.Refs();
largeCollection.set( large );
});
it( '20 attribute model with events subscribption', function(){
var largeCollection = new CollectionWithEvents();
largeCollection.set( large );
});
});
var _smallCollection = new SmallFlatModel.Collection( JSON.parse( smallData ) );
var _largeCollection = new LargeFlatModel.Collection( JSON.parse( largeData ) );
describe( 'Update 50K collection', function(){
it( '1 attribute model', function(){
_smallCollection.set( small );
});
it( '20 attribute model', function(){
_largeCollection.set( large );
});
});
describe( 'Reset 50K collection', function(){
it( '1 attribute model', function(){
_smallCollection.reset( small );
});
it( '20 attribute model', function(){
_largeCollection.reset( large );
});
});
});
describe( 'Recursive model structures', function(){
this.timeout( 100000 );
var LinkedList = Model.extend();
LinkedList.define({
attributes : {
next : LinkedList.value( null ),
value : 0
}
});
var Tree = Model.extend();
Tree.define({
attributes : {
a0 : Tree.value( null ),
a1 : Tree.value( null ),
a2 : Tree.value( null ),
a3 : Tree.value( null ),
a4 : Tree.value( null ),
a5 : Tree.value( null ),
a6 : Tree.value( null ),
a7 : Tree.value( null ),
a8 : Tree.value( null ),
a9 : Tree.value( null ),
value : 0
}
});
function createTree( level ){
if( level ){
var l = level - 1;
return {
a0 : createTree( l ),
a1 : createTree( l ),
a2 : createTree( l ),
a3 : createTree( l ),
a4 : createTree( l ),
a5 : createTree( l ),
a6 : createTree( l ),
a7 : createTree( l ),
a8 : createTree( l ),
a9 : createTree( l ),
value : l
}
}
return null;
}
function createList( n ){
var list = {};
for( var i = 0, l = list; i < n; i++, l = l.next ){
l.next = { next : null, value : i };
}
return list;
}
var treeData, listData;
treeJSON = JSON.stringify( createTree( 6 ) );
listJSON = JSON.stringify( createList( 500 ) );
var _tree, _list;
describe( '500 elements linked list, 1000 times + parse time', function(){
var list;
it( 'Create from JSON', function(){
for( var i = 0; i < 1000; i++ ){
list = new LinkedList( JSON.parse( listJSON ) );
}
});
it( 'Fetch empty from JSON', function(){
for( var i = 0; i < 1000; i++ ){
list = new LinkedList();
list.set( JSON.parse( listJSON ) );
}
});
it( 'Update with JSON', function(){
for( var i = 0; i < 1000; i++ ){
list.set( JSON.parse( listJSON ) );
}
});
});
describe( 'Wide model tree with 10 childs, 100K elements', function(){
var data, tree;
beforeEach( function(){
data = JSON.parse( treeJSON );
});
it( 'Create from JSON', function(){
tree = new Tree( data );
});
it( 'Fetch empty from JSON', function(){
tree = new Tree();
tree.set( data );
});
it( 'Update with JSON', function(){
tree.set( data );
});
});
});
describe( 'Recursive model/collection tree', function(){
this.timeout( 100000 );
var Comment = Model.extend();
Comment.define({
attributes : {
time : Date,
text : String,
author_id : Number,
replies : Comment.Collection
}
});
function createTree( level, id ){
var l = level - 1;
return {
id : id || 0,
time : new Date(),
author_id : level,
text : 'hjkfshdkhjfksdhkj fhsdjkhfsdjk hjfksdhjk hfjsdkhjk hfjdskhjkhj fdssdffsdn,m nm, nm, nv,xvcvcx',
replies : l ? [
createTree( l, 0 ),
createTree( l, 1 ),
createTree( l, 2 ),
createTree( l, 3 ),
createTree( l, 4 ),
createTree( l, 5 ),
createTree( l, 6 ),
createTree( l, 7 ),
createTree( l, 8 ),
createTree( l, 9 )
] : []
}
}
function createList( width, depth ){
var list = [];
for( var i = 0; i < width; i++ ){
list.push( createTree( depth, i ) );
}
return list;
}
var treeData = JSON.stringify( createTree( 6 ) ),
shortList = JSON.stringify( createList( 100, 4 ) ),
midList = JSON.stringify( createList( 1000, 3 ) ),
longList = JSON.stringify( createList( 10000, 2 ) );
var _comment, _short, _mid, _long;
describe( '100K Model/Collection Tree', function(){
var data, collection;
beforeEach( function(){
data = JSON.parse( treeData );
});
it( 'Create from JSON', function(){
collection = new Comment.Collection( data );
collection = null;
});
it( 'Fetch empty from JSON', function(){
collection = new Comment.Collection();
collection.set( data );
});
it( 'Update with JSON', function(){
collection.set( data );
collection = null;
});
});
describe( '100 elements Collection of 1000 items Model/Collection tree', function(){
var data, collection;
beforeEach( function(){
data = JSON.parse( shortList );
});
it( 'Create from JSON', function(){
collection = new Comment.Collection( data );
collection = null;
});
it( 'Fetch empty from JSON', function(){
collection = new Comment.Collection();
collection.set( data );
});
it( 'Update with JSON', function(){
collection.set( data );
collection = null;
});
});
describe( '1000 elements Collection of 100 items Model/Collection tree', function(){
var data, collection;
beforeEach( function(){
data = JSON.parse( midList );
});
it( 'Create from JSON', function(){
collection = new Comment.Collection( data );
collection = null;
});
it( 'Fetch empty from JSON', function(){
collection = new Comment.Collection();
collection.set( data );
});
it( 'Update with JSON', function(){
collection.set( data );
collection = null;
});
});
describe( '10 000 elements Collection of 10 items Model/Collection tree', function(){
var data, collection;
beforeEach( function(){
data = JSON.parse( longList );
});
it( 'Create from JSON', function(){
collection = new Comment.Collection( data );
collection = null;
});
it( 'Fetch empty from JSON', function(){
collection = new Comment.Collection();
collection.set( data );
});
it( 'Update with JSON', function(){
collection.set( data );
collection = null;
});
});
});
describe( 'Users directory', function(){
var User = Model.extend({
idAttribute: 'user_id',
defaults: {
created_at : Date,
updated_at : Date,
username : String,
password : String,
fname : String,
lname : String,
email : String,
active : Number,
'default' : Number,
guid : '',
old_7password : String,
created_by : null,
domain_id: 0,
user_type : Number,
user_types : Array,
permissions: {},
selectedEncoders : subsetOf( '~encoders' ),
roles : subsetOf( '~roles' ),
settings: Model.defaults({
TZ: '',
DefaultMetadataState: 'off',
LoudnessMeterState: 'off',
OrderChannels: 'name',
PlayLiveLastChannel: false,
SendExportNotification: true,
DisplayPlayerSpeed: false
})
}
});
var Dummy = Nested.Model.extend({
attributes : {
name : ''
}
});
var Store = Nested.Store.defaults({
encoders : Dummy.Collection,
roles : Dummy.Collection
});
var store;
if( Nested.store ){
store = Nested.store = new Store();
}
else{
store = Nested.Store.global = new Store();
}
var dummies = [];
for( var i = 0; i < 100; i++ ){
dummies.push( { id : i, name : 'name' + i });
}
store.encoders = dummies;
store.roles = dummies;
var _users = [];
for( var i = 0; i < 10000; i++ ){
_users.push({
active : 1,
created_at : "2015-11-18T16:57:10+00:00",
created_by : null,
'default' : 1,
domain_id : 0,
email : "observer-import-system@volicon.com",
fname : "api_import_user",
lname : "api_import_user",
roles:["5"],
updated_at:"2015-11-18T16:57:10+00:00",
user_id: i,
user_type:3,
user_types:["3"],
username : "api_import_user"
});
}
var usersJSONtext = JSON.stringify( _users );
_users = null;
var usersJSON;
beforeEach( function(){
usersJSON = JSON.parse( usersJSONtext );
});
var collection;
it( 'Creates users collection from JSON', function(){
collection = new User.Collection( usersJSON );
} );
it( 'Fetches users collection from JSON', function(){
collection = new User.Collection();
collection.set( usersJSON );
} );
it( 'Updates users collection', function(){
collection.set( usersJSON );
});
});
});