Extract middleware.is_slow_query() and add tests.

(imported from commit 60902244a420800f558fdf2f1c38b4ed736c1286)
This commit is contained in:
Steve Howell 2013-12-26 09:01:46 -05:00
parent c906db1c21
commit e0a1841b1c
2 changed files with 19 additions and 5 deletions

View File

@ -65,6 +65,13 @@ def format_timedelta(timedelta):
return "%.1fs" % (timedelta)
return "%.0fms" % (timedelta_ms(timedelta),)
def is_slow_query(time_delta, path):
return time_delta >= 1 \
and path not in ["/activity", "/json/report_error",
"/api/v1/deployments/report_error"] \
and not path.startswith("/realm_activity/") \
and not path.startswith("/user_activity/")
def write_log_line(log_data, path, method, remote_ip, email, client_name,
status_code=200, error_content=''):
# For statsd timer name
@ -160,11 +167,7 @@ def write_log_line(log_data, path, method, remote_ip, email, client_name,
logger_timing, extra_request_data, logger_client)
logger.info(logger_line)
if (time_delta >= 1
and path not in ["/activity", "/json/report_error",
"/api/v1/deployments/report_error"]
and not path.startswith("/realm_activity/")
and not path.startswith("/user_activity/")):
if (is_slow_query(time_delta, path)):
queue_json_publish("slow_queries", "%s (%s)" % (logger_timing, email), lambda e: None)
if settings.PROFILE_ALL_REQUESTS:

View File

@ -34,6 +34,7 @@ from zerver.lib.digest import send_digest_email
from zerver.forms import not_mit_mailing_list
from zerver.lib.validator import check_string, check_list, check_dict, \
check_bool, check_int
from zerver.middleware import is_slow_query
from zerver.worker import queue_processors
@ -201,6 +202,16 @@ def is_known_slow_test(test_method):
API_KEYS = {}
class SlowQueryTest(TestCase):
def test_is_slow_query(self):
self.assertFalse(is_slow_query(0.9, '/some/random/url'))
self.assertTrue(is_slow_query(2, '/some/random/url'))
self.assertFalse(is_slow_query(2, '/activity'))
self.assertFalse(is_slow_query(2, '/json/report_error'))
self.assertFalse(is_slow_query(2, '/api/v1/deployments/report_error'))
self.assertFalse(is_slow_query(2, '/realm_activity/whatever'))
self.assertFalse(is_slow_query(2, '/user_activity/whatever'))
class DecoratorTestCase(TestCase):
def test_REQ_converter(self):