2017-11-16 00:55:49 +01:00
|
|
|
import cProfile
|
|
|
|
import logging
|
2019-01-15 02:58:03 +01:00
|
|
|
import tempfile
|
2021-08-18 18:42:39 +02:00
|
|
|
from typing import Any
|
2013-06-19 20:24:16 +02:00
|
|
|
|
2021-08-14 16:51:57 +02:00
|
|
|
from django.contrib.sessions.backends.base import SessionBase
|
2017-07-18 02:02:59 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-07-18 02:02:59 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2021-08-14 16:51:57 +02:00
|
|
|
from zerver.lib.request import get_request_notes
|
|
|
|
from zerver.lib.test_helpers import HostRequestMock
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.middleware import LogRequests
|
2021-08-18 18:42:39 +02:00
|
|
|
from zerver.models import UserMessage
|
2020-06-22 23:32:53 +02:00
|
|
|
from zerver.views.message_fetch import get_messages_backend
|
2013-06-19 20:24:16 +02:00
|
|
|
|
|
|
|
request_logger = LogRequests()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-18 18:42:39 +02:00
|
|
|
class MockSession(SessionBase):
|
2017-10-27 12:57:54 +02:00
|
|
|
def __init__(self) -> None:
|
2013-06-19 20:24:16 +02:00
|
|
|
self.modified = False
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def profile_request(request: HttpRequest) -> HttpResponse:
|
2013-06-19 20:24:16 +02:00
|
|
|
request_logger.process_request(request)
|
|
|
|
prof = cProfile.Profile()
|
|
|
|
prof.enable()
|
2021-02-12 08:19:30 +01:00
|
|
|
ret = get_messages_backend(request, request.user, apply_markdown=True)
|
2013-06-19 20:24:16 +02:00
|
|
|
prof.disable()
|
2021-02-12 08:20:45 +01:00
|
|
|
with tempfile.NamedTemporaryFile(prefix="profile.data.", delete=False) as stats_file:
|
2019-01-15 02:58:03 +01:00
|
|
|
prof.dump_stats(stats_file.name)
|
|
|
|
request_logger.process_response(request, ret)
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Profiling data written to %s", stats_file.name)
|
2013-06-19 20:24:16 +02:00
|
|
|
return ret
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-07-18 02:02:59 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2017-10-27 12:57:54 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2020-09-02 21:24:05 +02:00
|
|
|
parser.add_argument("email", metavar="<email>", help="Email address of the user")
|
2017-07-18 02:02:59 +02:00
|
|
|
self.add_realm_args(parser)
|
2013-06-19 20:24:16 +02:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-07-18 02:02:59 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
user = self.get_user(options["email"], realm)
|
2021-08-18 18:42:39 +02:00
|
|
|
anchor = UserMessage.objects.filter(user_profile=user).order_by("-message")[200].message_id
|
|
|
|
mock_request = HostRequestMock(
|
|
|
|
post_data={
|
|
|
|
"anchor": anchor,
|
|
|
|
"num_before": 1200,
|
|
|
|
"num_after": 200,
|
|
|
|
},
|
|
|
|
user_profile=user,
|
|
|
|
meta_data={"REMOTE_ADDR": "127.0.0.1"},
|
|
|
|
path="/",
|
|
|
|
)
|
|
|
|
mock_request.session = MockSession()
|
|
|
|
get_request_notes(mock_request).log_data = None
|
|
|
|
|
|
|
|
profile_request(mock_request)
|