Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser performance test #556

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions test/run_parser_perf_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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')
eliben marked this conversation as resolved.
Show resolved Hide resolved
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
Loading