Added profiler.

This commit is contained in:
retoor 2025-02-18 13:29:33 +01:00
parent ebb520dd4a
commit c6620ad70a
3 changed files with 49 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import pathlib
import asyncio
import logging
logging.basicConfig(level=logging.DEBUG)
from aiohttp import web
@ -32,7 +33,7 @@ from snek.view.status import StatusView
from snek.view.web import WebView
from snek.view.upload import UploadView
from snek.view.search_user import SearchUserView
from snek.system.profiler import profiler_handler
SESSION_KEY = b"c79a0c5fda4b424189c427d28c9f7c34"
@ -43,7 +44,6 @@ async def session_middleware(request, handler):
response = await handler(request)
return response
class Application(BaseApplication):
def __init__(self, *args, **kwargs):
@ -77,6 +77,7 @@ class Application(BaseApplication):
name="static",
show_index=True,
)
self.router.add_view("/profiler.html", profiler_handler)
self.router.add_view("/about.html", AboutHTMLView)
self.router.add_view("/about.md", AboutMDView)
self.router.add_view("/logout.json", LogoutView)
@ -128,8 +129,6 @@ class Application(BaseApplication):
async def main():
app = Application(db_path="sqlite:///snek.db")
loop = asyncio.get_event_loop()
loop.set_debug(True)
await web._run_app(app, port=8081, host="0.0.0.0")
if __name__ == "__main__":

View File

@ -0,0 +1,42 @@
import cProfile
import pstats
import sys
from aiohttp import web
profiler = None
import io
@web.middleware
async def profile_middleware(request, handler):
global profiler
if not profiler:
profiler = cProfile.Profile()
profiler.enable()
response = await handler(request)
profiler.disable()
stats = pstats.Stats(profiler, stream=sys.stdout)
stats.sort_stats('cumulative')
stats.print_stats()
return response
async def profiler_handler(request):
output = io.StringIO()
stats = pstats.Stats(profiler, stream=output)
stats.sort_stats('cumulative')
stats.print_stats()
return web.Response(text=output.getvalue())
class Profiler:
def __init__(self):
global profiler
if profiler is None:
profiler = cProfile.Profile()
self.profiler = profiler
async def __aenter__(self):
self.profiler.enable()
async def __aexit__(self, *args, **kwargs):
self.profiler.disable()

View File

@ -12,6 +12,7 @@ from snek.system.view import BaseView
import traceback
import json
from snek.system.model import now
from snek.system.profiler import Profiler
class RPCView(BaseView):
@ -169,8 +170,10 @@ class RPCView(BaseView):
async for msg in ws:
if msg.type == web.WSMsgType.TEXT:
try:
async with Profiler():
await rpc(msg.json())
except Exception as ex:
print(ex, flush=True)
await self.services.socket.delete(ws)
break
elif msg.type == web.WSMsgType.ERROR: