Skip to content

Commit fa494de

Browse files
author
MarcoFalke
committed
[qa] pull-tester: Run rpc test in parallel
1 parent 4e14afe commit fa494de

File tree

3 files changed

+105
-13
lines changed

3 files changed

+105
-13
lines changed

qa/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Run all possible tests with
3535

3636
qa/pull-tester/rpc-tests.py -extended
3737

38+
By default, tests will be run in parallel if you want to specify how many
39+
tests should be run in parallel, append `-paralell=n` (default n=4).
40+
3841
If you want to create a basic coverage report for the rpc test suite, append `--coverage`.
3942

4043
Possible options, which apply to each individual test run:

qa/pull-tester/rpc-tests.py

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@
5353

5454
#Create a set to store arguments and create the passon string
5555
opts = set()
56-
passon_args = ""
56+
passon_args = []
5757
PASSON_REGEX = re.compile("^--")
58+
PARALLEL_REGEX = re.compile('^-parallel=')
5859

5960
print_help = False
61+
run_parallel = 4
6062

6163
for arg in sys.argv[1:]:
6264
if arg == "--help" or arg == "-h" or arg == "-?":
@@ -65,7 +67,9 @@
6567
if arg == '--coverage':
6668
ENABLE_COVERAGE = 1
6769
elif PASSON_REGEX.match(arg):
68-
passon_args += " " + arg
70+
passon_args.append(arg)
71+
elif PARALLEL_REGEX.match(arg):
72+
run_parallel = int(arg.split(sep='=', maxsplit=1)[1])
6973
else:
7074
opts.add(arg)
7175

@@ -96,6 +100,7 @@
96100

97101
#Tests
98102
testScripts = [
103+
'walletbackup.py',
99104
'bip68-112-113-p2p.py',
100105
'wallet.py',
101106
'listtransactions.py',
@@ -116,7 +121,6 @@
116121
'merkle_blocks.py',
117122
'fundrawtransaction.py',
118123
'signrawtransactions.py',
119-
'walletbackup.py',
120124
'nodehandling.py',
121125
'reindex.py',
122126
'decodescript.py',
@@ -131,7 +135,7 @@
131135
'abandonconflict.py',
132136
'p2p-versionbits-warning.py',
133137
'importprunedfunds.py',
134-
'signmessages.py'
138+
'signmessages.py',
135139
]
136140
if ENABLE_ZMQ:
137141
testScripts.append('zmq_test.py')
@@ -160,6 +164,7 @@
160164
'pruning.py', # leave pruning last as it takes a REALLY long time
161165
]
162166

167+
163168
def runtests():
164169
test_list = []
165170
if '-extended' in opts:
@@ -173,30 +178,91 @@ def runtests():
173178

174179
if print_help:
175180
# Only print help of the first script and exit
176-
subprocess.check_call(RPC_TESTS_DIR + test_list[0] + ' -h', shell=True)
181+
subprocess.check_call((RPC_TESTS_DIR + test_list[0]).split() + ['-h'])
177182
sys.exit(0)
178183

179184
coverage = None
180185

181186
if ENABLE_COVERAGE:
182187
coverage = RPCCoverage()
183188
print("Initializing coverage directory at %s\n" % coverage.dir)
184-
flags = " --srcdir %s/src %s %s" % (BUILDDIR, coverage.flag if coverage else '', passon_args)
189+
flags = ["--srcdir=%s/src" % BUILDDIR] + passon_args
190+
if coverage:
191+
flags.append(coverage.flag)
192+
193+
if len(test_list) > 1:
194+
# Populate cache
195+
subprocess.check_output([RPC_TESTS_DIR + 'create_cache.py'] + flags)
185196

