def profile_request(request: HttpRequest) -> HttpResponse: def get_response(request: HttpRequest) -> HttpResponse: return prof.runcall(get_messages_backend, request, request.user, apply_markdown=True) prof = cProfile.Profile() with tempfile.NamedTemporaryFile(prefix="profile.data.", delete=False) as stats_file: response = LogRequests(get_response)(request) prof.dump_stats(stats_file.name) logging.info("Profiling data written to %s", stats_file.name) return response
import cProfile import logging import tempfile from typing import Any, Dict from django.core.management.base import CommandParser from django.http import HttpRequest, HttpResponse from zerver.lib.management import ZulipBaseCommand from zerver.middleware import LogRequests from zerver.models import UserMessage, UserProfile from zerver.views.messages import get_messages_backend request_logger = LogRequests() class MockSession: def __init__(self) -> None: self.modified = False class MockRequest(HttpRequest): def __init__(self, user: UserProfile) -> None: self.user = user self.path = '/' self.method = "POST" self.META = {"REMOTE_ADDR": "127.0.0.1"} anchor = UserMessage.objects.filter( user_profile=self.user).order_by("-message")[200].message_id self.REQUEST = { "anchor": anchor,