2017-11-16 00:55:49 +01:00
|
|
|
import cProfile
|
|
|
|
import logging
|
2016-04-07 23:11:21 +02:00
|
|
|
from typing import Any, Dict
|
2013-06-19 20:24:16 +02:00
|
|
|
|
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
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.middleware import LogRequests
|
2017-07-18 02:02:59 +02:00
|
|
|
from zerver.models import UserMessage, UserProfile
|
2017-03-24 07:51:46 +01:00
|
|
|
from zerver.views.messages import get_messages_backend
|
2013-06-19 20:24:16 +02:00
|
|
|
|
|
|
|
request_logger = LogRequests()
|
|
|
|
|
2017-11-05 11:33:09 +01:00
|
|
|
class MockSession:
|
2017-10-27 12:57:54 +02:00
|
|
|
def __init__(self) -> None:
|
2013-06-19 20:24:16 +02:00
|
|
|
self.modified = False
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
class MockRequest(HttpRequest):
|
2017-10-27 12:57:54 +02:00
|
|
|
def __init__(self, user: UserProfile) -> None:
|
2017-07-18 02:02:59 +02:00
|
|
|
self.user = user
|
2013-06-19 20:24:16 +02:00
|
|
|
self.path = '/'
|
|
|
|
self.method = "POST"
|
|
|
|
self.META = {"REMOTE_ADDR": "127.0.0.1"}
|
2017-11-04 14:16:50 +01:00
|
|
|
anchor = UserMessage.objects.filter(user_profile=self.user).order_by("-message")[200].message_id
|
2016-08-13 16:56:15 +02:00
|
|
|
self.REQUEST = {
|
2017-11-04 14:16:50 +01:00
|
|
|
"anchor": anchor,
|
2016-08-13 16:56:15 +02:00
|
|
|
"num_before": 1200,
|
|
|
|
"num_after": 200
|
|
|
|
}
|
2017-05-07 20:14:04 +02:00
|
|
|
self.GET = {} # type: Dict[Any, Any]
|
2013-06-19 20:24:16 +02:00
|
|
|
self.session = MockSession()
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def get_full_path(self) -> str:
|
2013-06-19 20:24:16 +02:00
|
|
|
return self.path
|
|
|
|
|
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()
|
2017-03-24 07:51:46 +01:00
|
|
|
ret = get_messages_backend(request, request.user,
|
|
|
|
apply_markdown=True)
|
2013-06-19 20:24:16 +02:00
|
|
|
prof.disable()
|
|
|
|
prof.dump_stats("/tmp/profile.data")
|
|
|
|
request_logger.process_response(request, ret)
|
|
|
|
logging.info("Profiling data written to /tmp/profile.data")
|
|
|
|
return ret
|
|
|
|
|
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:
|
2017-07-18 02:02:59 +02:00
|
|
|
parser.add_argument("email", metavar="<email>", type=str, help="Email address of the user")
|
|
|
|
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)
|
|
|
|
profile_request(MockRequest(user))
|