Skip to content

Commit 61499fd

Browse files
authored
Merge pull request #19 from tiwariav/master
Middlewares to show django-silk profiling data for all requests
2 parents faf059a + f660a35 commit 61499fd

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

requirements.dev.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Django==3.2.5
1+
Django==3.2.6
22
attrs==21.2.0
33
pluggy==0.13.1
44
py==1.10.0
@@ -7,3 +7,5 @@ pytest-cov==2.12.1
77
pytest-django==4.4.0
88
pytz==2021.1
99
six==1.16.0
10+
# replace with pip when v4.1.1 is available for django-silk on pip
11+
git+https://github.com/jazzband/django-silk.git#egg=django-silk # https://github.com/jazzband/django-silk

server_timing/middleware.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ def add_service(service):
2222

2323

2424
class TimedService(object):
25-
def __init__(self, name, description=''):
25+
def __init__(self, name, description="", duration=None):
2626
self.name = name
2727
self.description = description
28+
self._duration = duration
2829
self._start = self._end = None
2930

31+
if self._duration:
32+
add_service(self)
33+
3034
def start(self):
3135
self._start = time.time()
3236
add_service(self)
@@ -36,6 +40,8 @@ def end(self):
3640

3741
@property
3842
def duration(self):
43+
if not self._start and self._duration:
44+
return self._duration
3945
return int(((self._end or time.time()) - self._start) * 1000)
4046

4147

server_timing/silk.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from .middleware import TimedService, timed
2+
3+
class ServerTimingSilkMiddleware:
4+
"""
5+
This middleware will measure the response time for each request,
6+
and also the time taken for sql queries, if silk is enabled.
7+
"""
8+
def __init__(self, get_response):
9+
self.get_response = get_response
10+
11+
def _get_query_timing(self, request):
12+
try:
13+
from silk.collector import DataCollector
14+
from silk.profiling.profiler import silk_meta_profiler, silk_profile
15+
except ModuleNotFoundError:
16+
return
17+
if not hasattr(request, 'silk_is_intercepted'):
18+
return
19+
with silk_meta_profiler():
20+
collector = DataCollector()
21+
silk_request = collector.request
22+
if silk_request:
23+
TimedService(
24+
name="sql",
25+
description="DB Queries",
26+
duration=sum([s.time_taken for s in silk_request.queries.all()]),
27+
)
28+
29+
def __call__(self, request):
30+
with timed("request-response", "Request Response"):
31+
response = self.get_response(request)
32+
self._get_query_timing(request)
33+
return response
34+
35+
36+
class ServerTimingSilkViewOnlyMiddleware:
37+
"""
38+
This middleware would be put as the last middleware, to measure the
39+
response time of only the underlying view.
40+
"""
41+
def __init__(self, get_response):
42+
self.get_response = get_response
43+
44+
def __call__(self, request):
45+
with timed("request-view", "Request View"):
46+
response = self.get_response(request)
47+
return response

0 commit comments

Comments
 (0)