run-bench-tests 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #! /usr/bin/env python3
  2. import os
  3. import subprocess
  4. import sys
  5. import argparse
  6. import re
  7. from glob import glob
  8. from collections import defaultdict
  9. # Tests require at least CPython 3.3. If your default python3 executable
  10. # is of lower version, you can point MICROPY_CPYTHON3 environment var
  11. # to the correct executable.
  12. if os.name == 'nt':
  13. CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3.exe')
  14. MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/windows/micropython.exe')
  15. else:
  16. CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3')
  17. MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/unix/micropython')
  18. def run_tests(pyb, test_dict):
  19. test_count = 0
  20. testcase_count = 0
  21. for base_test, tests in sorted(test_dict.items()):
  22. print(base_test + ":")
  23. for test_file in tests:
  24. # run MicroPython
  25. if pyb is None:
  26. # run on PC
  27. try:
  28. output_mupy = subprocess.check_output([MICROPYTHON, '-X', 'emit=bytecode', test_file[0]])
  29. except subprocess.CalledProcessError:
  30. output_mupy = b'CRASH'
  31. else:
  32. # run on pyboard
  33. pyb.enter_raw_repl()
  34. try:
  35. output_mupy = pyb.execfile(test_file).replace(b'\r\n', b'\n')
  36. except pyboard.PyboardError:
  37. output_mupy = b'CRASH'
  38. output_mupy = float(output_mupy.strip())
  39. test_file[1] = output_mupy
  40. testcase_count += 1
  41. test_count += 1
  42. baseline = None
  43. for t in tests:
  44. if baseline is None:
  45. baseline = t[1]
  46. print(" %.3fs (%+06.2f%%) %s" % (t[1], (t[1] * 100 / baseline) - 100, t[0]))
  47. print("{} tests performed ({} individual testcases)".format(test_count, testcase_count))
  48. # all tests succeeded
  49. return True
  50. def main():
  51. cmd_parser = argparse.ArgumentParser(description='Run tests for MicroPython.')
  52. cmd_parser.add_argument('--pyboard', action='store_true', help='run the tests on the pyboard')
  53. cmd_parser.add_argument('files', nargs='*', help='input test files')
  54. args = cmd_parser.parse_args()
  55. # Note pyboard support is copied over from run-tests, not testes, and likely needs revamping
  56. if args.pyboard:
  57. import pyboard
  58. pyb = pyboard.Pyboard('/dev/ttyACM0')
  59. pyb.enter_raw_repl()
  60. else:
  61. pyb = None
  62. if len(args.files) == 0:
  63. if pyb is None:
  64. # run PC tests
  65. test_dirs = ('bench',)
  66. else:
  67. # run pyboard tests
  68. test_dirs = ('basics', 'float', 'pyb')
  69. tests = sorted(test_file for test_files in (glob('{}/*.py'.format(dir)) for dir in test_dirs) for test_file in test_files)
  70. else:
  71. # tests explicitly given
  72. tests = sorted(args.files)
  73. test_dict = defaultdict(lambda: [])
  74. for t in tests:
  75. m = re.match(r"(.+?)-(.+)\.py", t)
  76. if not m:
  77. continue
  78. test_dict[m.group(1)].append([t, None])
  79. if not run_tests(pyb, test_dict):
  80. sys.exit(1)
  81. if __name__ == "__main__":
  82. main()