From 709370b2198e09f1dbe195fe8813602a3125b7f6 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 8 Sep 2021 23:29:05 -0700 Subject: Add doctests to some of this stuff. --- tests/parallelize_test.py | 85 +++++++++++++---------------------------------- tests/run_all_tests.sh | 5 +++ 2 files changed, 28 insertions(+), 62 deletions(-) (limited to 'tests') diff --git a/tests/parallelize_test.py b/tests/parallelize_test.py index d87b5e7..9d98710 100755 --- a/tests/parallelize_test.py +++ b/tests/parallelize_test.py @@ -37,20 +37,30 @@ def list_primes(n): @decorator_utils.timed -def driver() -> None: +def test_thread_parallelization() -> None: results = [] - for _ in range(20): - results.append(compute_factorial_process(_)) - for future in smart_future.wait_any(results): - print(f'Process: {future}') - - results = [] - for _ in range(20): + for _ in range(50): results.append(compute_factorial_thread(_)) smart_future.wait_all(results) for future in results: print(f'Thread: {future}') + texecutor = executors.DefaultExecutors().thread_pool() + texecutor.shutdown() + + +@decorator_utils.timed +def test_process_parallelization() -> None: + results = [] + for _ in range(50): + results.append(compute_factorial_process(_)) + for future in smart_future.wait_any(results): + print(f'Process: {future}') + pexecutor = executors.DefaultExecutors().process_pool() + pexecutor.shutdown() + +@decorator_utils.timed +def test_remote_parallelization() -> None: results = {} for _ in range(50): n = random.randint(0, 100000) @@ -59,66 +69,17 @@ def driver() -> None: for _ in results[n]: tot += _ print(tot) + rexecutor = executors.DefaultExecutors().remote_pool() + rexecutor.shutdown() @bootstrap.initialize def main() -> None: - print(driver()) - pexecutor = executors.DefaultExecutors().process_pool() - pexecutor.shutdown() - texecutor = executors.DefaultExecutors().thread_pool() - texecutor.shutdown() - rexecutor = executors.DefaultExecutors().remote_pool() - rexecutor.shutdown() + test_thread_parallelization() + test_process_parallelization() + test_remote_parallelization() sys.exit(0) if __name__ == '__main__': main() - -# print """Usage: python sum_primes.py [ncpus] -# [ncpus] - the number of workers to run in parallel, -# if omitted it will be set to the number of processors in the system -# """ - -# # tuple of all parallel python servers to connect with -# ppservers = () -# #ppservers = ("10.0.0.1",) - -# if len(sys.argv) > 1: -# ncpus = int(sys.argv[1]) -# # Creates jobserver with ncpus workers -# job_server = pp.Server(ncpus, ppservers=ppservers) -# else: -# # Creates jobserver with automatically detected number of workers -# job_server = pp.Server(ppservers=ppservers) - -# print "Starting pp with", job_server.get_ncpus(), "workers" - -# # Submit a job of calulating sum_primes(100) for execution. -# # sum_primes - the function -# # (100,) - tuple with arguments for sum_primes -# # (isprime,) - tuple with functions on which function sum_primes depends -# # ("math",) - tuple with module names which must be imported before sum_primes execution -# # Execution starts as soon as one of the workers will become available -# job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",)) - -# # Retrieves the result calculated by job1 -# # The value of job1() is the same as sum_primes(100) -# # If the job has not been finished yet, execution will wait here until result is available -# result = job1() - -# print "Sum of primes below 100 is", result - -# start_time = time.time() - -# # The following submits 8 jobs and then retrieves the results -# inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700) -# jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs] -# for input, job in jobs: -# print "Sum of primes below", input, "is", job() - -# print "Time elapsed: ", time.time() - start_time, "s" -# job_server.print_stats() - -# # Parallel Python Software: http://www.parallelpython.com diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh index c2f9f93..25365bb 100755 --- a/tests/run_all_tests.sh +++ b/tests/run_all_tests.sh @@ -1,5 +1,10 @@ #!/bin/bash +for doctest in $(grep -l doctest ../*.py); do + echo "------------------------- ${doctest} -------------------------" + python3 ${doctest} +done + for test in $(ls *_test.py); do if [ "${test}" != "parallelize_test.py" ]; then echo "------------------------- ${test} -------------------------" -- cgit v1.3