initial working proof of concept
This commit is contained in:
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
67
src/main.py
Normal file
67
src/main.py
Normal file
@ -0,0 +1,67 @@
|
||||
import asyncio
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastapi import status
|
||||
from hypercorn.config import Config
|
||||
from hypercorn.asyncio import serve
|
||||
|
||||
api = FastAPI()
|
||||
|
||||
BUFFER_SIZE = 1024 * 4 # 4KB
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 9250
|
||||
|
||||
|
||||
async def generate_test_data(size: int | str, max_size=1024 * 1024) -> bytes:
|
||||
size_left = None
|
||||
|
||||
try:
|
||||
print('Here1')
|
||||
size_left = int(size)
|
||||
except ValueError: # treat as string
|
||||
print('Here2')
|
||||
# TODO: maybe hardcode values
|
||||
units = {
|
||||
'GB': 10 ** 9, 'GiB': 2 ** 30,
|
||||
'MB': 10 ** 6, 'MiB': 2 ** 20,
|
||||
'KB': 10 ** 3, 'KiB': 2 ** 10,
|
||||
'B': 1
|
||||
}
|
||||
|
||||
for unit in units:
|
||||
if size.endswith(unit):
|
||||
size_left = int(float(size.removesuffix(unit)) * units[unit])
|
||||
break
|
||||
|
||||
print('size_left', size_left)
|
||||
|
||||
# yield data
|
||||
while size_left > BUFFER_SIZE:
|
||||
size_left -= BUFFER_SIZE
|
||||
yield b'\0' * BUFFER_SIZE
|
||||
else:
|
||||
yield b'\0' * size_left
|
||||
|
||||
|
||||
@api.get('/')
|
||||
async def test_data(size: str) -> StreamingResponse:
|
||||
try:
|
||||
return StreamingResponse(
|
||||
status_code=status.HTTP_200_OK,
|
||||
content=generate_test_data(size)
|
||||
)
|
||||
except err:
|
||||
raise err
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
asyncio.run(serve(
|
||||
api,
|
||||
Config().from_object({'bind': [f'{HOST}:{PORT}']})
|
||||
))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
0
src/test.py
Normal file
0
src/test.py
Normal file
Reference in New Issue
Block a user