Reputation: 3373
I want to measure execution time for some queries and add this data to responses, like: {"meta": {"execution_time_in_ms": 500 ...}}
I know how to add fields to tastypie's responses, but I haven't idea how to measure time in it, where I should initialize the timer and where I should stop it. Any ideas?
Upvotes: 1
Views: 114
Reputation: 3373
I found an answer, custom middleware is helpful in that case. It suits not only tastypie, but also DRF, and any another framework.
middleware.py:
from datetime import datetime
class AddMetaMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = None
if hasattr(self, 'process_request'):
response = self.process_request(request)
if not response:
response = self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response
def process_request(self, request):
request._request_start_time = datetime.now()
def process_template_response(self, request, response):
if not hasattr(response, 'data'):
return response
response.data = {
'data': response.data,
'meta': {
'current_time' : datetime.now(),
'benchmark': {
'time': datetime.now() - request._request_start_time,
}
}
}
return response
Upvotes: 0
Reputation: 4360
That'll only work for list endpoints though. My advice is to use a middleware to add X- headers, it's a cleaner, more generalized solution.
Upvotes: 1
Reputation: 1172
I don't know if there is a direct way through settings to do this or not but I can suggest to override get_list method and add this extra field to the meta like the following:
import json
from django.http import HttpResponse
class MyResource(ModelResource):
def get_list(self, request, **kwargs):
start = time.time()
resp = super(MyResource, self).get_list(request, **kwargs)
data = json.loads(resp.content)
data['meta']['execution_time_in_ms'] = time.time() - start
data = json.dumps(data)
return HttpResponse(data, content_type='application/json', status=200)
If you would use this for more than one ModelResource you can make a base class that overrides all the method needed and then inherit from this base class.
Upvotes: 1