blob: 978a15b63aea665bc8a96f7b2604acd3adbd5ff1 [file] [log] [blame]
Nicolas Nobleddef2462015-01-06 18:08:25 -08001#!/usr/bin/python
2"""Run tests in parallel."""
3
4import argparse
5import glob
6import itertools
7import multiprocessing
Craig Tiller1cc11db2015-01-15 22:50:50 -08008import os
Nicolas Nobleddef2462015-01-06 18:08:25 -08009import sys
ctiller3040cb72015-01-07 12:13:17 -080010import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080011
12import jobset
Craig Tillerb50d1662015-01-15 17:28:21 -080013import simplejson
ctiller3040cb72015-01-07 12:13:17 -080014import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080015
Craig Tillerb50d1662015-01-15 17:28:21 -080016
Craig Tiller738c3342015-01-12 14:28:33 -080017# SimpleConfig: just compile with CONFIG=config, and run the binary to test
18class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080019
Craig Tiller738c3342015-01-12 14:28:33 -080020 def __init__(self, config):
21 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080022 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080023
24 def run_command(self, binary):
25 return [binary]
26
27
28# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
29class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080030
Craig Tiller2aa4d642015-01-14 15:59:44 -080031 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080032 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080033 self.tool = tool
34 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080035
36 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080037 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080038
39
40# different configurations we can run under
41_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -080042 'dbg': SimpleConfig('dbg'),
43 'opt': SimpleConfig('opt'),
44 'tsan': SimpleConfig('tsan'),
45 'msan': SimpleConfig('msan'),
46 'asan': SimpleConfig('asan'),
47 'gcov': SimpleConfig('gcov'),
48 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
49 'helgrind': ValgrindConfig('dbg', 'helgrind')
50 }
Craig Tiller738c3342015-01-12 14:28:33 -080051
52
Craig Tillerb29797b2015-01-12 13:51:54 -080053_DEFAULT = ['dbg', 'opt']
Craig Tillerf1973b02015-01-16 12:32:13 -080054_LANGUAGE_BUILD_RULE = {
55 'c++': ['make', 'buildtests_cxx'],
56 'c': ['make', 'buildtests_c'],
57 'php': ['tools/run_tests/build_php.sh']
Craig Tiller686fb262015-01-15 07:39:09 -080058}
Nicolas Nobleddef2462015-01-06 18:08:25 -080059
60# parse command line
61argp = argparse.ArgumentParser(description='Run grpc tests.')
62argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080063 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080064 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080065 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080066argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
67argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080068argp.add_argument('-f', '--forever',
69 default=False,
70 action='store_const',
71 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080072argp.add_argument('--newline_on_success',
73 default=False,
74 action='store_const',
75 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -080076argp.add_argument('-l', '--language',
Craig Tillerf1973b02015-01-16 12:32:13 -080077 choices=sorted(_LANGUAGE_BUILD_RULE.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -080078 nargs='+',
Craig Tillerf1973b02015-01-16 12:32:13 -080079 default=sorted(_LANGUAGE_BUILD_RULE.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -080080args = argp.parse_args()
81
82# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080083run_configs = set(_CONFIGS[cfg]
84 for cfg in itertools.chain.from_iterable(
85 _CONFIGS.iterkeys() if x == 'all' else [x]
86 for x in args.config))
87build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -080088
89make_targets = set()
90build_steps = []
91for language in args.language:
92 cmd = _LANGUAGE_BUILD_RULE[language]
93 if cmd[0] == 'make':
94 make_targets.update(cmd[1:])
95 else:
96 build_steps.append(cmd)
97if make_targets:
98 build_steps = [['make',
99 '-j', '%d' % (multiprocessing.cpu_count() + 1),
100 'CONFIG=%s' % cfg] + list(make_targets)
101 for cfg in build_configs] + build_steps
102
Nicolas Nobleddef2462015-01-06 18:08:25 -0800103filters = args.test_filter
104runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800105forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800106
Nicolas Nobleddef2462015-01-06 18:08:25 -0800107
Craig Tiller71735182015-01-15 17:07:13 -0800108class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800109 """Cache for running tests."""
110
Craig Tiller71735182015-01-15 17:07:13 -0800111 def __init__(self):
112 self._last_successful_run = {}
113
114 def should_run(self, cmdline, bin_hash):
115 cmdline = ' '.join(cmdline)
116 if cmdline not in self._last_successful_run:
117 return True
118 if self._last_successful_run[cmdline] != bin_hash:
119 return True
120 return False
121
122 def finished(self, cmdline, bin_hash):
123 self._last_successful_run[' '.join(cmdline)] = bin_hash
124
125 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800126 return [{'cmdline': k, 'hash': v}
127 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800128
129 def parse(self, exdump):
130 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
131
132 def save(self):
133 with open('.run_tests_cache', 'w') as f:
134 f.write(simplejson.dumps(self.dump()))
135
Craig Tiller1cc11db2015-01-15 22:50:50 -0800136 def maybe_load(self):
137 if os.path.exists('.run_tests_cache'):
138 with open('.run_tests_cache') as f:
139 self.parse(simplejson.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800140
141
142def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800143 """Do one pass of building & running tests."""
144 # build latest, sharing cpu between the various makes
Craig Tillerf1973b02015-01-16 12:32:13 -0800145 if not jobset.run(build_steps):
Craig Tillerd86a3942015-01-14 12:48:54 -0800146 return 1
ctiller3040cb72015-01-07 12:13:17 -0800147
148 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -0800149 if not jobset.run(
150 itertools.ifilter(
151 lambda x: x is not None, (
152 config.run_command(x)
153 for config in run_configs
154 for filt in filters
155 for x in itertools.chain.from_iterable(itertools.repeat(
156 glob.glob('bins/%s/%s_test' % (
157 config.build_config, filt)),
158 runs_per_test)))),
Craig Tillerb50d1662015-01-15 17:28:21 -0800159 check_cancelled,
160 newline_on_success=newline_on_success,
161 maxjobs=min(c.maxjobs for c in run_configs),
162 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800163 return 2
164
165 return 0
ctiller3040cb72015-01-07 12:13:17 -0800166
167
Craig Tiller71735182015-01-15 17:07:13 -0800168test_cache = (None if runs_per_test != 1
Craig Tillerb50d1662015-01-15 17:28:21 -0800169 or 'gcov' in build_configs
170 or 'valgrind' in build_configs
171 else TestCache())
Craig Tiller71735182015-01-15 17:07:13 -0800172if test_cache:
Craig Tiller1cc11db2015-01-15 22:50:50 -0800173 test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800174
ctiller3040cb72015-01-07 12:13:17 -0800175if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800176 success = True
ctiller3040cb72015-01-07 12:13:17 -0800177 while True:
178 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
179 initial_time = dw.most_recent_change()
180 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800181 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800182 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800183 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800184 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800185 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800186 jobset.message('SUCCESS',
187 'All tests are now passing properly',
188 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800189 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800190 while not have_files_changed():
191 time.sleep(1)
192else:
Craig Tiller71735182015-01-15 17:07:13 -0800193 result = _build_and_run(check_cancelled=lambda: False,
194 newline_on_success=args.newline_on_success,
195 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800196 if result == 0:
197 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
198 else:
199 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tiller71735182015-01-15 17:07:13 -0800200 test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800201 sys.exit(result)