Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """Run tests in parallel.""" |
| 3 | |
| 4 | import argparse |
| 5 | import glob |
| 6 | import itertools |
| 7 | import multiprocessing |
| 8 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 9 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 10 | |
| 11 | import jobset |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 12 | import watch_dirs |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 13 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 14 | # SimpleConfig: just compile with CONFIG=config, and run the binary to test |
| 15 | class SimpleConfig(object): |
| 16 | def __init__(self, config): |
| 17 | self.build_config = config |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 18 | self.maxjobs = 32 * multiprocessing.cpu_count() |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 19 | |
| 20 | def run_command(self, binary): |
| 21 | return [binary] |
| 22 | |
| 23 | |
| 24 | # ValgrindConfig: compile with some CONFIG=config, but use valgrind to run |
| 25 | class ValgrindConfig(object): |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 26 | def __init__(self, config, tool): |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 27 | self.build_config = config |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 28 | self.tool = tool |
| 29 | self.maxjobs = 4 * multiprocessing.cpu_count() |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 30 | |
| 31 | def run_command(self, binary): |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 32 | return ['valgrind', binary, '--tool=%s' % self.tool] |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | # different configurations we can run under |
| 36 | _CONFIGS = { |
| 37 | 'dbg': SimpleConfig('dbg'), |
| 38 | 'opt': SimpleConfig('opt'), |
| 39 | 'tsan': SimpleConfig('tsan'), |
| 40 | 'msan': SimpleConfig('msan'), |
| 41 | 'asan': SimpleConfig('asan'), |
Craig Tiller | 934baa3 | 2015-01-12 18:19:45 -0800 | [diff] [blame] | 42 | 'gcov': SimpleConfig('gcov'), |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 43 | 'memcheck': ValgrindConfig('dbg', 'memcheck'), |
| 44 | 'helgrind': ValgrindConfig('dbg', 'helgrind') |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 48 | _DEFAULT = ['dbg', 'opt'] |
Craig Tiller | 7c1d7f7 | 2015-01-12 17:08:33 -0800 | [diff] [blame] | 49 | _MAKE_TEST_TARGETS = ['buildtests_c', 'buildtests_cxx'] |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 50 | |
| 51 | # parse command line |
| 52 | argp = argparse.ArgumentParser(description='Run grpc tests.') |
| 53 | argp.add_argument('-c', '--config', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 54 | choices=['all'] + sorted(_CONFIGS.keys()), |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 55 | nargs='+', |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 56 | default=_DEFAULT) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 57 | argp.add_argument('-t', '--test-filter', nargs='*', default=['*']) |
| 58 | argp.add_argument('-n', '--runs_per_test', default=1, type=int) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 59 | argp.add_argument('-f', '--forever', |
| 60 | default=False, |
| 61 | action='store_const', |
| 62 | const=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 63 | argp.add_argument('--newline_on_success', |
| 64 | default=False, |
| 65 | action='store_const', |
| 66 | const=True) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 67 | args = argp.parse_args() |
| 68 | |
| 69 | # grab config |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 70 | run_configs = set(_CONFIGS[cfg] |
| 71 | for cfg in itertools.chain.from_iterable( |
| 72 | _CONFIGS.iterkeys() if x == 'all' else [x] |
| 73 | for x in args.config)) |
| 74 | build_configs = set(cfg.build_config for cfg in run_configs) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 75 | filters = args.test_filter |
| 76 | runs_per_test = args.runs_per_test |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 77 | forever = args.forever |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 78 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 79 | |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 80 | def _build_and_run(check_cancelled, newline_on_success, forever=False): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 81 | """Do one pass of building & running tests.""" |
| 82 | # build latest, sharing cpu between the various makes |
| 83 | if not jobset.run( |
| 84 | (['make', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 85 | '-j', '%d' % (multiprocessing.cpu_count() + 1), |
Craig Tiller | be56a96 | 2015-01-14 11:33:03 -0800 | [diff] [blame] | 86 | 'CONFIG=%s' % cfg] + _MAKE_TEST_TARGETS |
| 87 | for cfg in build_configs), |
Craig Tiller | 7c1d7f7 | 2015-01-12 17:08:33 -0800 | [diff] [blame] | 88 | check_cancelled, maxjobs=1): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 89 | return 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 90 | |
| 91 | # run all the tests |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 92 | if not jobset.run( |
| 93 | itertools.ifilter( |
| 94 | lambda x: x is not None, ( |
| 95 | config.run_command(x) |
| 96 | for config in run_configs |
| 97 | for filt in filters |
| 98 | for x in itertools.chain.from_iterable(itertools.repeat( |
| 99 | glob.glob('bins/%s/%s_test' % ( |
| 100 | config.build_config, filt)), |
| 101 | runs_per_test)))), |
| 102 | check_cancelled, |
Nicolas Noble | 594ef6c | 2015-01-14 18:02:04 -0800 | [diff] [blame] | 103 | newline_on_success=newline_on_success, |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 104 | maxjobs=min(c.maxjobs for c in run_configs)): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 105 | return 2 |
| 106 | |
| 107 | return 0 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 108 | |
| 109 | |
| 110 | if forever: |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 111 | success = True |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 112 | while True: |
| 113 | dw = watch_dirs.DirWatcher(['src', 'include', 'test']) |
| 114 | initial_time = dw.most_recent_change() |
| 115 | have_files_changed = lambda: dw.most_recent_change() != initial_time |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 116 | previous_success = success |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame^] | 117 | success = _build_and_run(have_files_changed, |
| 118 | newline_on_success=False, |
| 119 | forever=True) == 0 |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 120 | if not previous_success and success: |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame^] | 121 | jobset.message('SUCCESS', |
| 122 | 'All tests are now passing properly', |
| 123 | do_newline=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 124 | jobset.message('IDLE', 'No change detected') |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 125 | while not have_files_changed(): |
| 126 | time.sleep(1) |
| 127 | else: |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame^] | 128 | result = _build_and_run(lambda: False, |
| 129 | newline_on_success=args.newline_on_success) |
| 130 | if result == 0: |
| 131 | jobset.message('SUCCESS', 'All tests passed', do_newline=True) |
| 132 | else: |
| 133 | jobset.message('FAILED', 'Some tests failed', do_newline=True) |
| 134 | sys.exit(result) |