Skip to content

Commit 88cb6bc

Browse files
AJMansfielddpgeorge
authored andcommitted
tests/run-internalbench.py: Allow running internalbench on hardware.
Signed-off-by: Anson Mansfield <[email protected]>
1 parent b9d6d6a commit 88cb6bc

File tree

2 files changed

+109
-44
lines changed

2 files changed

+109
-44
lines changed

tests/run-internalbench.py

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,50 @@
88
from glob import glob
99
from collections import defaultdict
1010

11+
run_tests_module = __import__("run-tests")
12+
sys.path.append(run_tests_module.base_path("../tools"))
13+
import pyboard
14+
1115
if os.name == "nt":
1216
MICROPYTHON = os.getenv(
1317
"MICROPY_MICROPYTHON", "../ports/windows/build-standard/micropython.exe"
1418
)
1519
else:
1620
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython")
1721

22+
injected_bench_code = b"""
23+
import time
24+
25+
class bench_class:
26+
ITERS = 20000000
27+
28+
@staticmethod
29+
def run(test):
30+
t = time.ticks_us()
31+
test(bench_class.ITERS)
32+
t = time.ticks_diff(time.ticks_us(), t)
33+
s, us = divmod(t, 1_000_000)
34+
print("{}.{:06}".format(s, us))
35+
36+
import sys
37+
sys.modules['bench'] = bench_class
38+
"""
39+
1840

19-
def run_tests(pyb, test_dict):
41+
def execbench(pyb, filename, iters):
42+
with open(filename, "rb") as f:
43+
pyfile = f.read()
44+
code = (injected_bench_code + pyfile).replace(b"20000000", str(iters).encode("utf-8"))
45+
return pyb.exec(code).replace(b"\r\n", b"\n")
46+
47+
48+
def run_tests(pyb, test_dict, iters):
2049
test_count = 0
2150
testcase_count = 0
2251

2352
for base_test, tests in sorted(test_dict.items()):
2453
print(base_test + ":")
54+
baseline = None
2555
for test_file in tests:
2656
# run MicroPython
2757
if pyb is None:
@@ -36,20 +66,25 @@ def run_tests(pyb, test_dict):
3666
# run on pyboard
3767
pyb.enter_raw_repl()
3868
try:
39-
output_mupy = pyb.execfile(test_file).replace(b"\r\n", b"\n")
69+
output_mupy = execbench(pyb, test_file[0], iters)
4070
except pyboard.PyboardError:
4171
output_mupy = b"CRASH"
4272

43-
output_mupy = float(output_mupy.strip())
73+
try:
74+
output_mupy = float(output_mupy.strip())
75+
except ValueError:
76+
output_mupy = -1
4477
test_file[1] = output_mupy
4578
testcase_count += 1
4679

47-
test_count += 1
48-
baseline = None
49-
for t in tests:
5080
if baseline is None:
51-
baseline = t[1]
52-
print(" %.3fs (%+06.2f%%) %s" % (t[1], (t[1] * 100 / baseline) - 100, t[0]))
81+
baseline = test_file[1]
82+
print(
83+
" %.3fs (%+06.2f%%) %s"
84+
% (test_file[1], (test_file[1] * 100 / baseline) - 100, test_file[0])
85+
)
86+
87+
test_count += 1
5388

5489
print("{} tests performed ({} individual testcases)".format(test_count, testcase_count))
5590

@@ -58,27 +93,47 @@ def run_tests(pyb, test_dict):
5893

5994

6095
def main():
61-
cmd_parser = argparse.ArgumentParser(description="Run tests for MicroPython.")
62-
cmd_parser.add_argument("--pyboard", action="store_true", help="run the tests on the pyboard")
96+
cmd_parser = argparse.ArgumentParser(
97+
formatter_class=argparse.RawDescriptionHelpFormatter,
98+
description=f"""Run and manage tests for MicroPython.
99+
100+
{run_tests_module.test_instance_description}
101+
{run_tests_module.test_directory_description}
102+
""",
103+
epilog=run_tests_module.test_instance_epilog,
104+
)
105+
cmd_parser.add_argument(
106+
"-t", "--test-instance", default="unix", help="the MicroPython instance to test"
107+
)
108+
cmd_parser.add_argument(
109+
"-b", "--baudrate", default=115200, help="the baud rate of the serial device"
110+
)
111+
cmd_parser.add_argument("-u", "--user", default="micro", help="the telnet login username")
112+
cmd_parser.add_argument("-p", "--password", default="python", help="the telnet login password")
113+
cmd_parser.add_argument(
114+
"-d", "--test-dirs", nargs="*", help="input test directories (if no files given)"
115+
)
116+
cmd_parser.add_argument(
117+
"-I",
118+
"--iters",
119+
type=int,
120+
default=200_000,
121+
help="number of test iterations, only for remote instances (default 200,000)",
122+
)
63123
cmd_parser.add_argument("files", nargs="*", help="input test files")
64124
args = cmd_parser.parse_args()
65125

