Skip to content

profiling

django_spire.core.middleware.profiling

IGNORE_PATH = ['/__', '/__debug__/', '/__debug__/history_sidebar/', '/__reload__/events/', '/_admin_profiling/', '/admin/', '/admin/autocomplete/', '/admin/jsi18n/', '/api-auth/', '/api-auth/login/', '/api-auth/logout/', '/api/schema/', '/browsable-api/', '/debug/', '/debug-toolbar/', '/django_glue/', '/docs/', '/favicon.ico', '/media/', '/openapi/', '/redoc/', '/robots.txt', '/schema/', '/sitemap.xml', '/static/', '/swagger/', 'django_glue'] module-attribute

IGNORE_EXTENSION = ['.css', '.eot', '.gif', '.ico', '.jpeg', '.jpg', '.js', '.map', '.pdf', '.png', '.svg', '.ttf', '.txt', '.woff', '.woff2', '.zip'] module-attribute

ProfilingMiddleware

Bases: MiddlewareMixin

Source code in django_spire/core/middleware/profiling.py
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]) -> None:
    super().__init__(get_response)

    if Profiler is None:
        message = 'pyinstrument is required for profiling.'
        raise ImportError(message)

    configuration = {
        'PROFILING_DIR': os.getenv('PROFILING_DIR', '.profile'),
        'PROFILING_ENABLED': os.getenv('PROFILING_ENABLED', 'False') == 'True',
    }

    directory = configuration.get('PROFILING_DIR', '.profile')

    if isinstance(directory, str):
        if not Path(directory).is_absolute():
            current = Path.cwd()
            base = getattr(settings, 'BASE_DIR', current)
            directory = Path(base) / directory
        else:
            directory = Path(directory)

    self.directory = Path(directory)
    self.directory.mkdir(exist_ok=True)

    self.enabled = configuration.get('PROFILING_ENABLED', False)
    self.profile_threshold = configuration.get('PROFILE_THRESHOLD', 0)

    self.count = 0
    self.lock = threading.Lock()

directory = Path(directory) instance-attribute

enabled = configuration.get('PROFILING_ENABLED', False) instance-attribute

profile_threshold = configuration.get('PROFILE_THRESHOLD', 0) instance-attribute

count = 0 instance-attribute

lock = threading.Lock() instance-attribute

process_view

Source code in django_spire/core/middleware/profiling.py
def process_view(
    self,
    request: HttpRequest,
    view_func: Callable[..., Any],
    args: tuple[Any, ...],
    kwargs: dict[str, Any]
) -> Any:
    if not settings.DEBUG:
        return None

    if not self.enabled:
        return None

    if self._should_skip_profiling(request):
        return None

    with self.lock:
        self.count = self.count + 1
        request._profiling_id = self.count

    profiler = Profiler(interval=0.001)
    start_time = time.time()
    profiler.start()

    try:
        response = view_func(request, *args, **kwargs)

        if hasattr(response, 'render'):
            response.render()
    except Exception:
        profiler.stop()
        duration_ms = (time.time() - start_time) * 1000
        self._save_profile(profiler, request, duration_ms)

        raise
    else:
        profiler.stop()
        duration_ms = (time.time() - start_time) * 1000

        if duration_ms >= self.profile_threshold:
            self._save_profile(profiler, request, duration_ms)

        return response