import time class TimingMiddleware: """Record server-side request time, mirroring the original app. Sets ``request.start_time`` before the view runs so views can report elapsed time, and exposes the total duration as a response header. """ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): started = time.perf_counter() request.start_time = started response = self.get_response(request) response["X-Server-Time-Ms"] = f"{(time.perf_counter() - started) * 1000:.1f}" return response