blob: d291abf3bf0e7736b4dc56329cdedf0f6fa40ffc [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 Tillerc7449162015-01-16 14:42:10 -080023 self.allow_hashing = (config != 'gcov')
Craig Tiller738c3342015-01-12 14:28:33 -080024
25 def run_command(self, binary):
26 return [binary]
27
28
29# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
30class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080031
Craig Tiller2aa4d642015-01-14 15:59:44 -080032 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080033 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080034 self.tool = tool
35 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080036 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080037
38 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080039 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080040
41
Craig Tillerc7449162015-01-16 14:42:10 -080042class CLanguage(object):
43
44 def __init__(self, make_target):
45 self.allow_hashing = True
46 self.make_target = make_target
47
48 def test_binaries(self, config):
49 return glob.glob('bins/%s/*_test' % config)
50
51 def make_targets(self):
52 return ['buildtests_%s' % self.make_target]
53
54 def build_steps(self):
55 return []
56
57
58class PhpLanguage(object):
59
60 def __init__(self):
61 self.allow_hashing = False
62
63 def test_binaries(self, config):
64 return ['src/php/bin/run_tests.sh']
65
66 def make_targets(self):
67 return []
68
69 def build_steps(self):
70 return [['tools/run_tests/build_php.sh']]
71
72
Craig Tiller738c3342015-01-12 14:28:33 -080073# different configurations we can run under
74_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -080075 'dbg': SimpleConfig('dbg'),
76 'opt': SimpleConfig('opt'),
77 'tsan': SimpleConfig('tsan'),
78 'msan': SimpleConfig('msan'),
79 'asan': SimpleConfig('asan'),
80 'gcov': SimpleConfig('gcov'),
81 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
82 'helgrind': ValgrindConfig('dbg', 'helgrind')
83 }
Craig Tiller738c3342015-01-12 14:28:33 -080084
85
Craig Tillerb29797b2015-01-12 13:51:54 -080086_DEFAULT = ['dbg', 'opt']
Craig Tillerc7449162015-01-16 14:42:10 -080087_LANGUAGES = {
88 'c++': CLanguage('cxx'),
89 'c': CLanguage('c'),
90 'php': PhpLanguage()
Craig Tiller686fb262015-01-15 07:39:09 -080091}
Nicolas Nobleddef2462015-01-06 18:08:25 -080092
93# parse command line
94argp = argparse.ArgumentParser(description='Run grpc tests.')
95argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080096 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080097 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080098 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080099argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -0800100argp.add_argument('-f', '--forever',
101 default=False,
102 action='store_const',
103 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800104argp.add_argument('--newline_on_success',
105 default=False,
106 action='store_const',
107 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800108argp.add_argument('-l', '--language',
Craig Tillerc7449162015-01-16 14:42:10 -0800109 choices=sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800110 nargs='+',
Craig Tillerc7449162015-01-16 14:42:10 -0800111 default=sorted(_LANGUAGES.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -0800112args = argp.parse_args()
113
114# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800115run_configs = set(_CONFIGS[cfg]
116 for cfg in itertools.chain.from_iterable(
117 _CONFIGS.iterkeys() if x == 'all' else [x]
118 for x in args.config))
119build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800120
Craig Tillerc7449162015-01-16 14:42:10 -0800121make_targets = []
122languages = set(_LANGUAGES[l] for l in args.language)
123build_steps = [['make',
124 '-j', '%d' % (multiprocessing.cpu_count() + 1),
125 'CONFIG=%s' % cfg] + list(set(
126 itertools.chain.from_iterable(l.make_targets()
127 for l in languages)))
128 for cfg in build_configs] + list(
129 itertools.chain.from_iterable(l.build_steps()
130 for l in languages))
Craig Tillerf1973b02015-01-16 12:32:13 -0800131
Nicolas Nobleddef2462015-01-06 18:08:25 -0800132runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800133forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800134
Nicolas Nobleddef2462015-01-06 18:08:25 -0800135
Craig Tiller71735182015-01-15 17:07:13 -0800136class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800137 """Cache for running tests."""
138
Craig Tiller71735182015-01-15 17:07:13 -0800139 def __init__(self):
140 self._last_successful_run = {}
141
142 def should_run(self, cmdline, bin_hash):
143 cmdline = ' '.join(cmdline)
144 if cmdline not in self._last_successful_run:
145 return True
146 if self._last_successful_run[cmdline] != bin_hash:
147 return True
148 return False
149
150 def finished(self, cmdline, bin_hash):
151 self._last_successful_run[' '.join(cmdline)] = bin_hash
152
153 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800154 return [{'cmdline': k, 'hash': v}
155 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800156
157 def parse(self, exdump):
158 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
159
160 def save(self):
161 with open('.run_tests_cache', 'w') as f:
162 f.write(simplejson.dumps(self.dump()))
163
Craig Tiller1cc11db2015-01-15 22:50:50 -0800164 def maybe_load(self):
165 if os.path.exists('.run_tests_cache'):
166 with open('.run_tests_cache') as f:
167 self.parse(simplejson.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800168
169
170def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800171 """Do one pass of building & running tests."""
172 # build latest, sharing cpu between the various makes
Craig Tillerf1973b02015-01-16 12:32:13 -0800173 if not jobset.run(build_steps):
Craig Tillerd86a3942015-01-14 12:48:54 -0800174 return 1
ctiller3040cb72015-01-07 12:13:17 -0800175
176 # run all the tests
Craig Tillerc7449162015-01-16 14:42:10 -0800177 one_run = dict(
178 (' '.join(config.run_command(x)), config.run_command(x))
179 for config in run_configs
180 for language in args.language
181 for x in _LANGUAGES[language].test_binaries(config.build_config)
182 ).values()
183 all_runs = itertools.chain.from_iterable(
184 itertools.repeat(one_run, runs_per_test))
185 if not jobset.run(all_runs, check_cancelled,
186 newline_on_success=newline_on_success,
187 maxjobs=min(c.maxjobs for c in run_configs),
188 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800189 return 2
190
191 return 0
ctiller3040cb72015-01-07 12:13:17 -0800192
193
Craig Tillerc7449162015-01-16 14:42:10 -0800194test_cache = (None
195 if not all(x.allow_hashing
196 for x in itertools.chain(languages, run_configs))
Craig Tillerb50d1662015-01-15 17:28:21 -0800197 else TestCache())
Craig Tiller71735182015-01-15 17:07:13 -0800198if test_cache:
Craig Tiller1cc11db2015-01-15 22:50:50 -0800199 test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800200
ctiller3040cb72015-01-07 12:13:17 -0800201if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800202 success = True
ctiller3040cb72015-01-07 12:13:17 -0800203 while True:
204 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
205 initial_time = dw.most_recent_change()
206 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800207 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800208 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800209 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800210 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800211 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800212 jobset.message('SUCCESS',
213 'All tests are now passing properly',
214 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800215 jobset.message('IDLE', 'No change detected')
Craig Tillerc7449162015-01-16 14:42:10 -0800216 if test_cache: test_cache.save()
ctiller3040cb72015-01-07 12:13:17 -0800217 while not have_files_changed():
218 time.sleep(1)
219else:
Craig Tiller71735182015-01-15 17:07:13 -0800220 result = _build_and_run(check_cancelled=lambda: False,
221 newline_on_success=args.newline_on_success,
222 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800223 if result == 0:
224 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
225 else:
226 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tillerc7449162015-01-16 14:42:10 -0800227 if test_cache: test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800228 sys.exit(result)