Compare commits
2 Commits
da6ba57b48
...
dae7bc720b
Author | SHA1 | Date | |
---|---|---|---|
dae7bc720b | |||
7e040bb70a |
3
Makefile
3
Makefile
@ -38,3 +38,6 @@ cli: ensure_env
|
||||
|
||||
test: ensure_env
|
||||
$(PYTHON) -m unittest $(APP_NAME).tests
|
||||
|
||||
repl: ensure_env
|
||||
$(BIN)repl
|
||||
|
BIN
dist/app-1.0.0-py3-none-any.whl
vendored
BIN
dist/app-1.0.0-py3-none-any.whl
vendored
Binary file not shown.
BIN
dist/app-1.0.0.tar.gz
vendored
BIN
dist/app-1.0.0.tar.gz
vendored
Binary file not shown.
@ -17,6 +17,7 @@ install_requires =
|
||||
aiohttp
|
||||
dataset
|
||||
zhurnal @ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
|
||||
ipython
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
|
@ -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
|
||||
|
@ -1,3 +1,4 @@
|
||||
aiohttp
|
||||
dataset
|
||||
zhurnal@ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
|
||||
ipython
|
||||
|
@ -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
|
||||
|
@ -20,21 +20,28 @@ async def cli_client(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:
|
||||
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())
|
||||
async with session.get(url.rstrip("/") + "/stat"):
|
||||
pass
|
||||
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:
|
||||
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():
|
||||
|
@ -1,12 +1,15 @@
|
||||
import code
|
||||
from IPython.terminal.embed import InteractiveShellEmbed
|
||||
|
||||
|
||||
def repl(**kwargs):
|
||||
varlables = {}
|
||||
|
||||
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__":
|
||||
|
Loading…
Reference in New Issue
Block a user