Compare commits
No commits in common. "dae7bc720b635e70fd31c7c421245cb9f1fa90bf" and "da6ba57b480410a961c2934dd0dbc00197fda8fc" have entirely different histories.
dae7bc720b
...
da6ba57b48
3
Makefile
3
Makefile
@ -38,6 +38,3 @@ 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,7 +17,6 @@ install_requires =
|
||||
aiohttp
|
||||
dataset
|
||||
zhurnal @ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
|
||||
ipython
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
|
@ -10,4 +10,3 @@ 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,4 +1,3 @@
|
||||
aiohttp
|
||||
dataset
|
||||
zhurnal@ git+https://retoor.molodetz.nl/retoor/zhurnal.git@main
|
||||
ipython
|
||||
|
@ -37,9 +37,6 @@ 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
|
||||
|
||||
@ -106,69 +103,56 @@ 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("/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)
|
||||
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)
|
||||
|
||||
async def insert_handler(self, request):
|
||||
obj = await request.json()
|
||||
response = await self.insert(obj.get("table"), obj.get("data"))
|
||||
await request.json()
|
||||
response = await self.insert(request.get("table"), request.get("data"))
|
||||
return web.json_response(response)
|
||||
|
||||
async def update_handler(self, request):
|
||||
obj = await request.json()
|
||||
await request.json()
|
||||
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)
|
||||
|
||||
async def upsert_handler(self, request):
|
||||
obj = await request.json()
|
||||
await request.json()
|
||||
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)
|
||||
|
||||
async def find_handler(self, request):
|
||||
obj = await request.json()
|
||||
response = await self.find(obj.get("table"), obj.get("where", {}))
|
||||
await request.json()
|
||||
response = await self.find(request.get("table"), requesst.get("where", {}))
|
||||
return web.json_response(response)
|
||||
|
||||
async def find_one_handler(self, request):
|
||||
obj = await request.json()
|
||||
response = await self.find_one(obj.get("table"), obj.get("where", {}))
|
||||
await request.json()
|
||||
response = await self.find_one(request.get("table"), requesst.get("where", {}))
|
||||
return web.json_response(response)
|
||||
|
||||
async def delete_handler(self, request):
|
||||
obj = await request.json()
|
||||
response = await self.delete(obj.get("table"), obj.get("where", {}))
|
||||
await request.json()
|
||||
response = await self.delete(request.get("table"), requesst.get("where", {}))
|
||||
return web.json_response(response)
|
||||
|
||||
async def set(self, key, value):
|
||||
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):
|
||||
record = self.db["kv"].find_one(key=key)
|
||||
@ -207,7 +191,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("/stat", self.index_handler)
|
||||
self.router.add_get("/", self.index_handler)
|
||||
self.request_count = 0
|
||||
self.time_started = time.time()
|
||||
self.running_since = None
|
||||
|
@ -20,28 +20,21 @@ async def cli_client(url):
|
||||
|
||||
|
||||
async def bench(url):
|
||||
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:
|
||||
index = 0
|
||||
while True:
|
||||
index += 1
|
||||
try:
|
||||
async with session.get(url.rstrip("/") + "/stat"):
|
||||
pass
|
||||
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()
|
||||
duration = time_end - time_start
|
||||
if duration >= duration_limit:
|
||||
break
|
||||
print(f"Request {index}. Duration: {time_end - time_start}")
|
||||
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,15 +1,12 @@
|
||||
from IPython.terminal.embed import InteractiveShellEmbed
|
||||
import code
|
||||
|
||||
|
||||
def repl(**kwargs):
|
||||
|
||||
variables = {}
|
||||
varlables = {}
|
||||
variables.update(globals().copy())
|
||||
variables.update(locals())
|
||||
variables.update(kwargs)
|
||||
|
||||
shell = InteractiveShellEmbed(exit_msg="Exiting...")
|
||||
shell(local_ns=variables)
|
||||
code.interact(local=variables)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user