blob: 8d54f884876bf3107b1d5ebd1c025729c829a11b [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
Craig Tiller71735182015-01-15 17:07:13 -08007import simplejson
Nicolas Nobleddef2462015-01-06 18:08:25 -08008import multiprocessing
9import sys
ctiller3040cb72015-01-07 12:13:17 -080010import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080011
12import jobset
ctiller3040cb72015-01-07 12:13:17 -080013import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080014
Craig Tiller738c3342015-01-12 14:28:33 -080015# SimpleConfig: just compile with CONFIG=config, and run the binary to test
16class SimpleConfig(object):
17 def __init__(self, config):
18 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080019 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080020
21 def run_command(self, binary):
22 return [binary]
23
24
25# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
26class ValgrindConfig(object):
Craig Tiller2aa4d642015-01-14 15:59:44 -080027 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080028 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080029 self.tool = tool
30 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080031
32 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080033 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080034
35
36# different configurations we can run under
37_CONFIGS = {
38 'dbg': SimpleConfig('dbg'),
39 'opt': SimpleConfig('opt'),
40 'tsan': SimpleConfig('tsan'),
41 'msan': SimpleConfig('msan'),
42 'asan': SimpleConfig('asan'),
Craig Tiller934baa32015-01-12 18:19:45 -080043 'gcov': SimpleConfig('gcov'),
Craig Tillerec0b8f32015-01-15 07:30:00 -080044 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
Craig Tiller2aa4d642015-01-14 15:59:44 -080045 'helgrind': ValgrindConfig('dbg', 'helgrind')
Craig Tiller738c3342015-01-12 14:28:33 -080046 }
47
48
Craig Tillerb29797b2015-01-12 13:51:54 -080049_DEFAULT = ['dbg', 'opt']
Craig Tiller686fb262015-01-15 07:39:09 -080050_LANGUAGE_TEST_TARGETS = {
51 'c++': 'buildtests_cxx',
52 'c': 'buildtests_c',
53}
Nicolas Nobleddef2462015-01-06 18:08:25 -080054
55# parse command line
56argp = argparse.ArgumentParser(description='Run grpc tests.')
57argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080058 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080059 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080060 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080061argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
62argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080063argp.add_argument('-f', '--forever',
64 default=False,
65 action='store_const',
66 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080067argp.add_argument('--newline_on_success',
68 default=False,
69 action='store_const',
70 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -080071argp.add_argument('-l', '--language',
72 choices=sorted(_LANGUAGE_TEST_TARGETS.keys()),
73 nargs='+',
74 default=sorted(_LANGUAGE_TEST_TARGETS.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -080075args = argp.parse_args()
76
77# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080078run_configs = set(_CONFIGS[cfg]
79 for cfg in itertools.chain.from_iterable(
80 _CONFIGS.iterkeys() if x == 'all' else [x]
81 for x in args.config))
82build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tiller686fb262015-01-15 07:39:09 -080083make_targets = set(_LANGUAGE_TEST_TARGETS[x] for x in args.language)
Nicolas Nobleddef2462015-01-06 18:08:25 -080084filters = args.test_filter
85runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -080086forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -080087
Nicolas Nobleddef2462015-01-06 18:08:25 -080088
Craig Tiller71735182015-01-15 17:07:13 -080089class TestCache(object):
90 def __init__(self):
91 self._last_successful_run = {}
92
93 def should_run(self, cmdline, bin_hash):
94 cmdline = ' '.join(cmdline)
95 if cmdline not in self._last_successful_run:
96 return True
97 if self._last_successful_run[cmdline] != bin_hash:
98 return True
99 return False
100
101 def finished(self, cmdline, bin_hash):
102 self._last_successful_run[' '.join(cmdline)] = bin_hash
103
104 def dump(self):
105 return [{'cmdline': k, 'hash': v} for k, v in self._last_successful_run.iteritems()]
106
107 def parse(self, exdump):
108 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
109
110 def save(self):
111 with open('.run_tests_cache', 'w') as f:
112 f.write(simplejson.dumps(self.dump()))
113
114 def load(self):
115 with open('.run_tests_cache') as f:
116 self.parse(simplejson.loads(f.read()))
117
118
119def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800120 """Do one pass of building & running tests."""
121 # build latest, sharing cpu between the various makes
122 if not jobset.run(
123 (['make',
Craig Tiller738c3342015-01-12 14:28:33 -0800124 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller686fb262015-01-15 07:39:09 -0800125 'CONFIG=%s' % cfg] + list(make_targets)
Craig Tillerbe56a962015-01-14 11:33:03 -0800126 for cfg in build_configs),
Craig Tiller7c1d7f72015-01-12 17:08:33 -0800127 check_cancelled, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -0800128 return 1
ctiller3040cb72015-01-07 12:13:17 -0800129
130 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -0800131 if not jobset.run(
132 itertools.ifilter(
133 lambda x: x is not None, (
134 config.run_command(x)
135 for config in run_configs
136 for filt in filters
137 for x in itertools.chain.from_iterable(itertools.repeat(
138 glob.glob('bins/%s/%s_test' % (
139 config.build_config, filt)),
140 runs_per_test)))),
141 check_cancelled,
Nicolas Noble594ef6c2015-01-14 18:02:04 -0800142 newline_on_success=newline_on_success,
Craig Tiller71735182015-01-15 17:07:13 -0800143 maxjobs=min(c.maxjobs for c in run_configs),
144 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800145 return 2
146
147 return 0
ctiller3040cb72015-01-07 12:13:17 -0800148
149
Craig Tiller71735182015-01-15 17:07:13 -0800150test_cache = (None if runs_per_test != 1
151 or 'gcov' in build_configs
152 or 'valgrind' in build_configs
153 else TestCache())
154if test_cache:
155 test_cache.load()
156
ctiller3040cb72015-01-07 12:13:17 -0800157if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800158 success = True
ctiller3040cb72015-01-07 12:13:17 -0800159 while True:
160 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
161 initial_time = dw.most_recent_change()
162 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800163 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800164 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800165 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800166 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800167 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800168 jobset.message('SUCCESS',
169 'All tests are now passing properly',
170 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800171 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800172 while not have_files_changed():
173 time.sleep(1)
174else:
Craig Tiller71735182015-01-15 17:07:13 -0800175 result = _build_and_run(check_cancelled=lambda: False,
176 newline_on_success=args.newline_on_success,
177 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800178 if result == 0:
179 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
180 else:
181 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tiller71735182015-01-15 17:07:13 -0800182 test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800183 sys.exit(result)