From 9941b408a0a6e1044e5b768c30ca487d12ba85e5 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 8 Aug 2018 13:19:44 -0700 Subject: [PATCH] email_log: Only import Django test client when using it. Importing the Django test client is somewhat expensive, and we only use it within one view function that's not used in production. So there's a significant startup-time performance optimization in doing an import inside the view code. --- zerver/views/email_log.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/zerver/views/email_log.py b/zerver/views/email_log.py index 762e7f8195..1e9abd6e18 100755 --- a/zerver/views/email_log.py +++ b/zerver/views/email_log.py @@ -1,7 +1,6 @@ from django.conf import settings from django.http import HttpRequest, HttpResponse from django.shortcuts import render, redirect -from django.test import Client from django.views.decorators.http import require_GET from django.views.decorators.csrf import csrf_exempt @@ -19,7 +18,6 @@ import os from typing import List, Dict, Any, Optional import datetime ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../') -client = Client() def email_page(request: HttpRequest) -> HttpResponse: if request.method == 'POST': @@ -43,6 +41,11 @@ def clear_emails(request: HttpRequest) -> HttpResponse: @require_GET def generate_all_emails(request: HttpRequest) -> HttpResponse: + # We import the Django test client inside the view function, + # because it isn't needed in production elsewhere, and not + # importing it saves ~50ms of unnecessary manage.py startup time. + from django.test import Client + client = Client() # write fake data for all variables registered_email = "hamlet@zulip.com"