186197
#Run Tests
187-
for t in test_list:
188-
print("Running testscript %s%s%s ..." % (BOLD[1], t, BOLD[0]))
189-
time0 = time.time()
190-
subprocess.check_call(
191-
RPC_TESTS_DIR + t + flags, shell=True)
192-
print("Duration: %s s\n" % (int(time.time() - time0)))
198+
max_len_name = len(max(test_list, key=len))
199+
time_sum = 0
200+
time0 = time.time()
201+
job_queue = RPCTestHandler(run_parallel, test_list, flags)
202+
results = BOLD[1] + "%s | %s | %s\n\n" % ("TEST".ljust(max_len_name), "PASSED", "DURATION") + BOLD[0]
203+
all_passed = True
204+
for _ in range(len(test_list)):
205+
(name, stdout, stderr, passed, duration) = job_queue.get_next()
206+
all_passed = all_passed and passed
207+
time_sum += duration
208+
209+
print('\n' + BOLD[1] + name + BOLD[0] + ":")
210+
print(stdout)
211+
print('stderr:\n' if not stderr == '' else '', stderr)
212+
results += "%s | %s | %s s\n" % (name.ljust(max_len_name), str(passed).ljust(6), duration)
213+
print("Pass: %s%s%s, Duration: %s s\n" % (BOLD[1], passed, BOLD[0], duration))
214+
results += BOLD[1] + "\n%s | %s | %s s (accumulated)" % ("ALL".ljust(max_len_name), str(all_passed).ljust(6), time_sum) + BOLD[0]
215+
print(results)
216+
print("\nRuntime: %s s" % (int(time.time() - time0)))
193217

194218
if coverage:
195219
coverage.report_rpc_coverage()
196220

197221
print("Cleaning up coverage data")
198222
coverage.cleanup()
199223

224+
sys.exit(not all_passed)
225+
226+
227+
class RPCTestHandler:
228+
"""
229+
Trigger the testscrips passed in via the list.
230+
"""
231+
232+
def __init__(self, num_tests_parallel, test_list=None, flags=None):
233+
assert(num_tests_parallel >= 1)
234+
self.num_jobs = num_tests_parallel
235+
self.test_list = test_list
236+
self.flags = flags
237+
self.num_running = 0
238+
self.jobs = []
239+
240+
def get_next(self):
241+
while self.num_running < self.num_jobs and self.test_list:
242+
# Add tests
243+
self.num_running += 1
244+
t = self.test_list.pop(0)
245+
self.jobs.append((t,
246+
time.time(),
247+
subprocess.Popen((RPC_TESTS_DIR + t).split() + self.flags,
248+
universal_newlines=True,
249+
stdout=subprocess.PIPE,
250+
stderr=subprocess.PIPE)))
251+
if not self.jobs:
252+
raise IndexError('%s from empty list' % __name__)
253+
while True:
254+
# Return first proc that finishes
255+
time.sleep(.5)
256+
for j in self.jobs:
257+
(name, time0, proc) = j
258+
if proc.poll() is not None:
259+
(stdout, stderr) = proc.communicate(timeout=3)
260+
passed = stderr == "" and proc.returncode == 0
261+
self.num_running -= 1
262+
self.jobs.remove(j)
263+
return name, stdout, stderr, passed, int(time.time() - time0)
264+
print('.', end='', flush=True)
265+
200266

201267
class RPCCoverage(object):
202268
"""
@@ -215,7 +281,7 @@ class RPCCoverage(object):
215281
"""
216282
def __init__(self):
217283
self.dir = tempfile.mkdtemp(prefix="coverage")
218-
self.flag = '--coveragedir %s' % self.dir
284+
self.flag = '--coveragedir=%s' % self.dir
219285

220286
def report_rpc_coverage(self):
221287
"""

qa/rpc-tests/create_cache.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2016 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# Helper script to create the cache
8+
# (see BitcoinTestFramework.setup_chain)
9+
#
10+
11+
from test_framework.test_framework import BitcoinTestFramework
12+
13+
class CreateCache(BitcoinTestFramework):
14+
15+
def setup_network(self):
16+
# Don't setup any test nodes
17+
self.options.noshutdown = True
18+
19+
def run_test(self):
20+
pass
21+
22+
if __name__ == '__main__':
23+
CreateCache().main()

0 commit comments

Comments
 (0)