Skip to content

Commit 270b002

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

File tree

2 files changed

+89
-17
lines changed

2 files changed

+89
-17
lines changed

tests/internal_bench/bench_hw.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import utime
2+
3+
4+
ITERS = 200000
5+
6+
7+
def run(f):
8+
t = utime.ticks_us()
9+
f(ITERS)
10+
t = utime.ticks_us() - t
11+
s, us = divmod(t, 1_000_000)
12+
print(f"{s}.{us:06}")

tests/run-internalbench.py

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
from glob import glob
99
from collections import defaultdict
1010

11+
import importlib
12+
13+
run_tests_py = importlib.import_module("run-tests")
14+
15+
sys.path.append(run_tests_py.base_path("../tools"))
16+
import pyboard
17+
1118
if os.name == "nt":
1219
MICROPYTHON = os.getenv(
1320
"MICROPY_MICROPYTHON", "../ports/windows/build-standard/micropython.exe"
@@ -22,6 +29,7 @@ def run_tests(pyb, test_dict):
2229

2330
for base_test, tests in sorted(test_dict.items()):
2431
print(base_test + ":")
32+
baseline = None
2533
for test_file in tests:
2634
# run MicroPython
2735
if pyb is None:
@@ -36,20 +44,25 @@ def run_tests(pyb, test_dict):
3644
# run on pyboard
3745
pyb.enter_raw_repl()
3846
try:
39-
output_mupy = pyb.execfile(test_file).replace(b"\r\n", b"\n")
47+
output_mupy = pyb.execfile(test_file[0]).replace(b"\r\n", b"\n")
4048
except pyboard.PyboardError:
4149
output_mupy = b"CRASH"
4250

43-
output_mupy = float(output_mupy.strip())
51+
try:
52+
output_mupy = float(output_mupy.strip())
53+
except ValueError:
54+
output_mupy = -1
4455
test_file[1] = output_mupy
4556
testcase_count += 1
4657

47-
test_count += 1
48-
baseline = None
49-
for t in tests:
5058
if baseline is None:
51-
baseline = t[1]
52-
print(" %.3fs (%+06.2f%%) %s" % (t[1], (t[1] * 100 / baseline) - 100, t[0]))
59+
baseline = test_file[1]
60+
print(
61+
" %.3fs (%+06.2f%%) %s"
62+
% (test_file[1], (test_file[1] * 100 / baseline) - 100, test_file[0])
63+
)
64+
65+
test_count += 1
5366

5467
print("{} tests performed ({} individual testcases)".format(test_count, testcase_count))
5568

@@ -58,27 +71,70 @@ def run_tests(pyb, test_dict):
5871

5972

6073
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")
74+
cmd_parser = argparse.ArgumentParser(
75+
formatter_class=argparse.RawDescriptionHelpFormatter,
76+
description="""Run benchmarks for MicroPython.
77+
78+
By default the tests are run against the unix port of MicroPython. To run it
79+
against something else, use the -t option. See below for details.
80+
""",
81+
epilog="""\
82+
The -t option accepts the following for the test instance:
83+
- unix - use the unix port of MicroPython, specified by the MICROPY_MICROPYTHON
84+
environment variable (which defaults to the standard variant of either the unix
85+
or windows ports, depending on the host platform)
86+
- webassembly - use the webassembly port of MicroPython, specified by the
87+
MICROPY_MICROPYTHON_MJS environment variable (which defaults to the standard
88+
variant of the webassembly port)
89+
- port:<device> - connect to and use the given serial port device
90+
- a<n> - connect to and use /dev/ttyACM<n>
91+
- u<n> - connect to and use /dev/ttyUSB<n>
92+
- c<n> - connect to and use COM<n>
93+
- exec:<command> - execute a command and attach to its stdin/stdout
94+
- execpty:<command> - execute a command and attach to the printed /dev/pts/<n> device
95+
- <a>.<b>.<c>.<d> - connect to the given IPv4 address
96+
- anything else specifies a serial port
97+
98+
Options -i and -e can be multiple and processed in the order given. Regex
99+
"search" (vs "match") operation is used. An action (include/exclude) of
100+
the last matching regex is used:
101+
run-tests.py -i async - exclude all, then include tests containing "async" anywhere
102+
run-tests.py -e '/big.+int' - include all, then exclude by regex
103+
run-tests.py -e async -i async_foo - include all, exclude async, yet still include async_foo
104+
""",
105+
)
106+
cmd_parser.add_argument(
107+
"-t", "--test-instance", default="unix", help="the MicroPython instance to test"
108+
)
109+
cmd_parser.add_argument(
110+
"-b", "--baudrate", default=115200, help="the baud rate of the serial device"
111+
)
112+
cmd_parser.add_argument("-u", "--user", default="micro", help="the telnet login username")
113+
cmd_parser.add_argument("-p", "--password", default="python", help="the telnet login password")
114+
cmd_parser.add_argument(
115+
"-d", "--test-dirs", nargs="*", help="input test directories (if no files given)"
116+
)
117+
cmd_parser.add_argument(
118+
"--denominator", type=int, default=1, help="divide input iterations by this value"
119+
)
63120
cmd_parser.add_argument("files", nargs="*", help="input test files")
64121
args = cmd_parser.parse_args()
65122

66123
# 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
124+
pyb = run_tests_py.get_test_instance(
125+
args.test_instance, args.baudrate, args.user, args.password
126+
)
74127

75128
if len(args.files) == 0:
76-
if pyb is None:
129+
if args.test_dirs:
130+
test_dirs = tuple(args.test_dirs)
131+
elif pyb is None:
77132
# run PC tests
78133
test_dirs = ("internal_bench",)
79134
else:
80135
# run pyboard tests
81136
test_dirs = ("basics", "float", "pyb")
137+
82138
tests = sorted(
83139
test_file
84140
for test_files in (glob("{}/*.py".format(dir)) for dir in test_dirs)
@@ -95,6 +151,10 @@ def main():
95151
continue
96152
test_dict[m.group(1)].append([t, None])
97153

154+
if pyb is not None:
155+
print("Uploading bench harness.")
156+
pyb.fs_put('internal_bench/bench_hw.py', 'bench.py')
157+
98158
if not run_tests(pyb, test_dict):
99159
sys.exit(1)
100160

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy