-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
201 lines (152 loc) · 6.5 KB
/
server.py
File metadata and controls
201 lines (152 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from contextvars import ContextVar
import time
from typing import Annotated
from fastapi import FastAPI, File, Request, Response, UploadFile
import psutil
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from models import ServerResponse
app = FastAPI()
# Chunk size for file reading (0.25 MB)
# This size was chosen to match the typical chunk size emitted by request.stream()
# for a more fair comparison between different upload methods
CHUNK_SIZE = 256 * 1024
# Context variables to store timing information
request_start_time: ContextVar[float] = ContextVar("request_start_time")
request_start_memory: ContextVar[float] = ContextVar("request_start_memory")
def get_memory_usage_mb() -> float:
"""Get current process memory usage in MB"""
process = psutil.Process()
return process.memory_info().rss / 1024 / 1024
class TimingMiddleware(BaseHTTPMiddleware):
"""Middleware to capture total request processing time including FastAPI overhead"""
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
# Capture timing and memory BEFORE any FastAPI processing
start_time = time.perf_counter()
start_memory = get_memory_usage_mb()
# Store in context for route handlers to access
request_start_time.set(start_time)
request_start_memory.set(start_memory)
# Process request
response = await call_next(request)
# Calculate total request metrics
end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
total_duration = end_time - start_time
# Add timing headers for inspection
response.headers["X-Total-Duration"] = str(total_duration)
response.headers["X-Total-Memory-Delta"] = str(end_memory - start_memory)
return response
# Add middleware
app.add_middleware(TimingMiddleware)
@app.post("/upload/sync-file")
def upload_sync_file(file: Annotated[bytes, File()]) -> ServerResponse:
"""Sync route with File() - FastAPI loads entire file into memory before handler"""
# Get middleware timing
start_memory = request_start_memory.get()
# Handler timing starts here
handler_start_time = time.perf_counter()
# File is already in memory as bytes (loaded by FastAPI, cannot be chunked)
file_size = len(file)
handler_end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
handler_duration = handler_end_time - handler_start_time
return ServerResponse(
endpoint="sync-file",
file_size_bytes=file_size,
file_size_mb=file_size / 1024 / 1024,
handler_duration_seconds=handler_duration,
memory_start_mb=start_memory,
memory_end_mb=end_memory,
memory_delta_mb=end_memory - start_memory,
)
@app.post("/upload/async-file")
async def upload_async_file(file: Annotated[bytes, File()]) -> ServerResponse:
"""Async route with File() - FastAPI loads entire file into memory before handler"""
# Get middleware timing
start_memory = request_start_memory.get()
# Handler timing starts here
handler_start_time = time.perf_counter()
# File is already in memory as bytes (loaded by FastAPI, cannot be chunked)
file_size = len(file)
handler_end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
handler_duration = handler_end_time - handler_start_time
return ServerResponse(
endpoint="async-file",
file_size_bytes=file_size,
file_size_mb=file_size / 1024 / 1024,
handler_duration_seconds=handler_duration,
memory_start_mb=start_memory,
memory_end_mb=end_memory,
memory_delta_mb=end_memory - start_memory,
)
@app.post("/upload/sync-uploadfile")
def upload_sync_uploadfile(file: UploadFile) -> ServerResponse:
"""Sync route with UploadFile using sync chunked reading"""
start_memory = request_start_memory.get()
# Handler timing starts here
handler_start_time = time.perf_counter()
# Read file synchronously in chunks using the underlying file object
file_size = 0
while chunk := file.file.read(CHUNK_SIZE):
file_size += len(chunk)
handler_end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
handler_duration = handler_end_time - handler_start_time
return ServerResponse(
endpoint="sync-uploadfile",
file_size_bytes=file_size,
file_size_mb=file_size / 1024 / 1024,
handler_duration_seconds=handler_duration,
memory_start_mb=start_memory,
memory_end_mb=end_memory,
memory_delta_mb=end_memory - start_memory,
)
@app.post("/upload/async-uploadfile")
async def upload_async_uploadfile(file: UploadFile) -> ServerResponse:
"""Async route with UploadFile using async chunked reading"""
start_memory = request_start_memory.get()
# Handler timing starts here
handler_start_time = time.perf_counter()
# Read file asynchronously in chunks
file_size = 0
while chunk := await file.read(CHUNK_SIZE):
file_size += len(chunk)
handler_end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
handler_duration = handler_end_time - handler_start_time
return ServerResponse(
endpoint="async-uploadfile",
file_size_bytes=file_size,
file_size_mb=file_size / 1024 / 1024,
handler_duration_seconds=handler_duration,
memory_start_mb=start_memory,
memory_end_mb=end_memory,
memory_delta_mb=end_memory - start_memory,
)
@app.post("/upload/async-stream")
async def upload_async_stream(request: Request) -> ServerResponse:
"""Async route using request.stream() with async processing"""
# Get middleware timing
start_memory = request_start_memory.get()
# Handler timing starts here
handler_start_time = time.perf_counter()
# Process stream asynchronously (no file writing, just consume the data)
file_size = 0
async for chunk in request.stream():
file_size += len(chunk)
handler_end_time = time.perf_counter()
end_memory = get_memory_usage_mb()
handler_duration = handler_end_time - handler_start_time
return ServerResponse(
endpoint="async-stream",
file_size_bytes=file_size,
file_size_mb=file_size / 1024 / 1024,
handler_duration_seconds=handler_duration,
memory_start_mb=start_memory,
memory_end_mb=end_memory,
memory_delta_mb=end_memory - start_memory,
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)