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