Refactor scripts in Tools/peg_generator/scripts (GH-20401)

This commit is contained in:
Lysandros Nikolaou 2020-06-06 07:21:40 +03:00 committed by GitHub
parent 2e6593db00
commit ba6fd87e41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 146 additions and 147 deletions

View file

@ -24,7 +24,7 @@ argparser = argparse.ArgumentParser(
argparser.add_argument(
"--parser",
action="store",
choices=["pegen", "cpython"],
choices=["new", "old"],
default="pegen",
help="Which parser to benchmark (default is pegen)",
)
@ -40,7 +40,12 @@ subcommands = argparser.add_subparsers(title="Benchmarks", dest="subcommand")
command_compile = subcommands.add_parser(
"compile", help="Benchmark parsing and compiling to bytecode"
)
command_parse = subcommands.add_parser("parse", help="Benchmark parsing and generating an ast.AST")
command_parse = subcommands.add_parser(
"parse", help="Benchmark parsing and generating an ast.AST"
)
command_notree = subcommands.add_parser(
"notree", help="Benchmark parsing and dumping the tree"
)
def benchmark(func):
@ -62,7 +67,7 @@ def benchmark(func):
@benchmark
def time_compile(source, parser):
if parser == "cpython":
if parser == "old":
return _peg_parser.compile_string(
source,
oldparser=True,
@ -73,32 +78,40 @@ def time_compile(source, parser):
@benchmark
def time_parse(source, parser):
if parser == "cpython":
if parser == "old":
return _peg_parser.parse_string(source, oldparser=True)
else:
return _peg_parser.parse_string(source)
@benchmark
def time_notree(source, parser):
if parser == "old":
return _peg_parser.parse_string(source, oldparser=True, ast=False)
else:
return _peg_parser.parse_string(source, ast=False)
def run_benchmark_xxl(subcommand, parser, source):
if subcommand == "compile":
time_compile(source, parser)
elif subcommand == "parse":
time_parse(source, parser)
elif subcommand == "notree":
time_notree(source, parser)
def run_benchmark_stdlib(subcommand, parser):
modes = {"compile": 2, "parse": 1, "notree": 0}
for _ in range(3):
parse_directory(
"../../Lib",
"../../Grammar/python.gram",
"../../Grammar/Tokens",
verbose=False,
excluded_files=["*/bad*", "*/lib2to3/tests/data/*",],
skip_actions=False,
tree_arg=0,
short=True,
mode=2 if subcommand == "compile" else 1,
parser=parser,
mode=modes[subcommand],
oldparser=(parser == "old"),
)