Skip to content

Commit 3d56992

Browse files
Restructure to remove server config from timing
1 parent 556e08f commit 3d56992

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

pyperformance/data-files/benchmarks/bm_fastapi/run_benchmark.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import asyncio
1616
import socket
17-
import time
1817

1918
import httpx
2019
import pyperf
@@ -44,41 +43,42 @@ async def get_item(item_id: int):
4443
}
4544

4645

47-
async def run_server_and_benchmark(loops):
46+
def bench_fastapi(loops):
4847
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
4948
s.bind((HOST, 0))
5049
s.listen(1)
5150
port = s.getsockname()[1]
5251

52+
# Setup server
5353
config = uvicorn.Config(app, host=HOST, port=port, log_level="error")
5454
server = uvicorn.Server(config)
55-
server_task = asyncio.create_task(server.serve())
5655

57-
await asyncio.sleep(0.5)
56+
async def run_benchmark():
57+
server_task = asyncio.create_task(server.serve())
58+
await asyncio.sleep(0.5)
5859

59-
async with httpx.AsyncClient() as client:
60-
t0 = time.perf_counter()
60+
async with httpx.AsyncClient() as client:
61+
t0 = pyperf.perf_counter()
6162

62-
for i in range(loops):
63-
tasks = [
64-
client.get(f"http://{HOST}:{port}/items/{i}")
65-
for _ in range(CONCURRENCY)
66-
]
67-
responses = await asyncio.gather(*tasks)
68-
for response in responses:
69-
response.raise_for_status()
70-
data = response.json()
71-
assert data["id"] == i
72-
assert "tags" in data
63+
for i in range(loops):
64+
tasks = [
65+
client.get(f"http://{HOST}:{port}/items/{i}")
66+
for _ in range(CONCURRENCY)
67+
]
68+
responses = await asyncio.gather(*tasks)
69+
for response in responses:
70+
response.raise_for_status()
71+
data = response.json()
72+
assert data["id"] == i
73+
assert "tags" in data
7374

74-
elapsed = time.perf_counter() - t0
75+
elapsed = pyperf.perf_counter() - t0
7576

76-
server.should_exit = True
77-
await server_task
78-
return elapsed
77+
server.should_exit = True
78+
await server_task
79+
return elapsed
7980

80-
def bench_fastapi(loops):
81-
return asyncio.run(run_server_and_benchmark(loops))
81+
return asyncio.run(run_benchmark())
8282

8383

8484
if __name__ == "__main__":

0 commit comments

Comments
 (0)