onqtam | ad89a13 | 2016-05-21 00:25:35 +0300 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | import math |
| 5 | import multiprocessing |
| 6 | import subprocess |
| 7 | |
| 8 | if len(sys.argv) != 2: |
| 9 | print("supply the path to the doctest executable as the first argument!") |
| 10 | sys.exit(0) |
| 11 | |
| 12 | # get the number of tests in the doctest executable |
| 13 | result = subprocess.Popen([sys.argv[1], "-count"], stdout = subprocess.PIPE).communicate()[0] |
| 14 | num_tests = int(result.rsplit(' ', 1)[-1]) |
| 15 | |
| 16 | # calculate the ranges |
| 17 | cores = multiprocessing.cpu_count() |
| 18 | l = range(num_tests + 1) |
| 19 | n = int(math.ceil(float(len( l )) / cores)) |
| 20 | data = [l[i : i + n] for i in range(1, len( l ), n)] |
| 21 | data = tuple([[x[0], x[-1]] for x in data]) |
| 22 | |
| 23 | # for 8 cores and 100 tests the ranges will look like this |
| 24 | # ([1, 13], [14, 26], [27, 39], [40, 52], [53, 65], [66, 78], [79, 91], [92, 100]) |
| 25 | |
| 26 | # the worker callback that runs the executable for the given range of tests |
| 27 | def worker((first, last)): |
| 28 | subprocess.Popen([sys.argv[1], "-first=%s -last=%s" % (first, last)]) |
| 29 | |
| 30 | # run the tasks on a pool |
| 31 | if __name__ == '__main__': |
| 32 | p = multiprocessing.Pool(cores) |
| 33 | p.map(worker, data) |