-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
192 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import asyncio | ||
import random | ||
import time | ||
from concurrent.futures import ProcessPoolExecutor | ||
|
||
from zero import AsyncZeroClient | ||
|
||
async_client = AsyncZeroClient("localhost", 5559) | ||
|
||
|
||
async def task(semaphore, items): | ||
async with semaphore: | ||
try: | ||
res = await async_client.call("sum_async", items) | ||
# print(res) | ||
except Exception as e: | ||
print(e) | ||
|
||
|
||
async def process_tasks(items_chunk): | ||
conc = 8 | ||
semaphore = asyncio.BoundedSemaphore(conc) | ||
tasks = [task(semaphore, items) for items in items_chunk] | ||
await asyncio.gather(*tasks) | ||
await async_client.close() | ||
|
||
|
||
def run_chunk(items_chunk): | ||
asyncio.run(process_tasks(items_chunk)) | ||
|
||
|
||
if __name__ == "__main__": | ||
process_no = 8 | ||
|
||
print("Preparing data...") | ||
|
||
sum_items = [[random.randint(50, 500) for _ in range(10)] for _ in range(100000)] | ||
|
||
# Split sum_items into chunks for each process | ||
chunk_size = len(sum_items) // process_no | ||
items_chunks = [ | ||
sum_items[i : i + chunk_size] for i in range(0, len(sum_items), chunk_size) | ||
] | ||
|
||
print("Running...") | ||
|
||
start = time.time() | ||
|
||
with ProcessPoolExecutor(max_workers=process_no) as executor: | ||
executor.map(run_chunk, items_chunks) | ||
|
||
end = time.time() | ||
time_taken_ms = 1e3 * (end - start) | ||
|
||
print(f"total time taken: {time_taken_ms} ms") | ||
print(f"requests per second: {len(sum_items) / time_taken_ms * 1e3}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import random | ||
import time | ||
from functools import partial | ||
from multiprocessing.pool import Pool | ||
|
||
from zero import ZeroClient | ||
|
||
client = ZeroClient("localhost", 5559) | ||
|
||
|
||
sum_func = partial(client.call, "sum_sync") | ||
|
||
|
||
def get_and_sum(msg): | ||
resp = sum_func(msg) | ||
# print(resp) | ||
|
||
|
||
if __name__ == "__main__": | ||
process_no = 32 | ||
pool = Pool(process_no) | ||
|
||
sum_items = [] | ||
for _ in range(100000): | ||
sum_items.append([random.randint(50, 500) for _ in range(10)]) | ||
|
||
start = time.time() | ||
|
||
res = pool.map_async(get_and_sum, sum_items) | ||
pool.close() | ||
pool.join() | ||
|
||
end = time.time() | ||
time_taken_ms = 1e3 * (end - start) | ||
|
||
print(f"total time taken: {time_taken_ms} ms") | ||
print(f"requests per second: {len(sum_items) / time_taken_ms * 1e3}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import random | ||
import time | ||
from functools import partial | ||
from multiprocessing.pool import Pool | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from zero import ZeroClient | ||
|
||
client = ZeroClient("localhost", 5559) | ||
|
||
sum_func = partial(client.call, "sum_sync") | ||
|
||
|
||
def get_and_sum(msg): | ||
return sum_func(msg) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
def run_task(process_no): | ||
sum_items = [ | ||
[random.randint(50, 500) for _ in range(10)] for _ in range(100000) | ||
] | ||
|
||
start = time.time() | ||
with Pool(process_no) as pool: | ||
pool.map_async(get_and_sum, sum_items) | ||
pool.close() | ||
pool.join() | ||
end = time.time() | ||
|
||
time_taken_ms = 1e3 * (end - start) | ||
requests_per_second = len(sum_items) / time_taken_ms * 1e3 | ||
return requests_per_second | ||
|
||
process_numbers = range(2, 128, 2) # From 4 to 128, stepping by 4 | ||
results = [run_task(pn) for pn in process_numbers] | ||
|
||
plt.plot(process_numbers, results) | ||
plt.xlabel("Number of Processes") | ||
plt.ylabel("Requests per Second") | ||
plt.title("Performance by Number of Processes") | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters