2013-06-19 20:24:16 +02:00
|
|
|
from __future__ import absolute_import
|
2016-04-07 23:11:21 +02:00
|
|
|
from typing import Any, Dict
|
2013-06-19 20:24:16 +02:00
|
|
|
|
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import get_user_profile_by_email, UserMessage
|
2015-11-10 07:02:52 +01:00
|
|
|
from zerver.views.messages import get_old_messages_backend
|
2013-06-19 20:24:16 +02:00
|
|
|
import cProfile
|
|
|
|
import logging
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.middleware import LogRequests
|
2013-06-19 20:24:16 +02:00
|
|
|
|
|
|
|
request_logger = LogRequests()
|
|
|
|
|
|
|
|
class MockSession(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.modified = False
|
|
|
|
|
|
|
|
class MockRequest(object):
|
|
|
|
def __init__(self, email):
|
|
|
|
self.user = get_user_profile_by_email(email)
|
|
|
|
self.path = '/'
|
|
|
|
self.method = "POST"
|
|
|
|
self.META = {"REMOTE_ADDR": "127.0.0.1"}
|
|
|
|
self.REQUEST = {"anchor": UserMessage.objects.filter(user_profile=self.user).order_by("-message")[200].message_id,
|
|
|
|
"num_before": 1200,
|
|
|
|
"num_after": 200}
|
2016-01-26 02:19:31 +01:00
|
|
|
self.GET = {} # type: Dict[Any, Any]
|
2013-06-19 20:24:16 +02:00
|
|
|
self.session = MockSession()
|
|
|
|
|
|
|
|
def get_full_path(self):
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
def profile_request(request):
|
|
|
|
request_logger.process_request(request)
|
|
|
|
prof = cProfile.Profile()
|
|
|
|
prof.enable()
|
|
|
|
ret = get_old_messages_backend(request, request.user,
|
|
|
|
apply_markdown=True)
|
|
|
|
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
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--email', action='store'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
profile_request(MockRequest(options["email"]))
|