From d69e8b50cd028dd00eaee68c71fa48bb85abfe19 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 26 Oct 2018 08:20:00 +0100 Subject: [PATCH] Only initialised service model once per request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `_get_current_service` is a function which gets called every time `current_service` is referenced in a view method or Jinja template. Because the service model was getting initialised inside this function it was being reconstructed many times in one request. On the service settings page, for example, it was getting initialised 43 times, adding about 200ms to the response time. This commit moves its initialisation to the point where we’re getting the data from the API, which only happens once per request. --- app/__init__.py | 6 ++++-- tests/app/notify_client/test_notify_admin_api_client.py | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 7e0f9229b..9e749d3d7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -99,7 +99,7 @@ platform_stats_api_client = PlatformStatsAPIClient() # The current service attached to the request stack. def _get_current_service(): - return Service(_lookup_req_object('service')) + return _lookup_req_object('service') current_service = LocalProxy(_get_current_service) @@ -465,7 +465,9 @@ def load_service_before_request(): if service_id: try: - _request_ctx_stack.top.service = service_api_client.get_service(service_id)['data'] + _request_ctx_stack.top.service = Service( + service_api_client.get_service(service_id)['data'] + ) except HTTPError as exc: # if service id isn't real, then 404 rather than 500ing later because we expect service to be set if exc.status_code == 404: diff --git a/tests/app/notify_client/test_notify_admin_api_client.py b/tests/app/notify_client/test_notify_admin_api_client.py index 4082647b0..3f127abad 100644 --- a/tests/app/notify_client/test_notify_admin_api_client.py +++ b/tests/app/notify_client/test_notify_admin_api_client.py @@ -5,6 +5,7 @@ import pytest import werkzeug from app.notify_client import NotifyAdminAPIClient +from app.notify_client.models import Service from tests import service_json from tests.conftest import api_user_active, platform_admin_user, set_config @@ -29,7 +30,7 @@ def test_active_service_can_be_modified(app_, method, user, service): with app_.test_request_context() as request_context, app_.test_client() as client: client.login(user) - request_context.service = service + request_context.service = Service(service) with patch.object(api_client, 'request') as request: ret = getattr(api_client, method)('url', 'data') @@ -48,7 +49,7 @@ def test_inactive_service_cannot_be_modified_by_normal_user(app_, api_user_activ with app_.test_request_context() as request_context, app_.test_client() as client: client.login(api_user_active) - request_context.service = service_json(active=False) + request_context.service = Service(service_json(active=False)) with patch.object(api_client, 'request') as request: with pytest.raises(werkzeug.exceptions.Forbidden): @@ -67,7 +68,7 @@ def test_inactive_service_can_be_modified_by_platform_admin(app_, platform_admin with app_.test_request_context() as request_context, app_.test_client() as client: client.login(platform_admin_user) - request_context.service = service_json(active=False) + request_context.service = Service(service_json(active=False)) with patch.object(api_client, 'request') as request: ret = getattr(api_client, method)('url', 'data')