blob: 8c4c998d7be924b66ece72578e08f238ccfa87f4 [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
8import sys
ctiller3040cb72015-01-07 12:13:17 -08009import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080010
11import jobset
Craig Tillerb50d1662015-01-15 17:28:21 -080012import simplejson
ctiller3040cb72015-01-07 12:13:17 -080013import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080014
Craig Tillerb50d1662015-01-15 17:28:21 -080015
Craig Tiller738c3342015-01-12 14:28:33 -080016# SimpleConfig: just compile with CONFIG=config, and run the binary to test
17class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080018
Craig Tiller738c3342015-01-12 14:28:33 -080019 def __init__(self, config):
20 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080021 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080022
23 def run_command(self, binary):
24 return [binary]
25
26
27# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
28class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080029
Craig Tiller2aa4d642015-01-14 15:59:44 -080030 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080031 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080032 self.tool = tool
33 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080034
35 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080036 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080037
38
39# different configurations we can run under
40_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -080041 'dbg': SimpleConfig('dbg'),
42 'opt': SimpleConfig('opt'),
43 'tsan': SimpleConfig('tsan'),
44 'msan': SimpleConfig('msan'),
45 'asan': SimpleConfig('asan'),
46 'gcov': SimpleConfig('gcov'),
47 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
48 'helgrind': ValgrindConfig('dbg', 'helgrind')
49 }
Craig Tiller738c3342015-01-12 14:28:33 -080050
51
Craig Tillerb29797b2015-01-12 13:51:54 -080052_DEFAULT = ['dbg', 'opt']
Craig Tiller686fb262015-01-15 07:39:09 -080053_LANGUAGE_TEST_TARGETS = {
54 'c++': 'buildtests_cxx',
55 'c': 'buildtests_c',
56}
Nicolas Nobleddef2462015-01-06 18:08:25 -080057
58# parse command line
59argp = argparse.ArgumentParser(description='Run grpc tests.')
60argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080061 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080062 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080063 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080064argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
65argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080066argp.add_argument('-f', '--forever',
67 default=False,
68 action='store_const',
69 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080070argp.add_argument('--newline_on_success',
71 default=False,
72 action='store_const',
73 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -080074argp.add_argument('-l', '--language',
75 choices=sorted(_LANGUAGE_TEST_TARGETS.keys()),
76 nargs='+',
77 default=sorted(_LANGUAGE_TEST_TARGETS.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -080078args = argp.parse_args()
79
80# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080081run_configs = set(_CONFIGS[cfg]
82 for cfg in itertools.chain.from_iterable(
83 _CONFIGS.iterkeys() if x == 'all' else [x]
84 for x in args.config))
85build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tiller686fb262015-01-15 07:39:09 -080086make_targets = set(_LANGUAGE_TEST_TARGETS[x] for x in args.language)
Nicolas Nobleddef2462015-01-06 18:08:25 -080087filters = args.test_filter
88runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -080089forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -080090
Nicolas Nobleddef2462015-01-06 18:08:25 -080091
Craig Tiller71735182015-01-15 17:07:13 -080092class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080093 """Cache for running tests."""
94
Craig Tiller71735182015-01-15 17:07:13 -080095 def __init__(self):
96 self._last_successful_run = {}
97
98 def should_run(self, cmdline, bin_hash):
99 cmdline = ' '.join(cmdline)
100 if cmdline not in self._last_successful_run:
101 return True
102 if self._last_successful_run[cmdline] != bin_hash:
103 return True
104 return False
105
106 def finished(self, cmdline, bin_hash):
107 self._last_successful_run[' '.join(cmdline)] = bin_hash
108
109 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800110 return [{'cmdline': k, 'hash': v}
111 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800112
113 def parse(self, exdump):
114 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
115
116 def save(self):
117 with open('.run_tests_cache', 'w') as f:
118 f.write(simplejson.dumps(self.dump()))
119
120 def load(self):
121 with open('.run_tests_cache') as f:
122 self.parse(simplejson.loads(f.read()))
123
124
125def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800126 """Do one pass of building & running tests."""
127 # build latest, sharing cpu between the various makes
128 if not jobset.run(
129 (['make',
Craig Tiller738c3342015-01-12 14:28:33 -0800130 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller686fb262015-01-15 07:39:09 -0800131 'CONFIG=%s' % cfg] + list(make_targets)
Craig Tillerbe56a962015-01-14 11:33:03 -0800132 for cfg in build_configs),
Craig Tiller7c1d7f72015-01-12 17:08:33 -0800133 check_cancelled, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -0800134 return 1
ctiller3040cb72015-01-07 12:13:17 -0800135
136 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -0800137 if not jobset.run(
138 itertools.ifilter(
139 lambda x: x is not None, (
140 config.run_command(x)
141 for config in run_configs
142 for filt in filters
143 for x in itertools.chain.from_iterable(itertools.repeat(
144 glob.glob('bins/%s/%s_test' % (
145 config.build_config, filt)),
146 runs_per_test)))),
Craig Tillerb50d1662015-01-15 17:28:21 -0800147 check_cancelled,
148 newline_on_success=newline_on_success,
149 maxjobs=min(c.maxjobs for c in run_configs),
150 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800151 return 2
152
153 return 0
ctiller3040cb72015-01-07 12:13:17 -0800154
155
Craig Tiller71735182015-01-15 17:07:13 -0800156test_cache = (None if runs_per_test != 1
Craig Tillerb50d1662015-01-15 17:28:21 -0800157 or 'gcov' in build_configs
158 or 'valgrind' in build_configs
159 else TestCache())
Craig Tiller71735182015-01-15 17:07:13 -0800160if test_cache:
161 test_cache.load()
162
ctiller3040cb72015-01-07 12:13:17 -0800163if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800164 success = True
ctiller3040cb72015-01-07 12:13:17 -0800165 while True:
166 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
167 initial_time = dw.most_recent_change()
168 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800169 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800170 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800171 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800172 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800173 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800174 jobset.message('SUCCESS',
175 'All tests are now passing properly',
176 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800177 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800178 while not have_files_changed():
179 time.sleep(1)
180else:
Craig Tiller71735182015-01-15 17:07:13 -0800181 result = _build_and_run(check_cancelled=lambda: False,
182 newline_on_success=args.newline_on_success,
183 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800184 if result == 0:
185 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
186 else:
187 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tiller71735182015-01-15 17:07:13 -0800188 test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800189 sys.exit(result)