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 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 12 | import watch_dirs |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 13 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 14 | |
| 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 |
| 19 | |
| 20 | def run_command(self, binary): |
| 21 | return [binary] |
| 22 | |
| 23 | |
| 24 | # ValgrindConfig: compile with some CONFIG=config, but use valgrind to run |
| 25 | class ValgrindConfig(object): |
| 26 | def __init__(self, config): |
| 27 | self.build_config = config |
| 28 | |
| 29 | def run_command(self, binary): |
| 30 | return ['valgrind', binary] |
| 31 | |
| 32 | |
| 33 | # different configurations we can run under |
| 34 | _CONFIGS = { |
| 35 | 'dbg': SimpleConfig('dbg'), |
| 36 | 'opt': SimpleConfig('opt'), |
| 37 | 'tsan': SimpleConfig('tsan'), |
| 38 | 'msan': SimpleConfig('msan'), |
| 39 | 'asan': SimpleConfig('asan'), |
| 40 | 'valgrind': ValgrindConfig('dbg'), |
| 41 | } |
| 42 | |
| 43 | |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 44 | _DEFAULT = ['dbg', 'opt'] |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 45 | |
| 46 | # parse command line |
| 47 | argp = argparse.ArgumentParser(description='Run grpc tests.') |
| 48 | argp.add_argument('-c', '--config', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 49 | choices=['all'] + sorted(_CONFIGS.keys()), |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 50 | nargs='+', |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 51 | default=_DEFAULT) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 52 | argp.add_argument('-t', '--test-filter', nargs='*', default=['*']) |
| 53 | argp.add_argument('-n', '--runs_per_test', default=1, type=int) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 54 | argp.add_argument('-f', '--forever', |
| 55 | default=False, |
| 56 | action='store_const', |
| 57 | const=True) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 58 | args = argp.parse_args() |
| 59 | |
| 60 | # grab config |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 61 | run_configs = set(_CONFIGS[cfg] |
| 62 | for cfg in itertools.chain.from_iterable( |
| 63 | _CONFIGS.iterkeys() if x == 'all' else [x] |
| 64 | for x in args.config)) |
| 65 | build_configs = set(cfg.build_config for cfg in run_configs) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 66 | filters = args.test_filter |
| 67 | runs_per_test = args.runs_per_test |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 68 | forever = args.forever |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 69 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 70 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 71 | def _build_and_run(check_cancelled): |
| 72 | """Do one pass of building & running tests.""" |
| 73 | # build latest, sharing cpu between the various makes |
| 74 | if not jobset.run( |
| 75 | (['make', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 76 | '-j', '%d' % (multiprocessing.cpu_count() + 1), |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 77 | 'buildtests_c', |
| 78 | 'CONFIG=%s' % cfg] |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 79 | for cfg in build_configs), check_cancelled): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 80 | sys.exit(1) |
| 81 | |
| 82 | # run all the tests |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame^] | 83 | jobset.run(( |
| 84 | config.run_command(x) |
| 85 | for config in run_configs |
| 86 | for filt in filters |
| 87 | for x in itertools.chain.from_iterable(itertools.repeat( |
| 88 | glob.glob('bins/%s/%s_test' % ( |
| 89 | config.build_config, filt)), |
| 90 | runs_per_test))), check_cancelled) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if forever: |
| 94 | while True: |
| 95 | dw = watch_dirs.DirWatcher(['src', 'include', 'test']) |
| 96 | initial_time = dw.most_recent_change() |
| 97 | have_files_changed = lambda: dw.most_recent_change() != initial_time |
| 98 | _build_and_run(have_files_changed) |
| 99 | while not have_files_changed(): |
| 100 | time.sleep(1) |
| 101 | else: |
| 102 | _build_and_run(lambda: False) |
| 103 | |