1
1
import * as chai from 'chai' ;
2
+ import * as fs from 'fs' ;
3
+ import { performance } from 'perf_hooks' ;
2
4
3
5
import { SerializeField , DataType , BinarySerialize , BinaryDeserialize } from './BinarySerializer' ;
4
6
5
7
const expect = chai . expect ;
6
8
7
- class ClassA {
9
+
10
+ class ClassA {
8
11
@SerializeField ( DataType . String )
9
- public mstr :string ;
12
+ public mstr : string ;
10
13
@SerializeField ( DataType . Bool )
11
- public mbool :boolean ;
14
+ public mbool : boolean ;
12
15
@SerializeField ( DataType . Float32 )
13
- public mfloat32 :number ;
16
+ public mfloat32 : number ;
14
17
@SerializeField ( DataType . Float64 )
15
- public mfloat64 :number ;
18
+ public mfloat64 : number ;
16
19
@SerializeField ( DataType . Int8 )
17
- public mint8 :number ;
20
+ public mint8 : number ;
18
21
@SerializeField ( DataType . Int16 )
19
- public mint16 :number ;
22
+ public mint16 : number ;
20
23
@SerializeField ( DataType . Int32 )
21
- public mint32 :number ;
24
+ public mint32 : number ;
22
25
@SerializeField ( DataType . Uint8 )
23
- public muint8 :number ;
26
+ public muint8 : number ;
24
27
@SerializeField ( DataType . Uint16 )
25
- public muint16 :number ;
28
+ public muint16 : number ;
26
29
@SerializeField ( DataType . Uint32 )
27
- public muint32 :number ;
30
+ public muint32 : number ;
28
31
}
29
32
30
- describe ( 'primitive-type' , ( ) => {
33
+ describe ( 'primitive-type' , ( ) => {
31
34
let obja = new ClassA ( ) ;
32
35
obja . mbool = false ;
33
36
obja . mstr = "helloworld" ;
@@ -37,64 +40,316 @@ describe('primitive-type',()=>{
37
40
obja . muint8 = 255 ;
38
41
obja . mint16 = - 23324 ;
39
42
obja . muint16 = 65535 ;
40
- obja . mint32 = - ( 1 << 16 ) ;
41
- obja . muint32 = 1 << 31 - 1 ;
43
+ obja . mint32 = - ( 1 << 16 ) ;
44
+ obja . muint32 = 1 << 31 - 1 ;
45
+
46
+ let buffer = BinarySerialize ( obja , ClassA ) ;
47
+ let objb = < ClassA > BinaryDeserialize ( ClassA , buffer ) ;
42
48
43
- let buffer = BinarySerialize ( obja ) ;
44
- let objb = < ClassA > BinaryDeserialize ( ClassA , buffer ) ;
45
- it ( "bool" , ( ) => {
49
+ it ( "bool" , ( ) => {
46
50
expect ( objb . mbool ) . to . eq ( obja . mbool ) ;
47
51
} )
48
- it ( "string" , ( ) => {
52
+ it ( "string" , ( ) => {
49
53
expect ( objb . mstr ) . to . eq ( obja . mstr ) ;
50
54
} )
51
- it ( "float32" , ( ) => {
52
- expect ( objb . mfloat32 ) . to . closeTo ( obja . mfloat32 , 0.0000001 ) ;
55
+ it ( "float32" , ( ) => {
56
+ expect ( objb . mfloat32 ) . to . closeTo ( obja . mfloat32 , 0.0000001 ) ;
53
57
} )
54
- it ( "float64" , ( ) => {
55
- expect ( objb . mfloat64 ) . to . closeTo ( obja . mfloat64 , 0.000000000000001 ) ;
58
+ it ( "float64" , ( ) => {
59
+ expect ( objb . mfloat64 ) . to . closeTo ( obja . mfloat64 , 0.000000000000001 ) ;
56
60
} )
57
- it ( "int8" , ( ) => {
61
+ it ( "int8" , ( ) => {
58
62
expect ( objb . mint8 ) . to . eq ( obja . mint8 ) ;
59
63
} )
60
- it ( "int16" , ( ) => {
64
+ it ( "int16" , ( ) => {
61
65
expect ( objb . mint16 ) . to . eq ( obja . mint16 ) ;
62
66
} )
63
- it ( "int32" , ( ) => {
67
+ it ( "int32" , ( ) => {
64
68
expect ( objb . mint32 ) . to . eq ( obja . mint32 ) ;
65
69
} )
66
- it ( "uint8" , ( ) => {
70
+ it ( "uint8" , ( ) => {
67
71
expect ( objb . muint8 ) . to . eq ( obja . muint8 ) ;
68
72
} )
69
- it ( "uint16" , ( ) => {
73
+ it ( "uint16" , ( ) => {
70
74
expect ( objb . muint16 ) . to . eq ( obja . muint16 ) ;
71
75
} )
72
- it ( "uint32" , ( ) => {
76
+ it ( "uint32" , ( ) => {
73
77
expect ( objb . muint32 ) . to . eq ( obja . muint32 ) ;
74
78
} )
75
79
} )
76
80
77
81
78
- class LargeData {
79
- @SerializeField ( DataType . Float64 , true )
80
- public numary :number [ ] ;
82
+ class LargeData {
83
+ @SerializeField ( DataType . Float64 , true )
84
+ public numary : number [ ] ;
81
85
}
82
86
83
- describe ( 'large-data' , ( ) => {
87
+ describe ( 'large-data' , ( ) => {
84
88
let data = new LargeData ( ) ;
85
89
let ary = new Array < number > ( ) ;
86
- for ( var i = 0 ; i < 300 ; i ++ ) {
90
+ for ( var i = 0 ; i < 300 ; i ++ ) {
87
91
ary . push ( Math . random ( ) ) ;
88
92
}
89
- data . numary = ary ;
93
+ data . numary = ary ;
90
94
let serializedData = BinarySerialize ( data ) ;
91
- let deserializeObj = < LargeData > BinaryDeserialize ( LargeData , serializedData ) ;
95
+ let deserializeObj = < LargeData > BinaryDeserialize ( LargeData , serializedData ) ;
92
96
let dary = deserializeObj . numary ;
93
97
94
- it ( 'large-array' , ( ) => {
98
+ it ( 'large-array' , ( ) => {
95
99
expect ( dary . length ) . to . eq ( ary . length ) ;
96
- for ( let i = 0 , len = dary . length ; i < len ; i ++ ) {
97
- expect ( dary [ i ] ) . to . closeTo ( ary [ i ] , 0.000000001 ) ;
100
+ for ( let i = 0 , len = dary . length ; i < len ; i ++ ) {
101
+ expect ( dary [ i ] ) . to . closeTo ( ary [ i ] , 0.000000001 ) ;
98
102
}
99
103
} )
100
104
} )
105
+
106
+ //----------------------
107
+
108
+ class AttatchType {
109
+ @SerializeField ( DataType . Float32 )
110
+ public num :number ;
111
+ }
112
+
113
+ describe ( 'attatch-type' , ( ) => {
114
+
115
+ //prototype: [AttatchType]
116
+ let obj1 = new AttatchType ( ) ;
117
+ obj1 . num = 10 ;
118
+
119
+ //prototype: [Object]
120
+ let jstr = '{"num":10}' ;
121
+ let obj2 = < AttatchType > JSON . parse ( jstr ) ;
122
+
123
+ //prototype: [Object]
124
+ let obj3 = { num :10 } ;
125
+
126
+ //prototype: null;
127
+ let obj4 = Object . create ( null ) ;
128
+ obj4 . num = 10 ;
129
+
130
+ //prototype: [AttatchType]
131
+ let obj5 = { } ;
132
+ Object . setPrototypeOf ( obj5 , Object . getPrototypeOf ( new AttatchType ( ) ) ) ;
133
+ obj5 [ 'num' ] = 10 ;
134
+
135
+ let d1 = < AttatchType > BinaryDeserialize ( AttatchType , BinarySerialize ( obj1 ) ) ;
136
+ let d2 = < AttatchType > BinaryDeserialize ( AttatchType , BinarySerialize ( obj2 , AttatchType ) ) ;
137
+ let d3 = < AttatchType > BinaryDeserialize ( AttatchType , BinarySerialize ( obj3 , AttatchType ) ) ;
138
+ let d4 = < AttatchType > BinaryDeserialize ( AttatchType , BinarySerialize ( obj4 , AttatchType ) ) ;
139
+ let d5 = < AttatchType > BinaryDeserialize ( AttatchType , BinarySerialize ( obj5 ) ) ;
140
+
141
+ it ( 'new()' , ( ) => {
142
+ expect ( d1 . num ) . to . eq ( 10 ) ;
143
+ } )
144
+ it ( 'json-parse' , ( ) => {
145
+ expect ( d2 . num ) . to . eq ( 10 ) ;
146
+ } )
147
+ it ( 'anonymous-object' , ( ) => {
148
+ expect ( d3 . num ) . to . eq ( 10 ) ;
149
+ } )
150
+ it ( 'object-create' , ( ) => {
151
+ expect ( d4 . num ) . to . eq ( 10 ) ;
152
+ } )
153
+ it ( 'object-set-prototype' , ( ) => {
154
+ expect ( d5 . num ) . to . eq ( 10 ) ;
155
+ } )
156
+ } ) ;
157
+
158
+ //-----------------------------------------------
159
+
160
+ class ClassWithArray {
161
+ @SerializeField ( DataType . Float32 , true )
162
+ public ary1 :Array < number > ;
163
+ @SerializeField ( DataType . Float32 , true )
164
+ public ary2 :number [ ] ;
165
+ @SerializeField ( DataType . Float32 , true )
166
+ public emptyAry :number [ ] ;
167
+ @SerializeField ( DataType . Float32 , true )
168
+ public nullAry :number [ ] ;
169
+ }
170
+
171
+
172
+ describe ( "array" , ( ) => {
173
+
174
+ let obj = new ClassWithArray ( ) ;
175
+ obj . ary1 = [ 10 ] ;
176
+ obj . ary2 = [ 10 ] ;
177
+ obj . emptyAry = [ ] ;
178
+
179
+ let d = BinarySerialize ( obj ) ;
180
+ var d1 = < ClassWithArray > BinaryDeserialize ( ClassWithArray , d ) ;
181
+
182
+ it ( 'Array<T>' , ( ) => {
183
+ expect ( d1 . ary1 [ 0 ] ) . to . eq ( 10 ) ;
184
+ } )
185
+
186
+ it ( 'number[]' , ( ) => {
187
+ expect ( d1 . ary2 [ 0 ] ) . to . eq ( 10 ) ;
188
+ } )
189
+
190
+ it ( 'empty array' , ( ) => {
191
+ expect ( d1 . emptyAry . length ) . to . eq ( 0 ) ;
192
+ } )
193
+
194
+ it ( 'null array' , ( ) => {
195
+ expect ( d1 . nullAry ) . to . null ;
196
+ } )
197
+
198
+ } ) ;
199
+
200
+ //-------------------------------------------------
201
+
202
+ export class Vector2
203
+ {
204
+ @SerializeField ( DataType . Float32 )
205
+ x :number ;
206
+ @SerializeField ( DataType . Float32 )
207
+ y :number ;
208
+ }
209
+
210
+ export class Color
211
+ {
212
+ @SerializeField ( DataType . Float32 )
213
+ r :number ;
214
+ @SerializeField ( DataType . Float32 )
215
+ g :number ;
216
+ @SerializeField ( DataType . Float32 )
217
+ b :number ;
218
+ @SerializeField ( DataType . Float32 )
219
+ a :number ;
220
+ }
221
+
222
+ export class Sprite
223
+ {
224
+ @SerializeField ( DataType . String )
225
+ public spriteName :string ;
226
+ @SerializeField ( DataType . Object , false , Color )
227
+ public spriteTint :Color ;
228
+ @SerializeField ( DataType . Object , false , Vector2 )
229
+ public spritePivot :Vector2 ;
230
+ @SerializeField ( DataType . Bool )
231
+ public flipX :boolean ;
232
+ @SerializeField ( DataType . Bool )
233
+ public flipY :boolean ;
234
+ @SerializeField ( DataType . String )
235
+ public maskBone :string ;
236
+ }
237
+
238
+ export class Vector3
239
+ {
240
+ @SerializeField ( DataType . Float32 )
241
+ x :number ;
242
+ @SerializeField ( DataType . Float32 )
243
+ y :number ;
244
+ @SerializeField ( DataType . Float32 )
245
+ z :number ;
246
+ }
247
+
248
+ class Bone
249
+ {
250
+ @SerializeField ( DataType . String )
251
+ public name :string ;
252
+ @SerializeField ( DataType . Object , false , Vector3 )
253
+ public position :Vector3 ;
254
+ @SerializeField ( DataType . Object , false , Vector3 )
255
+ public scale :Vector3 ;
256
+ @SerializeField ( DataType . Float32 )
257
+ public rotation :number ;
258
+ @SerializeField ( DataType . Bool )
259
+ public active :boolean ;
260
+ @SerializeField ( DataType . Object , false , Sprite )
261
+ public sprite :Sprite ;
262
+
263
+ public children :Array < string > ;
264
+ }
265
+
266
+ export class Frame
267
+ {
268
+ @SerializeField ( DataType . Float32 )
269
+ public time :number ;
270
+ @SerializeField ( DataType . Float32 )
271
+ public val :number ;
272
+ @SerializeField ( DataType . String )
273
+ public str :string ;
274
+ }
275
+
276
+ export class Curve
277
+ {
278
+ @SerializeField ( DataType . String )
279
+ public aim :string ;
280
+ @SerializeField ( DataType . String )
281
+ public type :string ;
282
+ @SerializeField ( DataType . Object , true , Frame )
283
+ public frames :Array < Frame > ;
284
+
285
+ private framesDic :{ [ key :number ] :number | string } = { }
286
+ }
287
+
288
+ export class State
289
+ {
290
+ @SerializeField ( DataType . String )
291
+ public name :string ;
292
+ @SerializeField ( DataType . String )
293
+ public aniName :string ;
294
+ @SerializeField ( DataType . String , true )
295
+ public nextStates :Array < string > ;
296
+ }
297
+
298
+
299
+ export class Clip
300
+ {
301
+ @SerializeField ( DataType . String )
302
+ public name :string ;
303
+ @SerializeField ( DataType . Object , true , Curve )
304
+ public curves :Array < Curve > ;
305
+ @SerializeField ( DataType . Int16 )
306
+ private frameCount :number ;
307
+
308
+ }
309
+
310
+ export class Animation
311
+ {
312
+ @SerializeField ( DataType . Object , true , Clip )
313
+ public animClips :Array < Clip > ;
314
+ @SerializeField ( DataType . Object , true , State )
315
+ public states :Array < State > ;
316
+ }
317
+
318
+ export class DataInfo
319
+ {
320
+ @SerializeField ( DataType . String )
321
+ public rootBone :string ;
322
+ @SerializeField ( DataType . Object , true , Bone )
323
+ public bones :Array < Bone > ;
324
+ @SerializeField ( DataType . Object , false , Animation )
325
+ public anim :Animation ;
326
+ }
327
+
328
+
329
+ describe ( 'benchmark' , ( ) => {
330
+
331
+ fs . readFile ( './testdata/sample-data.json' , ( e , buffer ) => {
332
+ let jsonstr = buffer . toString ( ) ;
333
+ let t1 = performance . now ( ) ;
334
+ let obj = JSON . parse ( jsonstr ) ;
335
+ console . log ( `json deserialize: ${ performance . now ( ) - t1 } ms` ) ;
336
+
337
+ let jsonsize = buffer . byteLength ;
338
+ console . log ( 'json size: ' + jsonsize + " byte" ) ;
339
+
340
+ let datainfo :DataInfo = < DataInfo > obj ;
341
+ let serializedData = BinarySerialize ( datainfo , DataInfo ) ;
342
+ let t2 = performance . now ( ) ;
343
+ let obj1 = BinaryDeserialize ( DataInfo , serializedData ) ;
344
+ console . log ( `binary deserialize: ${ performance . now ( ) - t2 } ms` ) ;
345
+
346
+ let binarysize = serializedData . byteLength ;
347
+ console . log ( 'binary size: ' + binarysize + " byte" ) ;
348
+ console . log ( 'size save: ' + ( ( jsonsize - binarysize ) / jsonsize ) ) ;
349
+
350
+ } ) ;
351
+
352
+
353
+ } )
354
+
355
+ //--------------------------------------
0 commit comments