8
8
from glob import glob
9
9
from collections import defaultdict
10
10
11
+ run_tests_module = __import__ ("run-tests" )
12
+ sys .path .append (run_tests_module .base_path ("../tools" ))
13
+ import pyboard
14
+
11
15
if os .name == "nt" :
12
16
MICROPYTHON = os .getenv (
13
17
"MICROPY_MICROPYTHON" , "../ports/windows/build-standard/micropython.exe"
14
18
)
15
19
else :
16
20
MICROPYTHON = os .getenv ("MICROPY_MICROPYTHON" , "../ports/unix/build-standard/micropython" )
17
21
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
+
18
40
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 ):
20
49
test_count = 0
21
50
testcase_count = 0
22
51
23
52
for base_test , tests in sorted (test_dict .items ()):
24
53
print (base_test + ":" )
54
+ baseline = None
25
55
for test_file in tests :
26
56
# run MicroPython
27
57
if pyb is None :
@@ -36,20 +66,25 @@ def run_tests(pyb, test_dict):
36
66
# run on pyboard
37
67
pyb .enter_raw_repl ()
38
68
try :
39
- output_mupy = pyb . execfile ( test_file ). replace ( b" \r \n " , b" \n " )
69
+ output_mupy = execbench ( pyb , test_file [ 0 ], iters )
40
70
except pyboard .PyboardError :
41
71
output_mupy = b"CRASH"
42
72
43
- output_mupy = float (output_mupy .strip ())
73
+ try :
74
+ output_mupy = float (output_mupy .strip ())
75
+ except ValueError :
76
+ output_mupy = - 1
44
77
test_file [1 ] = output_mupy
45
78
testcase_count += 1
46
79
47
- test_count += 1
48
- baseline = None
49
- for t in tests :
50
80
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
53
88
54
89
print ("{} tests performed ({} individual testcases)" .format (test_count , testcase_count ))
55
90
@@ -58,27 +93,47 @@ def run_tests(pyb, test_dict):
58
93
59
94
60
95
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
+ )
63
123
cmd_parser .add_argument ("files" , nargs = "*" , help = "input test files" )
64
124
args = cmd_parser .parse_args ()
65
125
66
126
# 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
+ )
74
130
75
131
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 )
79
134
else :
80
- # run pyboard tests
81
- test_dirs = ( "basics" , "float" , "pyb" )
135
+ test_dirs = ( "internal_bench" ,)
136
+
82
137
tests = sorted (
83
138
test_file
84
139
for test_files in (glob ("{}/*.py" .format (dir )) for dir in test_dirs )
@@ -95,7 +150,7 @@ def main():
95
150
continue
96
151
test_dict [m .group (1 )].append ([t , None ])
97
152
98
- if not run_tests (pyb , test_dict ):
153
+ if not run_tests (pyb , test_dict , args . iters ):
99
154
sys .exit (1 )
100
155
101
156
0 commit comments