8
8
from glob import glob
9
9
from collections import defaultdict
10
10
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
+
11
18
if os .name == "nt" :
12
19
MICROPYTHON = os .getenv (
13
20
"MICROPY_MICROPYTHON" , "../ports/windows/build-standard/micropython.exe"
@@ -22,6 +29,7 @@ def run_tests(pyb, test_dict):
22
29
23
30
for base_test , tests in sorted (test_dict .items ()):
24
31
print (base_test + ":" )
32
+ baseline = None
25
33
for test_file in tests :
26
34
# run MicroPython
27
35
if pyb is None :
@@ -36,20 +44,25 @@ def run_tests(pyb, test_dict):
36
44
# run on pyboard
37
45
pyb .enter_raw_repl ()
38
46
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 " )
40
48
except pyboard .PyboardError :
41
49
output_mupy = b"CRASH"
42
50
43
- output_mupy = float (output_mupy .strip ())
51
+ try :
52
+ output_mupy = float (output_mupy .strip ())
53
+ except ValueError :
54
+ output_mupy = - 1
44
55
test_file [1 ] = output_mupy
45
56
testcase_count += 1
46
57
47
- test_count += 1
48
- baseline = None
49
- for t in tests :
50
58
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
53
66
54
67
print ("{} tests performed ({} individual testcases)" .format (test_count , testcase_count ))
55
68
@@ -58,27 +71,70 @@ def run_tests(pyb, test_dict):
58
71
59
72
60
73
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
+ )
63
120
cmd_parser .add_argument ("files" , nargs = "*" , help = "input test files" )
64
121
args = cmd_parser .parse_args ()
65
122
66
123
# 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
+ )
74
127
75
128
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 :
77
132
# run PC tests
78
133
test_dirs = ("internal_bench" ,)
79
134
else :
80
135
# run pyboard tests
81
136
test_dirs = ("basics" , "float" , "pyb" )
137
+
82
138
tests = sorted (
83
139
test_file
84
140
for test_files in (glob ("{}/*.py" .format (dir )) for dir in test_dirs )
@@ -95,6 +151,10 @@ def main():
95
151
continue
96
152
test_dict [m .group (1 )].append ([t , None ])
97
153
154
+ if pyb is not None :
155
+ print ("Uploading bench harness." )
156
+ pyb .fs_put ('internal_bench/bench_hw.py' , 'bench.py' )
157
+
98
158
if not run_tests (pyb , test_dict ):
99
159
sys .exit (1 )
100
160
0 commit comments