|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from pytest_benchmark.fixture import BenchmarkFixture |
| 9 | + |
| 10 | + from dissect.cstruct.cstruct import cstruct |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.benchmark |
| 14 | +def test_benchmark_basic(cs: cstruct, compiled: bool, benchmark: BenchmarkFixture) -> None: |
| 15 | + """Benchmark the parsing of a simple struct with both the compiled and interpreted backends.""" |
| 16 | + cdef = """ |
| 17 | + struct test { |
| 18 | + uint32 a; |
| 19 | + uint64 b; |
| 20 | + uint16 c; |
| 21 | + uint8 d; |
| 22 | + }; |
| 23 | + """ |
| 24 | + cs.load(cdef, compiled=compiled) |
| 25 | + |
| 26 | + benchmark(lambda: cs.test(b"\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x04")) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.benchmark |
| 30 | +def test_benchmark_union(cs: cstruct, compiled: bool, benchmark: BenchmarkFixture) -> None: |
| 31 | + """Benchmark the parsing of a simple union with both the compiled and interpreted backends.""" |
| 32 | + cdef = """ |
| 33 | + union test { |
| 34 | + uint32 a; |
| 35 | + uint64 b; |
| 36 | + uint16 c; |
| 37 | + uint8 d; |
| 38 | + }; |
| 39 | + """ |
| 40 | + cs.load(cdef, compiled=compiled) |
| 41 | + |
| 42 | + benchmark(lambda: cs.test(b"\x01\x02\x03\x04\x05\x06\x07\x08")) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.benchmark |
| 46 | +def test_benchmark_attribute_access(cs: cstruct, benchmark: BenchmarkFixture) -> None: |
| 47 | + """Benchmark the attribute access of a parsed struct.""" |
| 48 | + cdef = """ |
| 49 | + struct test { |
| 50 | + uint32 a; |
| 51 | + uint64 b; |
| 52 | + uint16 c; |
| 53 | + uint8 d; |
| 54 | + }; |
| 55 | + """ |
| 56 | + cs.load(cdef) |
| 57 | + obj = cs.test(b"\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x04") |
| 58 | + |
| 59 | + benchmark(lambda: (obj.a, obj.b, obj.c, obj.d)) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.benchmark |
| 63 | +def test_benchmark_getattr_constants(cs: cstruct, benchmark: BenchmarkFixture) -> None: |
| 64 | + """Benchmark the resolving of constants on the cstruct instance.""" |
| 65 | + cdef = """ |
| 66 | + #define CONST1 1 |
| 67 | + """ |
| 68 | + cs.load(cdef) |
| 69 | + |
| 70 | + benchmark(lambda: cs.CONST1) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.benchmark |
| 74 | +def test_benchmark_getattr_types(cs: cstruct, benchmark: BenchmarkFixture) -> None: |
| 75 | + """Benchmark the resolving of types on the cstruct instance.""" |
| 76 | + benchmark(lambda: cs.uint8) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.benchmark |
| 80 | +def test_benchmark_getattr_typedefs(cs: cstruct, benchmark: BenchmarkFixture) -> None: |
| 81 | + """Benchmark the resolving of typedefs on the cstruct instance.""" |
| 82 | + cdef = """ |
| 83 | + typedef uint8 my_uint8; |
| 84 | + """ |
| 85 | + cs.load(cdef) |
| 86 | + |
| 87 | + benchmark(lambda: cs.my_uint8) |
0 commit comments