File: run_parser_perf_test.py

package info (click to toggle)
python-pyelftools 0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,964 kB
  • sloc: python: 15,903; ansic: 298; asm: 86; makefile: 24; cpp: 18; sh: 4
file content (71 lines) | stat: -rw-r--r-- 2,673 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
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# test/run_examples_test.py
#
# Run the examples and compare their output to a reference
#
# Seva Alekseyev (sevaa@sprynet.com)
# This code is in the public domain
#
# This runs and times in-memory firehose DWARF parsing on all files from the dwarfdump autotest.
# The idea was to isolate the performance of the struct parsing logic alone.
#-------------------------------------------------------------------------------
from io import BytesIO
import os, sys, time, argparse

from utils import is_in_rootdir

sys.path.insert(0, '.')

from elftools.elf.elffile import ELFFile
from elftools.dwarf.dwarfinfo import DWARFInfo
from elftools.dwarf.locationlists import LocationParser

def parse_dwarf(ef, args):
    di = ef.get_dwarf_info()
    llp = LocationParser(di.location_lists())
    ranges = di.range_lists()
    for cu in di.iter_CUs():
        ver = cu.header.version
        if args.lineprog:
            # No way to isolate lineprog parsing :(
            di.line_program_for_CU(cu).get_entries()
        for die in cu.iter_DIEs():
            for (_, attr) in die.attributes.items():
                if args.locs and LocationParser.attribute_has_location(attr, ver):
                    llp.parse_from_attribute(attr, ver, die)
                elif args.ranges and attr.name == "DW_AT_ranges":
                    if ver >= 5:
                        ranges.get_range_list_at_offset_ex(attr.value)
                    else:
                        ranges.get_range_list_at_offset(attr.value)

def slurp(filename):
    with open(filename, "rb") as file:
        return BytesIO(file.read())

def main():
    if not is_in_rootdir():
        print('Error: Please run me from the root dir of pyelftools!', file=sys.stderr)
        return 1
    
    argparser = argparse.ArgumentParser()
    argparser.add_argument('-l', action='store_true', dest='locs')
    argparser.add_argument('-r', action='store_true', dest='ranges')
    argparser.add_argument('-p', action='store_true', dest='lineprog')
    args = argparser.parse_args()
    
    root = os.path.join('.', 'test', 'testfiles_for_dwarfdump')
    filenames = [filename for filename in os.listdir(root) if os.path.splitext(filename)[1] == '.elf']
    fileblobs = [slurp(os.path.join(root, filename)) for filename in filenames]
    start_time = time.time()
    for stream in fileblobs:
        parse_dwarf(ELFFile(stream), args)
    print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == '__main__':
    sys.exit(main())

# To profile:
# python -m cProfile -s tottime test/run_parser_perf_test.py