Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Run the test suite using a separate process for each test file. |
| 5 | """ |
| 6 | |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 7 | import os, sys, platform |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 8 | import Queue, threading |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 9 | import multiprocessing |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 10 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 11 | from optparse import OptionParser |
| 12 | |
| 13 | # Command template of the invocation of the test driver. |
Todd Fiala | a4ec2fc | 2014-06-02 17:49:35 +0000 | [diff] [blame^] | 14 | template = '%s %s/dotest.py %s -p %s %s' |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 15 | |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 16 | def process_dir(root, files, test_root, dotest_options): |
| 17 | """Examine a directory for tests, and invoke any found within it.""" |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 18 | failed = [] |
| 19 | passed = [] |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 20 | for name in files: |
| 21 | path = os.path.join(root, name) |
| 22 | |
| 23 | # We're only interested in the test file with the "Test*.py" naming pattern. |
| 24 | if not name.startswith("Test") or not name.endswith(".py"): |
| 25 | continue |
| 26 | |
| 27 | # Neither a symbolically linked file. |
| 28 | if os.path.islink(path): |
| 29 | continue |
| 30 | |
Todd Fiala | a4ec2fc | 2014-06-02 17:49:35 +0000 | [diff] [blame^] | 31 | command = template % (sys.executable, test_root, dotest_options if dotest_options else "", name, root) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 32 | if 0 != os.system(command): |
| 33 | failed.append(name) |
| 34 | else: |
| 35 | passed.append(name) |
| 36 | return (failed, passed) |
| 37 | |
| 38 | in_q = None |
| 39 | out_q = None |
| 40 | |
| 41 | def process_dir_worker(): |
| 42 | """Worker thread main loop when in multithreaded mode. |
| 43 | Takes one directory specification at a time and works on it.""" |
| 44 | while True: |
| 45 | (root, files, test_root, dotest_options) = in_q.get() |
| 46 | (dir_failed, dir_passed) = process_dir(root, files, test_root, dotest_options) |
| 47 | out_q.put((dir_failed, dir_passed)) |
| 48 | in_q.task_done() |
| 49 | |
| 50 | def walk_and_invoke(test_root, dotest_options, num_threads): |
| 51 | """Look for matched files and invoke test driver on each one. |
| 52 | In single-threaded mode, each test driver is invoked directly. |
| 53 | In multi-threaded mode, submit each test driver to a worker |
| 54 | queue, and then wait for all to complete.""" |
| 55 | failed = [] |
| 56 | passed = [] |
| 57 | if (num_threads > 1): |
Ed Maste | 03c9207 | 2014-03-25 15:17:23 +0000 | [diff] [blame] | 58 | print("Running multithreaded with %d threads" % num_threads) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 59 | global in_q |
| 60 | global out_q |
| 61 | in_q = Queue.Queue() |
| 62 | out_q = Queue.Queue() |
| 63 | for i in range(num_threads): |
| 64 | t = threading.Thread(target=process_dir_worker) |
| 65 | t.daemon = True |
| 66 | t.start() |
Steve Pucci | 44ba171 | 2014-03-16 18:23:59 +0000 | [diff] [blame] | 67 | else: |
Ed Maste | 03c9207 | 2014-03-25 15:17:23 +0000 | [diff] [blame] | 68 | print("Running single-threaded") |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 69 | for root, dirs, files in os.walk(test_root, topdown=False): |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 70 | if (num_threads > 1): |
| 71 | in_q.put((root, files, test_root, dotest_options)) |
| 72 | else: |
| 73 | (dir_failed, dir_passed) = process_dir(root, files, test_root, dotest_options) |
| 74 | failed += dir_failed |
| 75 | passed += dir_passed |
| 76 | if (num_threads > 1): |
| 77 | in_q.join() |
| 78 | while not out_q.empty(): |
| 79 | (dir_failed, dir_passed) = out_q.get() |
| 80 | failed += dir_failed |
| 81 | passed += dir_passed |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 82 | return (failed, passed) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 83 | |
| 84 | def main(): |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 85 | test_root = sys.path[0] |
| 86 | |
| 87 | parser = OptionParser(usage="""\ |
| 88 | Run lldb test suite using a separate process for each test file. |
| 89 | """) |
| 90 | parser.add_option('-o', '--options', |
| 91 | type='string', action='store', |
| 92 | dest='dotest_options', |
| 93 | help="""The options passed to 'dotest.py' if specified.""") |
| 94 | |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 95 | parser.add_option('-t', '--threads', |
| 96 | type='int', |
| 97 | dest='num_threads', |
| 98 | help="""The number of threads to use when running tests separately.""", |
| 99 | default=multiprocessing.cpu_count()) |
| 100 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 101 | opts, args = parser.parse_args() |
| 102 | dotest_options = opts.dotest_options |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 103 | num_threads = opts.num_threads |
| 104 | if num_threads < 1: |
| 105 | num_threads_str = os.environ.get("LLDB_TEST_THREADS") |
| 106 | if num_threads_str: |
| 107 | num_threads = int(num_threads_str) |
| 108 | if num_threads < 1: |
| 109 | num_threads = 1 |
| 110 | else: |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 111 | num_threads = 1 |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 112 | |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 113 | system_info = " ".join(platform.uname()) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 114 | (failed, passed) = walk_and_invoke(test_root, dotest_options, num_threads) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 115 | num_tests = len(failed) + len(passed) |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 116 | |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 117 | print "Ran %d tests." % num_tests |
| 118 | if len(failed) > 0: |
| 119 | print "Failing Tests (%d)" % len(failed) |
| 120 | for f in failed: |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 121 | print "FAIL: LLDB (suite) :: %s (%s)" % (f, system_info) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 122 | sys.exit(1) |
| 123 | sys.exit(0) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main() |