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