This commit is contained in:
retoor 2024-12-03 23:25:17 +01:00
parent da6ba57b48
commit 7e040bb70a
7 changed files with 71 additions and 40 deletions

View File

@ -38,3 +38,6 @@ cli: ensure_env
test: ensure_env
$(PYTHON) -m unittest $(APP_NAME).tests
repl: ensure_env
$(BIN)repl

View File

@ -17,6 +17,7 @@ install_requires =
aiohttp
dataset
zhurnal @ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
ipython
[options.packages.find]
where = src

View File

@ -10,3 +10,4 @@ Description-Content-Type: text/markdown
Requires-Dist: aiohttp
Requires-Dist: dataset
Requires-Dist: zhurnal@ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
Requires-Dist: ipython

View File

@ -1,3 +1,4 @@
aiohttp
dataset
zhurnal@ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
ipython

View File

@ -37,6 +37,9 @@ class BaseApplication(web.Application):
middlewares.append(self.session_middleware)
super().__init__(*args, **kwargs)
def run(self, *args, **kwargs):
web.run_app(self,*args,**kwargs)
async def authenticate(self, username, password):
return self.username == username and self.password == password
@ -103,56 +106,69 @@ class WebDbApplication(BaseApplication):
def __init__(
self, db=None, db_web=False, db_path="sqlite:///:memory:", *args, **kwargs
):
super().__init__(*args, **kwargs)
self.db_web = db_web
self.db_path = db_path
self.db = db or dataset.connect(self.db_path)
super().__init__(*args, **kwargs)
if not self.db_web:
return
self.router.add_post("/insert", self.insert_handler)
self.router.add_post("/update", self.update_handler)
self.router.add_post("/upsert", self.upsert_handler)
self.router.add_post("/find", self.find_handler)
self.router.add_post("/find_one", self.find_one_handler)
self.router.add_post("/delete", self.delete_handler)
self.router.add_post("/db/insert", self.insert_handler)
self.router.add_post("/db/update", self.update_handler)
self.router.add_post("/db/upsert", self.upsert_handler)
self.router.add_post("/db/find", self.find_handler)
self.router.add_post("/db/find_one", self.find_one_handler)
self.router.add_post("/db/delete", self.delete_handler)
self.router.add_post("/db/get", self.get_handler)
self.router.add_post("/db/set", self.set_handler)
async def set_handler(self,request):
obj = await request.json()
response = await self.set(obj.get("key"),obj.get("value"))
return web.json_response(response)
async def get_handler(self, request):
obj = await request.json()
response = await self.get(obj.get("key"), None)
return web.json_response(response)
async def insert_handler(self, request):
await request.json()
response = await self.insert(request.get("table"), request.get("data"))
obj = await request.json()
response = await self.insert(obj.get("table"), obj.get("data"))
return web.json_response(response)
async def update_handler(self, request):
await request.json()
obj = await request.json()
response = await self.update(
request.get("table"), request.get("data"), request.get("where", {})
obj.get("table"), obj.get("data"), obj.get("where", {})
)
return web.json_response(response)
async def upsert_handler(self, request):
await request.json()
obj = await request.json()
response = await self.upsert(
request.get("table"), request.get("data"), request.get("keys", [])
obj.get("table"), obj.get("data"), obj.get("keys", [])
)
return web.json_response(response)
async def find_handler(self, request):
await request.json()
response = await self.find(request.get("table"), requesst.get("where", {}))
obj = await request.json()
response = await self.find(obj.get("table"), obj.get("where", {}))
return web.json_response(response)
async def find_one_handler(self, request):
await request.json()
response = await self.find_one(request.get("table"), requesst.get("where", {}))
obj = await request.json()
response = await self.find_one(obj.get("table"), obj.get("where", {}))
return web.json_response(response)
async def delete_handler(self, request):
await request.json()
response = await self.delete(request.get("table"), requesst.get("where", {}))
obj = await request.json()
response = await self.delete(obj.get("table"), obj.get("where", {}))
return web.json_response(response)
async def set(self, key, value):
value = json.dumps(value, default=str)
self.db["kv"].upsert({"key": key, "value": value}, ["key"])
return self.db["kv"].upsert({"key": key, "value": value}, ["key"])
async def get(self, key, default=None):
record = self.db["kv"].find_one(key=key)
@ -191,7 +207,7 @@ class Application(WebDbApplication):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.on_startup.append(self.on_startup_task)
self.router.add_get("/", self.index_handler)
self.router.add_get("/stat", self.index_handler)
self.request_count = 0
self.time_started = time.time()
self.running_since = None

View File

@ -20,22 +20,26 @@ async def cli_client(url):
async def bench(url):
index = 0
while True:
index += 1
try:
time_start = time.time()
async with ClientSession() as session:
async with session.get(url) as response:
print(await response.text())
# print(await response.json())
time_end = time.time()
print(f"Request {index}. Duration: {time_end - time_start}")
except Exception as ex:
log.exception(ex)
await asyncio.sleep(1)
index = 0.0
# Wait until server is up
await asyncio.sleep(2)
duration_limit = 15.0
time_start = time.time()
log.info("Benchmarking.")
async with ClientSession() as session:
while True:
index += 1
try:
async with session.get(url.rstrip("/") + "/stat") as response:
pass
time_end = time.time()
duration = time_end - time_start
if duration >= duration_limit:
break
except Exception as ex:
log.exception(ex)
await asyncio.sleep(1)
log.info("{} requests per second. Made {} requests in total in duration of {} seconds.".format(index/duration_limit,index,duration))
def cli_bench():
args = parse_args()

View File

@ -1,12 +1,17 @@
import code
from IPython.terminal.embed import InteractiveShellEmbed
def repl(**kwargs):
varlables = {}
from .app import Application
variables = {}
variables.update(globals().copy())
variables.update(locals())
variables.update(kwargs)
code.interact(local=variables)
shell = InteractiveShellEmbed(
exit_msg="Exiting..."
)
shell(local_ns=variables)
if __name__ == "__main__":