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
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
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):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: () -> None
|
2013-06-19 20:24:16 +02:00
|
|
|
self.modified = False
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
class MockRequest(HttpRequest):
|
2013-06-19 20:24:16 +02:00
|
|
|
def __init__(self, email):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (str) -> None
|
2013-06-19 20:24:16 +02:00
|
|
|
self.user = get_user_profile_by_email(email)
|
|
|
|
self.path = '/'
|
|
|
|
self.method = "POST"
|
|
|
|
self.META = {"REMOTE_ADDR": "127.0.0.1"}
|
2016-08-13 16:56:15 +02:00
|
|
|
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):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: () -> str
|
2013-06-19 20:24:16 +02:00
|
|
|
return self.path
|
|
|
|
|
|
|
|
def profile_request(request):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-06-19 20:24:16 +02:00
|
|
|
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):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-06-19 20:24:16 +02:00
|
|
|
profile_request(MockRequest(options["email"]))
|