vtemian / django-server-timing

Django middleware and utils that add `Server-Timing` header
GNU General Public License v3.0
43 stars 4 forks source link

django-server-timing

Django middleware that exposed collected metrics into HTTP Server Timing header.

This headers is used by browser to display several metrics into the Timing tab of the Network interface. Right now, this header is not supported properlly by all browser. It works pretty good on Chrome 65+ and on Firefox there is a bug report.

It doesn't effect unsupported browser.

This middleware will send the entire header value since not all browsers supports sending it via HTTP Trailer.

Install

It works on Python >= 2.7 and Django >= 1.8

Install easly via pip

pip install django-server-timing

And configure MIDDLEWARES

MIDDLEWARE += ['server_timing.middleware.ServerTiming']

Usage

import time                                                                        

from django.http import HttpResponse                                               

from server_timing.middleware import TimedService, timed, timed_wrapper            

@timed_wrapper('index', 'Index View')                                              
def index(request):                                                                
    home_service = TimedService('first', 'First service')                          
    home_service.start()                                                           

    time.sleep(0.3)                                                                

    home_service.end()                                                             

    with timed('second', 'Second service'):                                        
        time.sleep(0.5)                                                            

    return HttpResponse('This page shows a list of most recent posts.')

Server Timing Example