88from glob import glob
99from 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+
1115if os .name == "nt" :
1216 MICROPYTHON = os .getenv (
1317 "MICROPY_MICROPYTHON" , "../ports/windows/build-standard/micropython.exe"
1418 )
1519else :
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
6095def 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
0 commit comments