Compare commits

..

2 Commits

Author SHA1 Message Date
dae7bc720b Update.
All checks were successful
Build Base Application / Build (push) Successful in 1m38s
2024-12-03 23:30:11 +01:00
7e040bb70a Update. 2024-12-03 23:25:17 +01:00
9 changed files with 71 additions and 39 deletions

View File

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

Binary file not shown.

BIN
dist/app-1.0.0.tar.gz vendored

Binary file not shown.

View File

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

View File

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

View File

@ -1,3 +1,4 @@
aiohttp aiohttp
dataset dataset
zhurnal@ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main 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) middlewares.append(self.session_middleware)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def run(self, *args, **kwargs):
web.run_app(self, *args, **kwargs)
async def authenticate(self, username, password): async def authenticate(self, username, password):
return self.username == username and self.password == password return self.username == username and self.password == password
@ -103,56 +106,69 @@ class WebDbApplication(BaseApplication):
def __init__( def __init__(
self, db=None, db_web=False, db_path="sqlite:///:memory:", *args, **kwargs self, db=None, db_web=False, db_path="sqlite:///:memory:", *args, **kwargs
): ):
super().__init__(*args, **kwargs)
self.db_web = db_web self.db_web = db_web
self.db_path = db_path self.db_path = db_path
self.db = db or dataset.connect(self.db_path) self.db = db or dataset.connect(self.db_path)
super().__init__(*args, **kwargs)
if not self.db_web: if not self.db_web:
return return
self.router.add_post("/insert", self.insert_handler) self.router.add_post("/db/insert", self.insert_handler)
self.router.add_post("/update", self.update_handler) self.router.add_post("/db/update", self.update_handler)
self.router.add_post("/upsert", self.upsert_handler) self.router.add_post("/db/upsert", self.upsert_handler)
self.router.add_post("/find", self.find_handler) self.router.add_post("/db/find", self.find_handler)
self.router.add_post("/find_one", self.find_one_handler) self.router.add_post("/db/find_one", self.find_one_handler)
self.router.add_post("/delete", self.delete_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): async def insert_handler(self, request):
await request.json() obj = await request.json()
response = await self.insert(request.get("table"), request.get("data")) response = await self.insert(obj.get("table"), obj.get("data"))
return web.json_response(response) return web.json_response(response)
async def update_handler(self, request): async def update_handler(self, request):
await request.json() obj = await request.json()
response = await self.update( 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) return web.json_response(response)
async def upsert_handler(self, request): async def upsert_handler(self, request):
await request.json() obj = await request.json()
response = await self.upsert( 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) return web.json_response(response)
async def find_handler(self, request): async def find_handler(self, request):
await request.json() obj = await request.json()
response = await self.find(request.get("table"), requesst.get("where", {})) response = await self.find(obj.get("table"), obj.get("where", {}))
return web.json_response(response) return web.json_response(response)
async def find_one_handler(self, request): async def find_one_handler(self, request):
await request.json() obj = await request.json()
response = await self.find_one(request.get("table"), requesst.get("where", {})) response = await self.find_one(obj.get("table"), obj.get("where", {}))
return web.json_response(response) return web.json_response(response)
async def delete_handler(self, request): async def delete_handler(self, request):
await request.json() obj = await request.json()
response = await self.delete(request.get("table"), requesst.get("where", {})) response = await self.delete(obj.get("table"), obj.get("where", {}))
return web.json_response(response) return web.json_response(response)
async def set(self, key, value): async def set(self, key, value):
value = json.dumps(value, default=str) 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): async def get(self, key, default=None):
record = self.db["kv"].find_one(key=key) record = self.db["kv"].find_one(key=key)
@ -191,7 +207,7 @@ class Application(WebDbApplication):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.on_startup.append(self.on_startup_task) 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.request_count = 0
self.time_started = time.time() self.time_started = time.time()
self.running_since = None self.running_since = None

View File

@ -20,21 +20,28 @@ async def cli_client(url):
async def bench(url): async def bench(url):
index = 0 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: while True:
index += 1 index += 1
try: try:
time_start = time.time() async with session.get(url.rstrip("/") + "/stat"):
pass
async with ClientSession() as session:
async with session.get(url) as response:
print(await response.text())
# print(await response.json())
time_end = time.time() time_end = time.time()
print(f"Request {index}. Duration: {time_end - time_start}") duration = time_end - time_start
if duration >= duration_limit:
break
except Exception as ex: except Exception as ex:
log.exception(ex) log.exception(ex)
await asyncio.sleep(1) await asyncio.sleep(1)
log.info(
f"{index / duration_limit} requests per second. Made {index} requests in total in duration of {duration} seconds."
)
def cli_bench(): def cli_bench():

View File

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