File: test_benchmarks.py

package info (click to toggle)
construct 2.10.58%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,780 kB
  • sloc: python: 11,135; makefile: 132
file content (820 lines) | stat: -rw-r--r-- 24,014 bytes parent folder | download
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# -*- coding: utf-8 -*-

from declarativeunittest import *
from construct import *
from construct.lib import *
from test_compiler import example, exampledata


def test_class_bytes_parse(benchmark):
    d = Bytes(100)
    benchmark(d.parse, bytes(100))

def test_class_bytes_parse_compiled(benchmark):
    d = Bytes(100)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_bytes_build(benchmark):
    d = Bytes(100)
    benchmark(d.build, bytes(100))

def test_class_greedybytes_parse(benchmark):
    d = GreedyBytes
    benchmark(d.parse, bytes(100))

def test_class_greedybytes_parse_compiled(benchmark):
    d = GreedyBytes
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_greedybytes_build(benchmark):
    d = GreedyBytes
    benchmark(d.build, bytes(100))

def test_class_bitwise1_parse(benchmark):
    d = Bitwise(Bytes(800))
    benchmark(d.parse, bytes(100))

def test_class_bitwise1_parse_compiled(benchmark):
    d = Bitwise(Bytes(800))
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_bitwise1_build(benchmark):
    d = Bitwise(Bytes(800))
    benchmark(d.build, bytes(800))

def test_class_bitwise2_parse(benchmark):
    d = Bitwise(RepeatUntil(obj_ == 1, Byte))
    benchmark(d.parse, bytes(99)+b"\x01")

def test_class_bitwise2_parse_compiled(benchmark):
    d = Bitwise(RepeatUntil(obj_ == 1, Byte))
    d = d.compile()
    benchmark(d.parse, bytes(99)+b"\x01")

def test_class_bitwise2_build(benchmark):
    d = Bitwise(RepeatUntil(obj_ == 1, Byte))
    benchmark(d.build, [0 if i<800-1 else 1 for i in range(800)])

def test_class_bytewise1_parse(benchmark):
    d = Bitwise(Bytewise(Bytes(100)))
    benchmark(d.parse, bytes(100))

def test_class_bytewise1_parse_compiled(benchmark):
    d = Bitwise(Bytewise(Bytes(100)))
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_bytewise1_build(benchmark):
    d = Bitwise(Bytewise(Bytes(100)))
    benchmark(d.build, bytes(100))

def test_class_bytewise2_parse(benchmark):
    d = Bitwise(Bytewise(RepeatUntil(obj_ == 1, Byte)))
    benchmark(d.parse, bytes(99)+b"\x01")

def test_class_bytewise2_parse_compiled(benchmark):
    d = Bitwise(Bytewise(RepeatUntil(obj_ == 1, Byte)))
    d = d.compile()
    benchmark(d.parse, bytes(99)+b"\x01")

def test_class_bytewise2_build(benchmark):
    d = Bitwise(Bytewise(RepeatUntil(obj_ == 1, Byte)))
    benchmark(d.build, [0 if i<100-1 else 1 for i in range(100)])

def test_class_formatfield_parse(benchmark):
    d = FormatField(">", "L")
    benchmark(d.parse, bytes(4))

def test_class_formatfield_parse_compiled(benchmark):
    d = FormatField(">", "L")
    d = d.compile()
    benchmark(d.parse, bytes(4))

def test_class_formatfield_build(benchmark):
    d = FormatField(">", "L")
    benchmark(d.build, 0)

def test_class_bytesinteger_parse(benchmark):
    d = BytesInteger(16)
    benchmark(d.parse, bytes(16))

def test_class_bytesinteger_parse_compiled(benchmark):
    d = BytesInteger(16)
    d = d.compile()
    benchmark(d.parse, bytes(16))

def test_class_bytesinteger_build(benchmark):
    d = BytesInteger(16)
    benchmark(d.build, 0)

