Compare commits

..

No commits in common. "dae7bc720b635e70fd31c7c421245cb9f1fa90bf" and "da6ba57b480410a961c2934dd0dbc00197fda8fc" have entirely different histories.

9 changed files with 39 additions and 71 deletions

View File

@ -38,6 +38,3 @@ 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,7 +17,6 @@ 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,4 +10,3 @@ 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,4 +1,3 @@
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,9 +37,6 @@ 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
@ -106,69 +103,56 @@ 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("/db/insert", self.insert_handler) self.router.add_post("/insert", self.insert_handler)
self.router.add_post("/db/update", self.update_handler) self.router.add_post("/update", self.update_handler)
self.router.add_post("/db/upsert", self.upsert_handler) self.router.add_post("/upsert", self.upsert_handler)
self.router.add_post("/db/find", self.find_handler) self.router.add_post("/find", self.find_handler)
self.router.add_post("/db/find_one", self.find_one_handler) self.router.add_post("/find_one", self.find_one_handler)
self.router.add_post("/db/delete", self.delete_handler) self.router.add_post("/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):
obj = await request.json() await request.json()
response = await self.insert(obj.get("table"), obj.get("data")) response = await self.insert(request.get("table"), request.get("data"))
return web.json_response(response) return web.json_response(response)
async def update_handler(self, request): async def update_handler(self, request):
obj = await request.json() await request.json()
response = await self.update( response = await self.update(
obj.get("table"), obj.get("data"), obj.get("where", {}) request.get("table"), request.get("data"), request.get("where", {})
) )
return web.json_response(response) return web.json_response(response)
async def upsert_handler(self, request): async def upsert_handler(self, request):
obj = await request.json() await request.json()
response = await self.upsert( response = await self.upsert(
obj.get("table"), obj.get("data"), obj.get("keys", []) request.get("table"), request.get("data"), request.get("keys", [])
) )
return web.json_response(response) return web.json_response(response)
async def find_handler(self, request): async def find_handler(self, request):
obj = await request.json() await request.json()
response = await self.find(obj.get("table"), obj.get("where", {})) response = await self.find(request.get("table"), requesst.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):
obj = await request.json() await request.json()
response = await self.find_one(obj.get("table"), obj.get("where", {})) response = await self.find_one(request.get("table"), requesst.get("where", {}))
return web.json_response(response) return web.json_response(response)
async def delete_handler(self, request): async def delete_handler(self, request):
obj = await request.json() await request.json()
response = await self.delete(obj.get("table"), obj.get("where", {})) response = await self.delete(request.get("table"), requesst.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)
return self.db["kv"].upsert({"key": key, "value": value}, ["key"]) 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)
@ -207,7 +191,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("/stat", self.index_handler) self.router.add_get("/", 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,28 +20,21 @@ async def cli_client(url):
async def bench(url): async def bench(url):
index = 0.0 index = 0
# Wait until server is up while True:
await asyncio.sleep(2) index += 1
duration_limit = 15.0 try:
time_start = time.time() time_start = time.time()
log.info("Benchmarking.")
async with ClientSession() as session: async with ClientSession() as session:
while True: async with session.get(url) as response:
index += 1 print(await response.text())
try: # print(await response.json())
async with session.get(url.rstrip("/") + "/stat"): time_end = time.time()
pass print(f"Request {index}. Duration: {time_end - time_start}")
time_end = time.time() except Exception as ex:
duration = time_end - time_start log.exception(ex)
if duration >= duration_limit: await asyncio.sleep(1)
break
except Exception as ex:
log.exception(ex)
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,15 +1,12 @@
from IPython.terminal.embed import InteractiveShellEmbed import code
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__":