66126
# Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping
67-
if args.pyboard:
68-
import pyboard
69-
70-
pyb = pyboard.Pyboard("/dev/ttyACM0")
71-
pyb.enter_raw_repl()
72-
else:
73-
pyb = None
127+
pyb = run_tests_module.get_test_instance(
128+
args.test_instance, args.baudrate, args.user, args.password
129+
)
74130

75131
if len(args.files) == 0:
76-
if pyb is None:
77-
# run PC tests
78-
test_dirs = ("internal_bench",)
132+
if args.test_dirs:
133+
test_dirs = tuple(args.test_dirs)
79134
else:
80-
# run pyboard tests
81-
test_dirs = ("basics", "float", "pyb")
135+
test_dirs = ("internal_bench",)
136+
82137
tests = sorted(
83138
test_file
84139
for test_files in (glob("{}/*.py".format(dir)) for dir in test_dirs)
@@ -95,7 +150,7 @@ def main():
95150
continue
96151
test_dict[m.group(1)].append([t, None])
97152

98-
if not run_tests(pyb, test_dict):
153+
if not run_tests(pyb, test_dict, args.iters):
99154
sys.exit(1)
100155

101156

tests/run-tests.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,29 +1145,11 @@ def __call__(self, parser, args, value, option):
11451145
args.filters.append((option, re.compile(value)))
11461146

11471147

1148-
def main():
1149-
global injected_import_hook_code
1150-
1151-
cmd_parser = argparse.ArgumentParser(
1152-
formatter_class=argparse.RawDescriptionHelpFormatter,
1153-
description="""Run and manage tests for MicroPython.
1154-
1148+
test_instance_description = """\
11551149
By default the tests are run against the unix port of MicroPython. To run it
11561150
against something else, use the -t option. See below for details.
1157-
1158-
Tests are discovered by scanning test directories for .py files or using the
1159-
specified test files. If test files nor directories are specified, the script
1160-
expects to be ran in the tests directory (where this file is located) and the
1161-
builtin tests suitable for the target platform are ran.
1162-
1163-
When running tests, run-tests.py compares the MicroPython output of the test with the output
1164-
produced by running the test through CPython unless a <test>.exp file is found, in which
1165-
case it is used as comparison.
1166-
1167-
If a test fails, run-tests.py produces a pair of <test>.out and <test>.exp files in the result
1168-
directory with the MicroPython output and the expectations, respectively.
1169-
""",
1170-
epilog="""\
1151+
"""
1152+
test_instance_epilog = """\
11711153
The -t option accepts the following for the test instance:
11721154
- unix - use the unix port of MicroPython, specified by the MICROPY_MICROPYTHON
11731155
environment variable (which defaults to the standard variant of either the unix
@@ -1183,7 +1165,35 @@ def main():
11831165
- execpty:<command> - execute a command and attach to the printed /dev/pts/<n> device
11841166
- <a>.<b>.<c>.<d> - connect to the given IPv4 address
11851167
- anything else specifies a serial port
1168+
"""
11861169

1170+
test_directory_description = """\
1171+
Tests are discovered by scanning test directories for .py files or using the
1172+
specified test files. If test files nor directories are specified, the script
1173+
expects to be ran in the tests directory (where this file is located) and the
1174+
builtin tests suitable for the target platform are ran.
1175+
"""
1176+
1177+
1178+
def main():
1179+
global injected_import_hook_code
1180+
1181+
cmd_parser = argparse.ArgumentParser(
1182+
formatter_class=argparse.RawDescriptionHelpFormatter,
1183+
description=f"""Run and manage tests for MicroPython.
1184+
1185+
{test_instance_description}
1186+
{test_directory_description}
1187+
1188+
When running tests, run-tests.py compares the MicroPython output of the test with the output
1189+
produced by running the test through CPython unless a <test>.exp file is found, in which
1190+
case it is used as comparison.
1191+
1192+
If a test fails, run-tests.py produces a pair of <test>.out and <test>.exp files in the result
1193+
directory with the MicroPython output and the expectations, respectively.
1194+
""",
1195+
epilog=f"""\
1196+
{test_instance_epilog}
11871197
Options -i and -e can be multiple and processed in the order given. Regex
11881198
"search" (vs "match") operation is used. An action (include/exclude) of
11891199
the last matching regex is used:

0 commit comments

Comments
 (0)