def test_class_bitsinteger_parse(benchmark):
    d = Bitwise(BitsInteger(128, swapped=True))
    benchmark(d.parse, bytes(128//8))

def test_class_bitsinteger_parse_compiled(benchmark):
    d = Bitwise(BitsInteger(128, swapped=True))
    d = d.compile()
    benchmark(d.parse, bytes(128//8))

def test_class_bitsinteger_build(benchmark):
    d = Bitwise(BitsInteger(128, swapped=True))
    benchmark(d.build, 0)

def test_class_varint_parse(benchmark):
    d = VarInt
    benchmark(d.parse, b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x04')

def test_class_varint_parse_compiled(benchmark):
    d = VarInt
    d = d.compile()
    benchmark(d.parse, b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x04')

def test_class_varint_build(benchmark):
    d = VarInt
    benchmark(d.build, 2**64)

def test_class_paddedstring_parse(benchmark):
    d = PaddedString(100, "utf8")
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00\x00'+bytes(100))

def test_class_paddedstring_parse_compiled(benchmark):
    d = PaddedString(100, "utf8")
    d = d.compile()
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00\x00'+bytes(100))

def test_class_paddedstring_build(benchmark):
    d = PaddedString(100, "utf8")
    benchmark(d.build, u"Афон")

def test_class_pascalstring_parse(benchmark):
    d = PascalString(Byte, "utf8")
    benchmark(d.parse, b'\x08\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd'+bytes(100))

def test_class_pascalstring_parse_compiled(benchmark):
    d = PascalString(Byte, "utf8")
    d = d.compile()
    benchmark(d.parse, b'\x08\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd'+bytes(100))

def test_class_pascalstring_build(benchmark):
    d = PascalString(Byte, "utf8")
    benchmark(d.build, u"Афон")

def test_class_cstring_parse(benchmark):
    d = CString("utf8")
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00'+bytes(100))

def test_class_cstring_parse_compiled(benchmark):
    d = CString("utf8")
    d = d.compile()
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00'+bytes(100))

def test_class_cstring_build(benchmark):
    d = CString("utf8")
    benchmark(d.build, u"Афон")

def test_class_greedystring_parse(benchmark):
    d = GreedyString("utf8")
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00'+bytes(100))

def test_class_greedystring_parse_compiled(benchmark):
    d = GreedyString("utf8")
    d = d.compile()
    benchmark(d.parse, b'\xd0\x90\xd1\x84\xd0\xbe\xd0\xbd\x00'+bytes(100))

def test_class_greedystring_build(benchmark):
    d = GreedyString("utf8")
    benchmark(d.build, u"Афон")

def test_class_flag_parse(benchmark):
    d = Flag
    benchmark(d.parse, bytes(1))

def test_class_flag_parse_compiled(benchmark):
    d = Flag
    d = d.compile()
    benchmark(d.parse, bytes(1))

def test_class_flag_build(benchmark):
    d = Flag
    benchmark(d.build, False)

def test_class_enum_parse(benchmark):
    d = Enum(Byte, zero=0)
    benchmark(d.parse, bytes(1))

def test_class_enum_parse_compiled(benchmark):
    d = Enum(Byte, zero=0)
    d = d.compile()
    benchmark(d.parse, bytes(1))

def test_class_enum_build(benchmark):
    d = Enum(Byte, zero=0)
    benchmark(d.build, 0)

def test_class_flagsenum_parse(benchmark):
    d = FlagsEnum(Byte, a=1, b=2, c=4, d=8)
    benchmark(d.parse, bytes(1))

def test_class_flagsenum_parse_compiled(benchmark):
    d = FlagsEnum(Byte, a=1, b=2, c=4, d=8)
    d = d.compile()
    benchmark(d.parse, bytes(1))

def test_class_flagsenum_build(benchmark):
    d = FlagsEnum(Byte, a=1, b=2, c=4, d=8)
    benchmark(d.build, Container(a=False, b=False, c=False, d=False))

def test_class_mapping_parse(benchmark):
    x = "object"
    d = Mapping(Byte, {x:0})
    benchmark(d.parse, bytes(1))

def test_class_mapping_parse_compiled(benchmark):
    x = "object"
    d = Mapping(Byte, {x:0})
    d = d.compile()
    benchmark(d.parse, bytes(1))

def test_class_mapping_build(benchmark):
    x = "object"
    d = Mapping(Byte, {x:0})
    benchmark(d.build, x)

def test_class_struct_parse(benchmark):
    d = Struct("a"/Byte, "b"/Byte, "c"/Byte, "d"/Byte, "e"/Byte)
    benchmark(d.parse, bytes(5))

def test_class_struct_parse_compiled(benchmark):
    d = Struct("a"/Byte, "b"/Byte, "c"/Byte, "d"/Byte, "e"/Byte)
    d = d.compile()
    benchmark(d.parse, bytes(5))

def test_class_struct_build(benchmark):
    d = Struct("a"/Byte, "b"/Byte, "c"/Byte, "d"/Byte, "e"/Byte)
    benchmark(d.build, dict(a=0, b=0, c=0, d=0, e=0))

def test_class_sequence_parse(benchmark):
    d = Sequence(Byte, Byte, Byte, Byte, Byte)
    benchmark(d.parse, bytes(5))

def test_class_sequence_parse_compiled(benchmark):
    d = Sequence(Byte, Byte, Byte, Byte, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(5))

def test_class_sequence_build(benchmark):
    d = Sequence(Byte, Byte, Byte, Byte, Byte)
    benchmark(d.build, [0]*5)

def test_class_array_parse(benchmark):
    d = Array(100, Byte)
    benchmark(d.parse, bytes(100))

def test_class_array_parse_compiled(benchmark):
    d = Array(100, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_array_build(benchmark):
    d = Array(100, Byte)
    benchmark(d.build, [0]*100)

def test_class_greedyrange_parse(benchmark):
    d = GreedyRange(Byte)
    benchmark(d.parse, bytes(100))

def test_class_greedyrange_parse_compiled(benchmark):
    d = GreedyRange(Byte)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_greedyrange_build(benchmark):
    d = GreedyRange(Byte)
    benchmark(d.build, [0]*100)

def test_class_repeatuntil_parse(benchmark):
    d = RepeatUntil(obj_ > 0, Byte)
    benchmark(d.parse, bytes(i<100 for i in range(100)))

def test_class_repeatuntil_parse_compiled(benchmark):
    d = RepeatUntil(obj_ > 0, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(i<100 for i in range(100)))

def test_class_repeatuntil_build(benchmark):
    d = RepeatUntil(obj_ > 0, Byte)
    benchmark(d.build, [int(i<99) for i in range(100)])

def test_class_const_parse(benchmark):
    d = Const(bytes(10))
    benchmark(d.parse, bytes(10))

def test_class_const_parse_compiled(benchmark):
    d = Const(bytes(10))
    d = d.compile()
    benchmark(d.parse, bytes(10))

def test_class_const_build(benchmark):
    d = Const(bytes(10))
    benchmark(d.build, bytes(10))

def test_class_computed_parse(benchmark):
    d = Computed(this.entry)
    benchmark(d.parse, bytes(), entry=1)

def test_class_computed_parse_compiled(benchmark):
    d = Computed(this.entry)
    d = d.compile()
    benchmark(d.parse, bytes(), entry=1)

def test_class_computed_build(benchmark):
    d = Computed(this.entry)
    benchmark(d.build, None, entry=1)

# - not supported by compiler
# Index

def test_class_rebuild_parse(benchmark):
    d = Rebuild(Int32ub, 0)
    benchmark(d.parse, bytes(4))

def test_class_rebuild_parse_compiled(benchmark):
    d = Rebuild(Int32ub, 0)
    d = d.compile()
    benchmark(d.parse, bytes(4))

def test_class_rebuild_build(benchmark):
    d = Rebuild(Int32ub, 0)
    benchmark(d.build, None)

def test_class_default_parse(benchmark):
    d = Default(Int32ub, 0)
    benchmark(d.parse, bytes(4))

def test_class_default_parse_compiled(benchmark):
    d = Default(Int32ub, 0)
    d = d.compile()
    benchmark(d.parse, bytes(4))

def test_class_default_build(benchmark):
    d = Default(Int32ub, 0)
    benchmark(d.build, None)

def test_class_check_parse(benchmark):
    d = Check(this.entry == 1)
    benchmark(d.parse, bytes(), entry=1)

def test_class_check_parse_compiled(benchmark):
    d = Check(this.entry == 1)
    d = d.compile()
    benchmark(d.parse, bytes(), entry=1)

def test_class_check_build(benchmark):
    d = Check(this.entry == 1)
    benchmark(d.build, None, entry=1)

# - raises exception
# Error

def test_class_focusedseq_parse(benchmark):
    d = FocusedSeq("num", Const(bytes(10)), "num"/Int32ub, Terminated)
    benchmark(d.parse, bytes(14))

def test_class_focusedseq_parse_compiled(benchmark):
    d = FocusedSeq("num", Const(bytes(10)), "num"/Int32ub, Terminated)
    d = d.compile()
    benchmark(d.parse, bytes(14))

def test_class_focusedseq_build(benchmark):
    d = FocusedSeq("num", Const(bytes(10)), "num"/Int32ub, Terminated)
    benchmark(d.build, 0)

def test_class_pickled_parse(benchmark):
    d = Pickled
    if PY3:
        data = b'\x80\x03]q\x00()K\x01G@\x02ffffff}q\x01]q\x02C\x01\x00q\x03X\x00\x00\x00\x00q\x04e.'
    else:
        data = b"(lp0\n(taI1\naF2.3\na(dp1\na(lp2\naS'1'\np3\naS''\np4\na."
    benchmark(d.parse, data)

def test_class_pickled_parse_compiled(benchmark):
    d = Pickled
    d = d.compile()
    if PY3:
        data = b'\x80\x03]q\x00()K\x01G@\x02ffffff}q\x01]q\x02C\x01\x00q\x03X\x00\x00\x00\x00q\x04e.'
    else:
        data = b"(lp0\n(taI1\naF2.3\na(dp1\na(lp2\naS'1'\np3\naS''\np4\na."
    benchmark(d.parse, data)

def test_class_pickled_build(benchmark):
    d = Pickled
    if PY3:
        data = b'\x80\x03]q\x00()K\x01G@\x02ffffff}q\x01]q\x02C\x01\x00q\x03X\x00\x00\x00\x00q\x04e.'
    else:
        data = b"(lp0\n(taI1\naF2.3\na(dp1\na(lp2\naS'1'\np3\naS''\np4\na."
    benchmark(d.build, d.parse(data))

def test_class_numpy_parse(benchmark):
    d = Numpy
    data = b"\x93NUMPY\x01\x00F\x00{'descr': '<i8', 'fortran_order': False, 'shape': (3,), }            \n\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00"
    benchmark(d.parse, data)

def test_class_numpy_parse_compiled(benchmark):
    d = Numpy
    d = d.compile()
    data = b"\x93NUMPY\x01\x00F\x00{'descr': '<i8', 'fortran_order': False, 'shape': (3,), }            \n\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00"
    benchmark(d.parse, data)

def test_class_numpy_build(benchmark):
    d = Numpy
    data = b"\x93NUMPY\x01\x00F\x00{'descr': '<i8', 'fortran_order': False, 'shape': (3,), }            \n\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00"
    benchmark(d.build, d.parse(data))

def test_class_namedtuple1_parse(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Array(3, Byte))
    benchmark(d.parse, bytes(3))

def test_class_namedtuple1_parse_compiled(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Array(3, Byte))
    d = d.compile()
    benchmark(d.parse, bytes(3))

def test_class_namedtuple1_build(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Array(3, Byte))
    benchmark(d.build, t(1,2,3))

def test_class_namedtuple2_parse(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Struct("x"/Byte, "y"/Byte, "z"/Byte))
    benchmark(d.parse, bytes(3))

def test_class_namedtuple2_parse_compiled(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Struct("x"/Byte, "y"/Byte, "z"/Byte))
    d = d.compile()
    benchmark(d.parse, bytes(3))

def test_class_namedtuple2_build(benchmark):
    t = collections.namedtuple("coord", "x y z")
    d = NamedTuple("coord", "x y z", Struct("x"/Byte, "y"/Byte, "z"/Byte))
    benchmark(d.build, t(x=1,y=2,z=3))

def test_class_timestamp1_parse(benchmark):
    d = Timestamp(Int64ub, 1, 1970)
    benchmark(d.parse, bytes(8))

def test_class_timestamp1_parse_compiled(benchmark):
    d = Timestamp(Int64ub, 1, 1970)
    d = d.compile()
    benchmark(d.parse, bytes(8))

def test_class_timestamp1_build(benchmark):
    import arrow
    d = Timestamp(Int64ub, 1, 1970)
    benchmark(d.build, arrow.Arrow(2000,1,1))

def test_class_timestamp2_parse(benchmark):
    d = Timestamp(Int32ub, "msdos", "msdos")
    benchmark(d.parse, b'H9\x8c"')

def test_class_timestamp2_parse_compiled(benchmark):
    d = Timestamp(Int32ub, "msdos", "msdos")
    d = d.compile()
    benchmark(d.parse, b'H9\x8c"')

def test_class_timestamp2_build(benchmark):
    import arrow
    d = Timestamp(Int32ub, "msdos", "msdos")
    benchmark(d.build, arrow.Arrow(2000,1,1))

def test_class_hex_parse(benchmark):
    d = Hex(GreedyBytes)
    benchmark(d.parse, bytes(100))

def test_class_hex_parse_compiled(benchmark):
    d = Hex(GreedyBytes)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_hex_build(benchmark):
    d = Hex(GreedyBytes)
    benchmark(d.build, bytes(100))

def test_class_hexdump_parse(benchmark):
    d = HexDump(GreedyBytes)
    benchmark(d.parse, bytes(100))

def test_class_hexdump_parse_compiled(benchmark):
    d = HexDump(GreedyBytes)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_hexdump_build(benchmark):
    d = HexDump(GreedyBytes)
    benchmark(d.build, bytes(100))

def test_class_union_parse(benchmark):
    d = Union(0, "raw"/Bytes(8), "ints"/Int32ub[2], "shorts"/Int16ub[4], "chars"/Byte[8])
    benchmark(d.parse, bytes(8))

def test_class_union_parse_compiled(benchmark):
    d = Union(0, "raw"/Bytes(8), "ints"/Int32ub[2], "shorts"/Int16ub[4], "chars"/Byte[8])
    d = d.compile()
    benchmark(d.parse, bytes(8))

def test_class_union_build(benchmark):
    d = Union(0, "raw"/Bytes(8), "ints"/Int32ub[2], "shorts"/Int16ub[4], "chars"/Byte[8])
    benchmark(d.build, dict(chars=[0]*8))

def test_class_select_parse(benchmark):
    d = Select(Int32ub, CString("utf8"))
    benchmark(d.parse, bytes(20))

def test_class_select_parse_compiled(benchmark):
    d = Select(Int32ub, CString("utf8"))
    d = d.compile()
    benchmark(d.parse, bytes(20))

def test_class_select_build(benchmark):
    d = Select(Int32ub, CString("utf8"))
    benchmark(d.build, u"...")

# - combines performance of other fields
# Optional
# - combines performance of other fields
# If

def test_class_ifthenelse_parse(benchmark):
    d = IfThenElse(this.cond, Byte, Byte)
    benchmark(d.parse, bytes(4), cond=True)

def test_class_ifthenelse_parse_compiled(benchmark):
    d = IfThenElse(this.cond, Byte, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(4), cond=True)

def test_class_ifthenelse_build(benchmark):
    d = IfThenElse(this.cond, Byte, Byte)
    benchmark(d.build, 0, cond=True)

def test_class_switch_parse(benchmark):
    d = Switch(this.n, { 1:Int8ub, 2:Int16ub, 4:Int32ub })
    benchmark(d.parse, bytes(4), n=4)

def test_class_switch_parse_compiled(benchmark):
    d = Switch(this.n, { 1:Int8ub, 2:Int16ub, 4:Int32ub })
    d = d.compile()
    benchmark(d.parse, bytes(4), n=4)

def test_class_switch_build(benchmark):
    d = Switch(this.n, { 1:Int8ub, 2:Int16ub, 4:Int32ub })
    benchmark(d.build, 0, n=4)

# - raises exception
# StopIf

def test_class_padding_parse(benchmark):
    d = Padding(100)
    benchmark(d.parse, bytes(100))

def test_class_padding_parse_compiled(benchmark):
    d = Padding(100)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_padding_build(benchmark):
    d = Padding(100)
    benchmark(d.build, None)

def test_class_padded_parse(benchmark):
    d = Padded(100, Byte)
    benchmark(d.parse, bytes(101))

def test_class_padded_parse_compiled(benchmark):
    d = Padded(100, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(101))

def test_class_padded_build(benchmark):
    d = Padded(100, Byte)
    benchmark(d.build, 0)

def test_class_aligned_parse(benchmark):
    d = Aligned(100, Byte)
    benchmark(d.parse, bytes(100))

def test_class_aligned_parse_compiled(benchmark):
    d = Aligned(100, Byte)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_aligned_build(benchmark):
    d = Aligned(100, Byte)
    benchmark(d.build, 0)

# - combines performance of other fields
# AlignedStruct
# BitStruct

def test_class_pointer_parse(benchmark):
    d = Pointer(8, Bytes(4))
    benchmark(d.parse, bytes(12))

def test_class_pointer_parse_compiled(benchmark):
    d = Pointer(8, Bytes(4))
    d = d.compile()
    benchmark(d.parse, bytes(12))

def test_class_pointer_build(benchmark):
    d = Pointer(8, Bytes(4))
    benchmark(d.build, bytes(4))

def test_class_peek_parse(benchmark):
    d = Sequence(Peek(Int8ub), Peek(Int16ub))
    benchmark(d.parse, bytes(2))

def test_class_peek_parse_compiled(benchmark):
    d = Sequence(Peek(Int8ub), Peek(Int16ub))
    d = d.compile()
    benchmark(d.parse, bytes(2))

def test_class_peek_build(benchmark):
    d = Sequence(Peek(Int8ub), Peek(Int16ub))
    benchmark(d.build, [None,None])

# - not worth measuring
# Seek
# Tell
# Pass
# Terminated

def test_class_rawcopy_parse(benchmark):
    d = RawCopy(Byte)
    benchmark(d.parse, bytes(1))

def test_class_rawcopy_parse_compiled(benchmark):
    d = RawCopy(Byte)
    d = d.compile()
    benchmark(d.parse, bytes(1))

def test_class_rawcopy_build1(benchmark):
    d = RawCopy(Byte)
    benchmark(d.build, dict(data=bytes(1)))

def test_class_rawcopy_build2(benchmark):
    d = RawCopy(Byte)
    benchmark(d.build, dict(value=0))

def test_class_byteswapped1_parse(benchmark):
    d = ByteSwapped(Bytes(100))
    benchmark(d.parse, bytes(100))

def test_class_byteswapped1_parse_compiled(benchmark):
    d = ByteSwapped(Bytes(100))
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_byteswapped1_build(benchmark):
    d = ByteSwapped(Bytes(100))
    benchmark(d.build, bytes(100))

def test_class_bitsswapped1_parse(benchmark):
    d = BitsSwapped(Bytes(100))
    benchmark(d.parse, bytes(100))

def test_class_bitsswapped1_parse_compiled(benchmark):
    d = BitsSwapped(Bytes(100))
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_bitsswapped1_build(benchmark):
    d = BitsSwapped(Bytes(100))
    benchmark(d.build, bytes(100))

def test_class_bitsswapped2_parse(benchmark):
    d = BitsSwapped(RepeatUntil(obj_ == 1, Byte))
    benchmark(d.parse, bytes(99)+b"\x80")

def test_class_bitsswapped2_parse_compiled(benchmark):
    d = BitsSwapped(RepeatUntil(obj_ == 1, Byte))
    d = d.compile()
    benchmark(d.parse, bytes(99)+b"\x80")

def test_class_bitsswapped2_build(benchmark):
    d = BitsSwapped(RepeatUntil(obj_ == 1, Byte))
    benchmark(d.build, [0 if i<100-1 else 1 for i in range(100)])

def test_class_prefixed_parse(benchmark):
    d = Prefixed(Byte, GreedyBytes)
    benchmark(d.parse, b"\xff"+bytes(255))

def test_class_prefixed_parse_compiled(benchmark):
    d = Prefixed(Byte, GreedyBytes)
    d = d.compile()
    benchmark(d.parse, b"\xff"+bytes(255))

def test_class_prefixed_build(benchmark):
    d = Prefixed(Byte, GreedyBytes)
    benchmark(d.build, bytes(255))

def test_class_prefixedarray_parse(benchmark):
    d = PrefixedArray(Byte, Byte)
    benchmark(d.parse, b"\xff"+bytes(255))

def test_class_prefixedarray_parse_compiled(benchmark):
    d = PrefixedArray(Byte, Byte)
    d = d.compile()
    benchmark(d.parse, b"\xff"+bytes(255))

def test_class_prefixedarray_build(benchmark):
    d = PrefixedArray(Byte, Byte)
    benchmark(d.build, [0]*255)

def test_class_fixedsized_parse(benchmark):
    d = FixedSized(100, GreedyBytes)
    benchmark(d.parse, bytes(100))

def test_class_fixedsized_parse_compiled(benchmark):
    d = FixedSized(100, GreedyBytes)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_fixedsized_build(benchmark):
    d = FixedSized(100, GreedyBytes)
    benchmark(d.build, bytes(100))

def test_class_nullterminated_parse(benchmark):
    d = NullTerminated(GreedyBytes)
    benchmark(d.parse, b'\x01'*99+b'\x00')

def test_class_nullterminated_parse_compiled(benchmark):
    d = NullTerminated(GreedyBytes)
    d = d.compile()
    benchmark(d.parse, b'\x01'*99+b'\x00')

def test_class_nullterminated_build(benchmark):
    d = NullTerminated(GreedyBytes)
    benchmark(d.build, b'\x01'*99)

def test_class_nullstripped_parse(benchmark):
    d = NullStripped(GreedyBytes)
    benchmark(d.parse, bytes(100))

def test_class_nullstripped_parse_compiled(benchmark):
    d = NullStripped(GreedyBytes)
    d = d.compile()
    benchmark(d.parse, bytes(100))

def test_class_nullstripped_build(benchmark):
    d = NullStripped(GreedyBytes)
    benchmark(d.build, bytes(100))

# - measured by other fields
# RestreamData
# Transformed
# - not compilable
# Restreamed
# ProcessXor
# ProcessRotateLeft
# Checksum
# - decompilable
# Compressed
# - not compilable
# Rebuffered

# - not compilable
# LazyBound

# - not compilable
# ExprAdapter
# ExprSymmetricAdapter
# ExprValidator
# OneOf
# NoneOf
# Filter
# Slicing
# Indexing

def test_overall_parse(benchmark):
    d = example
    benchmark(d.parse, exampledata)

def test_overall_parse_compiled(benchmark):
    d = example.compile()
    benchmark(d.parse, exampledata)

def test_overall_build(benchmark):
    d = example
    obj = example.parse(exampledata)
    benchmark(d.build, obj)

def test_overall_build_compiled(benchmark):
    d = example.compile()
    obj = example.parse(exampledata)
    benchmark(d.build, obj)