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