summaryrefslogtreecommitdiff
path: root/tests/parallelize_itest.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-09-09 16:42:13 -0700
committerScott Gasch <[email protected]>2021-09-09 16:42:13 -0700
commit6f132c0342ab7aa438ed88d7c5f987cb52d8ca05 (patch)
tree80cf90e7d8ae2ea9d6d191bb78650030fef8cb2e /tests/parallelize_itest.py
parent709370b2198e09f1dbe195fe8813602a3125b7f6 (diff)
Update tests / test harness.
Diffstat (limited to 'tests/parallelize_itest.py')
-rwxr-xr-xtests/parallelize_itest.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/parallelize_itest.py b/tests/parallelize_itest.py
new file mode 100755
index 0000000..9d98710
--- /dev/null
+++ b/tests/parallelize_itest.py
@@ -0,0 +1,85 @@
+#!/usr/bin/env python3
+
+import random
+import sys
+
+import bootstrap
+import parallelize as p
+import decorator_utils
+import executors
+import math_utils
+import smart_future
+
+
[email protected](method=p.Method.THREAD)
+def compute_factorial_thread(n):
+ total = 1
+ for x in range(2, n):
+ total *= x
+ return total
+
+
[email protected](method=p.Method.PROCESS)
+def compute_factorial_process(n):
+ total = 1
+ for x in range(2, n):
+ total *= x
+ return total
+
+
[email protected](method=p.Method.REMOTE)
+def list_primes(n):
+ """Calculates sum of all primes below given integer n"""
+ ret = []
+ for x in range(2, n):
+ ret.append(math_utils.is_prime(x))
+ return ret
+
+
+@decorator_utils.timed
+def test_thread_parallelization() -> None:
+ results = []
+ 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)
+ results[n] = list_primes(n)
+ tot = 0
+ for _ in results[n]:
+ tot += _
+ print(tot)
+ rexecutor = executors.DefaultExecutors().remote_pool()
+ rexecutor.shutdown()
+
+
+def main() -> None:
+ test_thread_parallelization()
+ test_process_parallelization()
+ test_remote_parallelization()
+ sys.exit(0)
+
+
+if __name__ == '__main__':
+ main()