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