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