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