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