Browse Source

simple test

chenhaiyang 4 years ago
parent
commit
ee876c216f
1 changed files with 49 additions and 0 deletions
  1. 49 0
      tests/test.py

+ 49 - 0
tests/test.py

@@ -0,0 +1,49 @@
+# -*- 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())