-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcelery_producer.py
64 lines (47 loc) · 1.67 KB
/
celery_producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import time
from celigo_pipeline_poc.celery_worker import add, ioBoundTask, cpuBoundTask
from functools import reduce
NUM_JOBS_DEFAULT = 50
def testAdd(x, y):
responses = []
for i in range(NUM_JOBS_DEFAULT):
responses.append(add.delay(x, y))
results = map(lambda r: r.get(), responses)
sum = reduce(lambda x, y: x + y, results)
print(sum)
def testIoBoundTask(num_jobs=NUM_JOBS_DEFAULT):
start = time.time()
responses = []
for _ in range(NUM_JOBS_DEFAULT):
responses.append(ioBoundTask.delay())
results = map(lambda r: r.get(), responses)
reduced_result = reduce(_reduce_result, list(results), {})
# print(json.dumps(reduced_result))
print(f"{num_jobs} Jobs | Time taken: {time.time() - start}")
def testCpuBoundTask(num_jobs=NUM_JOBS_DEFAULT):
start = time.time()
compute_results = []
for _ in range(NUM_JOBS_DEFAULT):
compute_results.append(cpuBoundTask.delay(5))
results = list(map(lambda r: r.get(), compute_results))
reduced_result = reduce(_reduce_result, list(results), {})
# print(json.dumps(reduced_result))
print(f"{num_jobs} Jobs | Time taken: {time.time() - start}")
def _reduce_result(acc, code):
if code in acc:
acc[code] += 1
else:
acc[code] = 1
return acc
def main():
# testAdd(2, 3)
print("________Testing IO bound task________")
# test of n = 1 to negate any caching effects
testIoBoundTask(1)
for num_jobs in [10, 50, 100, 200]:
testIoBoundTask(num_jobs)
print("________Testing CPU bound task________")
for num_jobs in [10, 50, 100, 200]:
testCpuBoundTask(num_jobs)
if __name__ == "__main__":
main()