|
| 1 | +% Packet field performance issue |
| 2 | + |
| 3 | +############ |
| 4 | +############ |
| 5 | ++ Test utils |
| 6 | + |
| 7 | += Prepare a `check_exec_time()` function. |
| 8 | + |
| 9 | +def check_exec_time(description, t0, tf, max): |
| 10 | + print(f"{description}: {tf - t0:.3f} seconds") |
| 11 | + assert tf - t0 <= max, f"{description!r} FAILED: {tf - t0:.3f} > {max:.3f}" |
| 12 | + |
| 13 | + |
| 14 | +############ |
| 15 | +############ |
| 16 | ++ Test data |
| 17 | + |
| 18 | += Define the `C` final packet class. |
| 19 | + |
| 20 | +class C(Packet): |
| 21 | + fields_desc = [ |
| 22 | + ByteField(name="val", default=0), |
| 23 | + ] |
| 24 | + def extract_padding(self, s): |
| 25 | + return (b'', s) |
| 26 | + |
| 27 | += Define the `B` intermediate packet class. |
| 28 | + |
| 29 | +class B(Packet): |
| 30 | + NUMBER_OF_C_PER_B = 100 |
| 31 | + fields_desc = [ |
| 32 | + PacketField(name=f"c{i}", pkt_cls=C, default=C(val=i)) |
| 33 | + for i in range(NUMBER_OF_C_PER_B) |
| 34 | + ] |
| 35 | + def extract_padding(self, s): |
| 36 | + return (b'', s) |
| 37 | + |
| 38 | += Define the `A` root packet class. |
| 39 | + |
| 40 | +class A(Packet): |
| 41 | + NUMBER_OF_B_PER_A = 100 |
| 42 | + fields_desc = [ |
| 43 | + PacketField(name=f"b{i}", pkt_cls=B, default=B()) |
| 44 | + for i in range(NUMBER_OF_B_PER_A) |
| 45 | + ] |
| 46 | + |
| 47 | += Define `MAX_INSTANTIATION_TIME_EXPECTED`. |
| 48 | + |
| 49 | +MAX_INSTANTIATION_TIME_EXPECTED = 1.0 |
| 50 | + |
| 51 | += Define `MAX_SERIALIZATION_TIME_EXPECTED`. |
| 52 | + |
| 53 | +MAX_SERIALIZATION_TIME_EXPECTED = 0.250 |
| 54 | + |
| 55 | += Define `MAX_PARSING_TIME_EXPECTED`. |
| 56 | + |
| 57 | +MAX_PARSING_TIME_EXPECTED = 0.250 |
| 58 | + |
| 59 | + |
| 60 | +############ |
| 61 | +############ |
| 62 | ++ Default instantiations |
| 63 | + |
| 64 | += Build a default instance of `C`. |
| 65 | + |
| 66 | +t0 = time.time() |
| 67 | +c = C() |
| 68 | +tf = time.time() |
| 69 | +check_exec_time("Default instantiation of `C`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 70 | + |
| 71 | += Build a default instance of `C` again. |
| 72 | + |
| 73 | +t0 = time.time() |
| 74 | +c = C() |
| 75 | +tf = time.time() |
| 76 | +check_exec_time("Default instantiation of `C`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 77 | + |
| 78 | += Build a default instance of `B`. |
| 79 | + |
| 80 | +t0 = time.time() |
| 81 | +b = B() |
| 82 | +tf = time.time() |
| 83 | +check_exec_time("Default instantiation of `B`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 84 | + |
| 85 | += Build a default instance of `B` again. |
| 86 | + |
| 87 | +t0 = time.time() |
| 88 | +b = B() |
| 89 | +tf = time.time() |
| 90 | +check_exec_time("Default instantiation of `B`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 91 | + |
| 92 | += Build a default instance of `A`. |
| 93 | + |
| 94 | +t0 = time.time() |
| 95 | +a = A() |
| 96 | +tf = time.time() |
| 97 | +check_exec_time("Default instantiation of `A`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 98 | + |
| 99 | += Build a default instance of `A` again. |
| 100 | + |
| 101 | +t0 = time.time() |
| 102 | +a = A() |
| 103 | +tf = time.time() |
| 104 | +check_exec_time("Default instantiation of `A`", t0, tf, MAX_INSTANTIATION_TIME_EXPECTED) |
| 105 | + |
| 106 | + |
| 107 | +############ |
| 108 | +############ |
| 109 | ++ Serialization |
| 110 | + |
| 111 | += Launch serialization from the latest instance of `A` created. |
| 112 | + |
| 113 | +t0 = time.time() |
| 114 | +b = a.build() |
| 115 | +tf = time.time() |
| 116 | +check_exec_time("Serialization of `A`", t0, tf, MAX_SERIALIZATION_TIME_EXPECTED) |
| 117 | + |
| 118 | + |
| 119 | +############ |
| 120 | +############ |
| 121 | ++ Parsing |
| 122 | + |
| 123 | += Parse the buffer serialized before with the latest instance of `A` created. |
| 124 | + |
| 125 | +t0 = time.time() |
| 126 | +a.dissect(b) |
| 127 | +tf = time.time() |
| 128 | +check_exec_time("Parsing of `A`", t0, tf, MAX_PARSING_TIME_EXPECTED) |
0 commit comments