Skip to content

Commit f391368

Browse files
committed
tests/run-internalbench: Allow running internalbench on hardware.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent df6291f commit f391368

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
@@ -1146,29 +1146,11 @@ def __call__(self, parser, args, value, option):
11461146
args.filters.append((option, re.compile(value)))
11471147

11481148

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

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

0 commit comments

Comments
 (0)