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 |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame^] | 8 | import os |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 9 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 10 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 11 | |
| 12 | import jobset |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 13 | import simplejson |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 14 | import watch_dirs |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 15 | |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 16 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 17 | # SimpleConfig: just compile with CONFIG=config, and run the binary to test |
| 18 | class SimpleConfig(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 19 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 20 | def __init__(self, config): |
| 21 | self.build_config = config |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 22 | self.maxjobs = 32 * multiprocessing.cpu_count() |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 23 | |
| 24 | def run_command(self, binary): |
| 25 | return [binary] |
| 26 | |
| 27 | |
| 28 | # ValgrindConfig: compile with some CONFIG=config, but use valgrind to run |
| 29 | class ValgrindConfig(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 30 | |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 31 | def __init__(self, config, tool): |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 32 | self.build_config = config |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 33 | self.tool = tool |
| 34 | self.maxjobs = 4 * multiprocessing.cpu_count() |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 35 | |
| 36 | def run_command(self, binary): |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 37 | return ['valgrind', binary, '--tool=%s' % self.tool] |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | # different configurations we can run under |
| 41 | _CONFIGS = { |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 42 | 'dbg': SimpleConfig('dbg'), |
| 43 | 'opt': SimpleConfig('opt'), |
| 44 | 'tsan': SimpleConfig('tsan'), |
| 45 | 'msan': SimpleConfig('msan'), |
| 46 | 'asan': SimpleConfig('asan'), |
| 47 | 'gcov': SimpleConfig('gcov'), |
| 48 | 'memcheck': ValgrindConfig('valgrind', 'memcheck'), |
| 49 | 'helgrind': ValgrindConfig('dbg', 'helgrind') |
| 50 | } |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 51 | |
| 52 | |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 53 | _DEFAULT = ['dbg', 'opt'] |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 54 | _LANGUAGE_TEST_TARGETS = { |
| 55 | 'c++': 'buildtests_cxx', |
| 56 | 'c': 'buildtests_c', |
| 57 | } |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 58 | |
| 59 | # parse command line |
| 60 | argp = argparse.ArgumentParser(description='Run grpc tests.') |
| 61 | argp.add_argument('-c', '--config', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 62 | choices=['all'] + sorted(_CONFIGS.keys()), |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 63 | nargs='+', |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 64 | default=_DEFAULT) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 65 | argp.add_argument('-t', '--test-filter', nargs='*', default=['*']) |
| 66 | argp.add_argument('-n', '--runs_per_test', default=1, type=int) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 67 | argp.add_argument('-f', '--forever', |
| 68 | default=False, |
| 69 | action='store_const', |
| 70 | const=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 71 | argp.add_argument('--newline_on_success', |
| 72 | default=False, |
| 73 | action='store_const', |
| 74 | const=True) |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 75 | argp.add_argument('-l', '--language', |
| 76 | choices=sorted(_LANGUAGE_TEST_TARGETS.keys()), |
| 77 | nargs='+', |
| 78 | default=sorted(_LANGUAGE_TEST_TARGETS.keys())) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 79 | args = argp.parse_args() |
| 80 | |
| 81 | # grab config |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 82 | run_configs = set(_CONFIGS[cfg] |
| 83 | for cfg in itertools.chain.from_iterable( |
| 84 | _CONFIGS.iterkeys() if x == 'all' else [x] |
| 85 | for x in args.config)) |
| 86 | build_configs = set(cfg.build_config for cfg in run_configs) |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 87 | make_targets = set(_LANGUAGE_TEST_TARGETS[x] for x in args.language) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 88 | filters = args.test_filter |
| 89 | runs_per_test = args.runs_per_test |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 90 | forever = args.forever |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 91 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 92 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 93 | class TestCache(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 94 | """Cache for running tests.""" |
| 95 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 96 | def __init__(self): |
| 97 | self._last_successful_run = {} |
| 98 | |
| 99 | def should_run(self, cmdline, bin_hash): |
| 100 | cmdline = ' '.join(cmdline) |
| 101 | if cmdline not in self._last_successful_run: |
| 102 | return True |
| 103 | if self._last_successful_run[cmdline] != bin_hash: |
| 104 | return True |
| 105 | return False |
| 106 | |
| 107 | def finished(self, cmdline, bin_hash): |
| 108 | self._last_successful_run[' '.join(cmdline)] = bin_hash |
| 109 | |
| 110 | def dump(self): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 111 | return [{'cmdline': k, 'hash': v} |
| 112 | for k, v in self._last_successful_run.iteritems()] |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 113 | |
| 114 | def parse(self, exdump): |
| 115 | self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump) |
| 116 | |
| 117 | def save(self): |
| 118 | with open('.run_tests_cache', 'w') as f: |
| 119 | f.write(simplejson.dumps(self.dump())) |
| 120 | |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame^] | 121 | def maybe_load(self): |
| 122 | if os.path.exists('.run_tests_cache'): |
| 123 | with open('.run_tests_cache') as f: |
| 124 | self.parse(simplejson.loads(f.read())) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def _build_and_run(check_cancelled, newline_on_success, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 128 | """Do one pass of building & running tests.""" |
| 129 | # build latest, sharing cpu between the various makes |
| 130 | if not jobset.run( |
| 131 | (['make', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 132 | '-j', '%d' % (multiprocessing.cpu_count() + 1), |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 133 | 'CONFIG=%s' % cfg] + list(make_targets) |
Craig Tiller | be56a96 | 2015-01-14 11:33:03 -0800 | [diff] [blame] | 134 | for cfg in build_configs), |
Craig Tiller | 7c1d7f7 | 2015-01-12 17:08:33 -0800 | [diff] [blame] | 135 | check_cancelled, maxjobs=1): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 136 | return 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 137 | |
| 138 | # run all the tests |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 139 | if not jobset.run( |
| 140 | itertools.ifilter( |
| 141 | lambda x: x is not None, ( |
| 142 | config.run_command(x) |
| 143 | for config in run_configs |
| 144 | for filt in filters |
| 145 | for x in itertools.chain.from_iterable(itertools.repeat( |
| 146 | glob.glob('bins/%s/%s_test' % ( |
| 147 | config.build_config, filt)), |
| 148 | runs_per_test)))), |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 149 | check_cancelled, |
| 150 | newline_on_success=newline_on_success, |
| 151 | maxjobs=min(c.maxjobs for c in run_configs), |
| 152 | cache=cache): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 153 | return 2 |
| 154 | |
| 155 | return 0 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 156 | |
| 157 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 158 | test_cache = (None if runs_per_test != 1 |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 159 | or 'gcov' in build_configs |
| 160 | or 'valgrind' in build_configs |
| 161 | else TestCache()) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 162 | if test_cache: |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame^] | 163 | test_cache.maybe_load() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 164 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 165 | if forever: |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 166 | success = True |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 167 | while True: |
| 168 | dw = watch_dirs.DirWatcher(['src', 'include', 'test']) |
| 169 | initial_time = dw.most_recent_change() |
| 170 | have_files_changed = lambda: dw.most_recent_change() != initial_time |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 171 | previous_success = success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 172 | success = _build_and_run(check_cancelled=have_files_changed, |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 173 | newline_on_success=False, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 174 | cache=test_cache) == 0 |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 175 | if not previous_success and success: |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 176 | jobset.message('SUCCESS', |
| 177 | 'All tests are now passing properly', |
| 178 | do_newline=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 179 | jobset.message('IDLE', 'No change detected') |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 180 | while not have_files_changed(): |
| 181 | time.sleep(1) |
| 182 | else: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 183 | result = _build_and_run(check_cancelled=lambda: False, |
| 184 | newline_on_success=args.newline_on_success, |
| 185 | cache=test_cache) |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 186 | if result == 0: |
| 187 | jobset.message('SUCCESS', 'All tests passed', do_newline=True) |
| 188 | else: |
| 189 | jobset.message('FAILED', 'Some tests failed', do_newline=True) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 190 | test_cache.save() |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 191 | sys.exit(result) |