From 8a1f050771c010ada8eec8211ade1c77d91196c7 Mon Sep 17 00:00:00 2001 From: Anson Mansfield Date: Thu, 11 Sep 2025 11:07:25 -0400 Subject: [PATCH] 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 --- tests/run-internalbench.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/tests/run-internalbench.py b/tests/run-internalbench.py index 4f2d29525d..715b0ffc9e 100755 --- a/tests/run-internalbench.py +++ b/tests/run-internalbench.py @@ -21,8 +21,14 @@ if os.name == "nt": MICROPYTHON = os.getenv( "MICROPY_MICROPYTHON", "../ports/windows/build-standard/micropython.exe" ) + CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") else: MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython") + CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") + +MICROPYTHON_CMD = [MICROPYTHON, "-X", "emit=bytecode"] +CPYTHON3_CMD = [CPYTHON3, "-BS"] + injected_bench_code = b""" import time @@ -43,14 +49,14 @@ sys.modules['bench'] = bench_class """ -def execbench(pyb, filename, iters): +def execbench(test_instance, filename, iters): with open(filename, "rb") as f: pyfile = f.read() code = (injected_bench_code + pyfile).replace(b"20000000", str(iters).encode("utf-8")) - return pyb.exec(code).replace(b"\r\n", b"\n") + return test_instance.exec(code).replace(b"\r\n", b"\n") -def run_tests(pyb, test_dict, iters): +def run_tests(test_instance, test_dict, iters): test_count = 0 testcase_count = 0 @@ -59,19 +65,17 @@ def run_tests(pyb, test_dict, iters): baseline = None for test_file in tests: # run MicroPython - if pyb is None: + if isinstance(test_instance, list): # run on PC try: - output_mupy = subprocess.check_output( - [MICROPYTHON, "-X", "emit=bytecode", test_file[0]] - ) + output_mupy = subprocess.check_output(test_instance + [test_file[0]]) except subprocess.CalledProcessError: output_mupy = b"CRASH" else: # run on pyboard - pyb.enter_raw_repl() + test_instance.enter_raw_repl() try: - output_mupy = execbench(pyb, test_file[0], iters) + output_mupy = execbench(test_instance, test_file[0], iters) except pyboard.PyboardError: output_mupy = b"CRASH" @@ -105,7 +109,7 @@ def main(): {test_instance_description} {test_directory_description} """, - epilog=test_instance_epilog, + epilog=f"""{test_instance_epilog}- cpython - use CPython to run the benchmarks instead\n""", ) cmd_parser.add_argument( "-t", "--test-instance", default="unix", help="the MicroPython instance to test" @@ -128,8 +132,15 @@ def main(): cmd_parser.add_argument("files", nargs="*", help="input test files") args = cmd_parser.parse_args() - # Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping - pyb = get_test_instance(args.test_instance, args.baudrate, args.user, args.password) + if args.test_instance == "cpython": + test_instance = CPYTHON3_CMD + else: + # Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping + test_instance = get_test_instance( + args.test_instance, args.baudrate, args.user, args.password + ) + if test_instance is None: + test_instance = MICROPYTHON_CMD if len(args.files) == 0: if args.test_dirs: @@ -153,7 +164,7 @@ def main(): continue test_dict[m.group(1)].append([t, None]) - if not run_tests(pyb, test_dict, args.iters): + if not run_tests(test_instance, test_dict, args.iters): sys.exit(1)