blob: e8c121456a2437cba3998a460663e23f3458a158 [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 Tiller686fb262015-01-15 07:39:09 -080054_LANGUAGE_TEST_TARGETS = {
55 'c++': 'buildtests_cxx',
56 'c': 'buildtests_c',
57}
Nicolas Nobleddef2462015-01-06 18:08:25 -080058
59# parse command line
60argp = argparse.ArgumentParser(description='Run grpc tests.')
61argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080062 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080063 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080064 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080065argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
66argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080067argp.add_argument('-f', '--forever',
68 default=False,
69 action='store_const',
70 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080071argp.add_argument('--newline_on_success',
72 default=False,
73 action='store_const',
74 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -080075argp.add_argument('-l', '--language',
76 choices=sorted(_LANGUAGE_TEST_TARGETS.keys()),
77 nargs='+',
78 default=sorted(_LANGUAGE_TEST_TARGETS.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -080079args = argp.parse_args()
80
81# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080082run_configs = set(_CONFIGS[cfg]
83 for cfg in itertools.chain.from_iterable(
84 _CONFIGS.iterkeys() if x == 'all' else [x]
85 for x in args.config))
86build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tiller686fb262015-01-15 07:39:09 -080087make_targets = set(_LANGUAGE_TEST_TARGETS[x] for x in args.language)
Nicolas Nobleddef2462015-01-06 18:08:25 -080088filters = args.test_filter
89runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -080090forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -080091
Nicolas Nobleddef2462015-01-06 18:08:25 -080092
Craig Tiller71735182015-01-15 17:07:13 -080093class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080094 """Cache for running tests."""
95
Craig Tiller71735182015-01-15 17:07:13 -080096 def __init__(self):
97 self._last_successful_run = {}
98
99 def should_run(self, cmdline, bin_hash):
100 cmdline = ' '.join(cmdline)
101 if cmdline not in self._last_successful_run:
102 return True
103 if self._last_successful_run[cmdline] != bin_hash:
104 return True
105 return False
106
107 def finished(self, cmdline, bin_hash):
108 self._last_successful_run[' '.join(cmdline)] = bin_hash
109
110 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800111 return [{'cmdline': k, 'hash': v}
112 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800113
114 def parse(self, exdump):
115 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
116
117 def save(self):
118 with open('.run_tests_cache', 'w') as f:
119 f.write(simplejson.dumps(self.dump()))
120
Craig Tiller1cc11db2015-01-15 22:50:50 -0800121 def maybe_load(self):
122 if os.path.exists('.run_tests_cache'):
123 with open('.run_tests_cache') as f:
124 self.parse(simplejson.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800125
126
127def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800128 """Do one pass of building & running tests."""
129 # build latest, sharing cpu between the various makes
130 if not jobset.run(
131 (['make',
Craig Tiller738c3342015-01-12 14:28:33 -0800132 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller686fb262015-01-15 07:39:09 -0800133 'CONFIG=%s' % cfg] + list(make_targets)
Craig Tillerbe56a962015-01-14 11:33:03 -0800134 for cfg in build_configs),
Craig Tiller7c1d7f72015-01-12 17:08:33 -0800135 check_cancelled, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -0800136 return 1
ctiller3040cb72015-01-07 12:13:17 -0800137
138 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -0800139 if not jobset.run(
140 itertools.ifilter(
141 lambda x: x is not None, (
142 config.run_command(x)
143 for config in run_configs
144 for filt in filters
145 for x in itertools.chain.from_iterable(itertools.repeat(
146 glob.glob('bins/%s/%s_test' % (
147 config.build_config, filt)),
148 runs_per_test)))),
Craig Tillerb50d1662015-01-15 17:28:21 -0800149 check_cancelled,
150 newline_on_success=newline_on_success,
151 maxjobs=min(c.maxjobs for c in run_configs),
152 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800153 return 2
154
155 return 0
ctiller3040cb72015-01-07 12:13:17 -0800156
157
Craig Tiller71735182015-01-15 17:07:13 -0800158test_cache = (None if runs_per_test != 1
Craig Tillerb50d1662015-01-15 17:28:21 -0800159 or 'gcov' in build_configs
160 or 'valgrind' in build_configs
161 else TestCache())
Craig Tiller71735182015-01-15 17:07:13 -0800162if test_cache:
Craig Tiller1cc11db2015-01-15 22:50:50 -0800163 test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800164
ctiller3040cb72015-01-07 12:13:17 -0800165if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800166 success = True
ctiller3040cb72015-01-07 12:13:17 -0800167 while True:
168 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
169 initial_time = dw.most_recent_change()
170 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800171 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800172 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800173 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800174 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800175 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800176 jobset.message('SUCCESS',
177 'All tests are now passing properly',
178 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800179 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800180 while not have_files_changed():
181 time.sleep(1)
182else:
Craig Tiller71735182015-01-15 17:07:13 -0800183 result = _build_and_run(check_cancelled=lambda: False,
184 newline_on_success=args.newline_on_success,
185 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800186 if result == 0:
187 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
188 else:
189 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tiller71735182015-01-15 17:07:13 -0800190 test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800191 sys.exit(result)