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