Skip to content

Commit 8a1f050

Browse files
AJMansfielddpgeorge
authored andcommitted
tests/run-internalbench.py: Add option for running against CPython.
Occasionally, it's useful to be able to compare MicroPython's performance figures to CPython's. This change adds the ability to run the internalbench test runner with `--test-instance=cpython` in order to execute the same test routines against CPython and produce a benchmark performance report in the same format as MicroPython. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent f013735 commit 8a1f050

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

tests/run-internalbench.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
MICROPYTHON = os.getenv(
2222
"MICROPY_MICROPYTHON", "../ports/windows/build-standard/micropython.exe"
2323
)
24+
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
2425
else:
2526
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython")
27+
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
28+
29+
MICROPYTHON_CMD = [MICROPYTHON, "-X", "emit=bytecode"]
30+
CPYTHON3_CMD = [CPYTHON3, "-BS"]
31+
2632

2733
injected_bench_code = b"""
2834
import time
@@ -43,14 +49,14 @@ def run(test):
4349
"""
4450

4551

46-
def execbench(pyb, filename, iters):
52+
def execbench(test_instance, filename, iters):
4753
with open(filename, "rb") as f:
4854
pyfile = f.read()
4955
code = (injected_bench_code + pyfile).replace(b"20000000", str(iters).encode("utf-8"))
50-
return pyb.exec(code).replace(b"\r\n", b"\n")
56+
return test_instance.exec(code).replace(b"\r\n", b"\n")
5157

5258

53-
def run_tests(pyb, test_dict, iters):
59+
def run_tests(test_instance, test_dict, iters):
5460
test_count = 0
5561
testcase_count = 0
5662

@@ -59,19 +65,17 @@ def run_tests(pyb, test_dict, iters):
5965
baseline = None
6066
for test_file in tests:
6167
# run MicroPython
62-
if pyb is None:
68+
if isinstance(test_instance, list):
6369
# run on PC
6470
try:
65-
output_mupy = subprocess.check_output(
66-
[MICROPYTHON, "-X", "emit=bytecode", test_file[0]]
67-
)
71+
output_mupy = subprocess.check_output(test_instance + [test_file[0]])
6872
except subprocess.CalledProcessError:
6973
output_mupy = b"CRASH"
7074
else:
7175
# run on pyboard
72-
pyb.enter_raw_repl()
76+
test_instance.enter_raw_repl()
7377
try:
74-
output_mupy = execbench(pyb, test_file[0], iters)
78+
output_mupy = execbench(test_instance, test_file[0], iters)
7579
except pyboard.PyboardError:
7680
output_mupy = b"CRASH"
7781

@@ -105,7 +109,7 @@ def main():
105109
{test_instance_description}
106110
{test_directory_description}
107111
""",
108-
epilog=test_instance_epilog,
112+
epilog=f"""{test_instance_epilog}- cpython - use CPython to run the benchmarks instead\n""",
109113
)
110114
cmd_parser.add_argument(
111115
"-t", "--test-instance", default="unix", help="the MicroPython instance to test"
@@ -128,8 +132,15 @@ def main():
128132
cmd_parser.add_argument("files", nargs="*", help="input test files")
129133
args = cmd_parser.parse_args()
130134

131-
# Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping
132-
pyb = get_test_instance(args.test_instance, args.baudrate, args.user, args.password)
135+
if args.test_instance == "cpython":
136+
test_instance = CPYTHON3_CMD
137+
else:
138+
# Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping
139+
test_instance = get_test_instance(
140+
args.test_instance, args.baudrate, args.user, args.password
141+
)
142+
if test_instance is None:
143+
test_instance = MICROPYTHON_CMD
133144

134145
if len(args.files) == 0:
135146
if args.test_dirs:
@@ -153,7 +164,7 @@ def main():
153164
continue
154165
test_dict[m.group(1)].append([t, None])
155166

156-
if not run_tests(pyb, test_dict, args.iters):
167+
if not run_tests(test_instance, test_dict, args.iters):
157168
sys.exit(1)
158169

159170

0 commit comments

Comments
 (0)