12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # -*- coding: utf-8 -*-
- import asyncio
- import time
- import httpx
- import requests
- async def request(client):
- await client.get("http://172.16.47.29:8000/users")
- async def main():
- epochs = 1000
- async with httpx.AsyncClient() as client:
- start = time.process_time()
- task_list = []
- for _ in range(epochs):
- req = request(client)
- task = asyncio.create_task(req)
- task_list.append(task)
- await asyncio.gather(*task_list)
- end = time.process_time()
- print(f"Sent {epochs} requests in method 1,elapsed:{end - start}")
- start = time.process_time()
- for _ in range(epochs):
- async with httpx.AsyncClient() as client:
- await request(client)
- end = time.process_time()
- print(f"Sent {epochs} requests in method 2,elapsed:{end - start}")
- start = time.process_time()
- for _ in range(epochs):
- with httpx.Client() as client:
- client.get("http://172.16.47.29:8000/users")
- end = time.process_time()
- print(f"Sent {epochs} requests in method 3, elapsed: {end - start}")
- start = time.process_time()
- for _ in range(epochs):
- requests.get("http://172.16.47.29:8000/users")
- end = time.process_time()
- print(f"Sent {epochs} requests by requests, elapsed: {end - start}")
- if __name__ == "__main__":
- asyncio.run(main())
|