test.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. import asyncio
  3. import time
  4. import httpx
  5. import requests
  6. async def request(client):
  7. await client.get("http://172.16.47.29:8000/users")
  8. async def main():
  9. epochs = 1000
  10. async with httpx.AsyncClient() as client:
  11. start = time.process_time()
  12. task_list = []
  13. for _ in range(epochs):
  14. req = request(client)
  15. task = asyncio.create_task(req)
  16. task_list.append(task)
  17. await asyncio.gather(*task_list)
  18. end = time.process_time()
  19. print(f"Sent {epochs} requests in method 1,elapsed:{end - start}")
  20. start = time.process_time()
  21. for _ in range(epochs):
  22. async with httpx.AsyncClient() as client:
  23. await request(client)
  24. end = time.process_time()
  25. print(f"Sent {epochs} requests in method 2,elapsed:{end - start}")
  26. start = time.process_time()
  27. for _ in range(epochs):
  28. with httpx.Client() as client:
  29. client.get("http://172.16.47.29:8000/users")
  30. end = time.process_time()
  31. print(f"Sent {epochs} requests in method 3, elapsed: {end - start}")
  32. start = time.process_time()
  33. for _ in range(epochs):
  34. requests.get("http://172.16.47.29:8000/users")
  35. end = time.process_time()
  36. print(f"Sent {epochs} requests by requests, elapsed: {end - start}")
  37. if __name__ == "__main__":
  38. asyncio.run(main())