Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import aiohttp
- import asyncio
- import time
- import ssl
- import certifi
- import matplotlib.pyplot as plt
- import numpy as np
- API_URL='https://api.github.com/users/DimitriChrysafis/repos'
- def EXECUTE_SEQUENTIAL_REQUESTS(api_url:str, request_count:int)->float:
- start=time.time()
- for _ in range(request_count):
- requests.get(api_url).json()
- end=time.time()
- return end-start
- def GENERATE_SSL_CONTEXT()->ssl.SSLContext:
- return ssl.create_default_context(cafile=certifi.where())
- async def PERFORM_FETCH(session:aiohttp.ClientSession, api_url:str)->dict:
- async with session.get(api_url, ssl=GENERATE_SSL_CONTEXT()) as response:
- return await response.json()
- async def PERFORM_ASYNC_REQUESTS(api_url:str, request_count:int, thread_count:int)->float:
- start=time.time()
- async with aiohttp.ClientSession() as session:
- tasks=[PERFORM_FETCH(session, api_url) for _ in range(request_count)]
- await asyncio.gather(*tasks)
- end=time.time()
- return end-start
- def EVALUATE_PERFORMANCE(request_count:int, thread_count:int)->dict:
- sequential_duration=EXECUTE_SEQUENTIAL_REQUESTS(API_URL, request_count)
- async_duration=asyncio.run(PERFORM_ASYNC_REQUESTS(API_URL, request_count, thread_count))
- return {
- 'request_count':request_count,
- 'thread_count':thread_count,
- 'sequential_duration':sequential_duration,
- 'async_duration':async_duration
- }
- def VISUALIZE_RESULTS(results:list):
- unique_request_counts=sorted(set(result['request_count'] for result in results))
- unique_thread_counts=sorted(set(result['thread_count'] for result in results))
- sequential_durations={count:[] for count in unique_request_counts}
- async_durations={count:{threads:[] for threads in unique_thread_counts} for count in unique_request_counts}
- for result in results:
- req_count=result['request_count']
- thrd_count=result['thread_count']
- if thrd_count==1:
- sequential_durations[req_count].append(result['sequential_duration'])
- else:
- async_durations[req_count][thrd_count].append(result['async_duration'])
- plt.figure(figsize=(14,9))
- plt.plot(
- unique_request_counts,
- [np.mean(sequential_durations[count]) for count in unique_request_counts],
- marker='x',
- color='red',
- linestyle='-',
- linewidth=2,
- label='SEQ'
- )
- for threads in unique_thread_counts:
- if threads>1:
- plt.plot(
- unique_request_counts,
- [np.mean(async_durations[count][threads]) for count in unique_request_counts],
- marker='s',
- linestyle='--',
- linewidth=1.5,
- label=f'Async with {threads} Threads'
- )
- plt.xlabel('NUM REQUESTS')
- plt.ylabel('DURATION')
- plt.title('SEQ vs ASYNC REQUESTS')
- plt.legend(loc='best')
- plt.grid(True, which='both', linestyle='--', linewidth=0.5)
- plt.tight_layout()
- plt.show()
- def RUN_EXPERIMENT():
- request_counts=[1,5,10,20,30]
- thread_counts=range(1,10)
- experiment_results=[]
- for req_count in request_counts:
- for thrd_count in thread_counts:
- result=EVALUATE_PERFORMANCE(req_count, thrd_count)
- experiment_results.append(result)
- VISUALIZE_RESULTS(experiment_results)
- if __name__=='__main__':
- RUN_EXPERIMENT()
Advertisement
Add Comment
Please, Sign In to add comment