Guest User

updated src

a guest
Aug 1st, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import requests
  2. import aiohttp
  3. import asyncio
  4. import time
  5. import ssl
  6. import certifi
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9.  
  10. API_URL='https://api.github.com/users/DimitriChrysafis/repos'
  11.  
  12. def EXECUTE_SEQUENTIAL_REQUESTS(api_url:str, request_count:int)->float:
  13. start=time.time()
  14. for _ in range(request_count):
  15. requests.get(api_url).json()
  16. end=time.time()
  17. return end-start
  18.  
  19. def GENERATE_SSL_CONTEXT()->ssl.SSLContext:
  20. return ssl.create_default_context(cafile=certifi.where())
  21.  
  22. async def PERFORM_FETCH(session:aiohttp.ClientSession, api_url:str)->dict:
  23. async with session.get(api_url, ssl=GENERATE_SSL_CONTEXT()) as response:
  24. return await response.json()
  25.  
  26. async def PERFORM_ASYNC_REQUESTS(api_url:str, request_count:int, thread_count:int)->float:
  27. start=time.time()
  28. async with aiohttp.ClientSession() as session:
  29. tasks=[PERFORM_FETCH(session, api_url) for _ in range(request_count)]
  30. await asyncio.gather(*tasks)
  31. end=time.time()
  32. return end-start
  33.  
  34. def EVALUATE_PERFORMANCE(request_count:int, thread_count:int)->dict:
  35. sequential_duration=EXECUTE_SEQUENTIAL_REQUESTS(API_URL, request_count)
  36. async_duration=asyncio.run(PERFORM_ASYNC_REQUESTS(API_URL, request_count, thread_count))
  37. return {
  38. 'request_count':request_count,
  39. 'thread_count':thread_count,
  40. 'sequential_duration':sequential_duration,
  41. 'async_duration':async_duration
  42. }
  43.  
  44. def VISUALIZE_RESULTS(results:list):
  45. unique_request_counts=sorted(set(result['request_count'] for result in results))
  46. unique_thread_counts=sorted(set(result['thread_count'] for result in results))
  47. sequential_durations={count:[] for count in unique_request_counts}
  48. async_durations={count:{threads:[] for threads in unique_thread_counts} for count in unique_request_counts}
  49. for result in results:
  50. req_count=result['request_count']
  51. thrd_count=result['thread_count']
  52. if thrd_count==1:
  53. sequential_durations[req_count].append(result['sequential_duration'])
  54. else:
  55. async_durations[req_count][thrd_count].append(result['async_duration'])
  56. plt.figure(figsize=(14,9))
  57. plt.plot(
  58. unique_request_counts,
  59. [np.mean(sequential_durations[count]) for count in unique_request_counts],
  60. marker='x',
  61. color='red',
  62. linestyle='-',
  63. linewidth=2,
  64. label='SEQ'
  65. )
  66. for threads in unique_thread_counts:
  67. if threads>1:
  68. plt.plot(
  69. unique_request_counts,
  70. [np.mean(async_durations[count][threads]) for count in unique_request_counts],
  71. marker='s',
  72. linestyle='--',
  73. linewidth=1.5,
  74. label=f'Async with {threads} Threads'
  75. )
  76. plt.xlabel('NUM REQUESTS')
  77. plt.ylabel('DURATION')
  78. plt.title('SEQ vs ASYNC REQUESTS')
  79. plt.legend(loc='best')
  80. plt.grid(True, which='both', linestyle='--', linewidth=0.5)
  81. plt.tight_layout()
  82. plt.show()
  83.  
  84. def RUN_EXPERIMENT():
  85. request_counts=[1,5,10,20,30]
  86. thread_counts=range(1,10)
  87. experiment_results=[]
  88. for req_count in request_counts:
  89. for thrd_count in thread_counts:
  90. result=EVALUATE_PERFORMANCE(req_count, thrd_count)
  91. experiment_results.append(result)
  92. VISUALIZE_RESULTS(experiment_results)
  93.  
  94. if __name__=='__main__':
  95. RUN_EXPERIMENT()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment