Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django debug toolbar: how do I profile a file download?

My Django webapp lets users download text files that are generated on the fly:

response = HttpResponse(my_file_contents)
response['Content-Disposition'] = 'attachment; filename="my file.txt"'
return response

I installed Django Debug Toolbar (0.11.0, since I cannot get 1.0.1 to work), but when I click to make the download, the toolbar doesn't show info about the file that was downloaded, presumably because that is a separate page/request (or maybe because it's a non-HTML file). The downloaded file doesn't contain any debug info either.

How can I profile the performance of this file download?

like image 462
RexE Avatar asked Feb 04 '14 08:02

RexE


2 Answers

An alternative, temporary solution if you are focused on profiling DB queries is to simply not return the file download response, and instead load a template (any valid Django template in your application should work). DDT still logs all the queries and you can see them on the subsequent page. This works because often what you are interested in is the queries that take place in order to build the data that is being prepared for download. The actual process of taking some data already in-hand and returning a response is usually very quick.

So let's say you have a form where this download can be requested. Typically your view would return something like:

# (Do something here to collect data)
response = HttpResponse(export_data, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename=somefile.txt'
return response

Just comment this out and return a regular rendered template instead -- you don't need to display the data if you don't want to. If using a mixin like TemplateView or FormView, this might be simply commenting out the above and then Django will render the template as if the download action hadn't been performed. Or, just render any Django template in your application. Now, open the DDT toolbar -- there's all your queries!

like image 132
timothyashaw Avatar answered Oct 04 '22 15:10

timothyashaw


You are right, this is one of the cases where the Debug Toolbar cannot help you. I would recommend using log files to time your request times. For instance, if you are using Nginx then you can use its syntax for adding extra information to log files. For instance the following line adds the response time for each request:

log_format timed_combined '$remote_addr - $remote_user [$time_local]  '
      '"$request" $status $body_bytes_sent '
      '"$http_referer" "$http_user_agent" '
      '$request_time $upstream_response_time $gzip_ratio';

If you prefer a Django app solution, you can check out django-timelog.

like image 31
arocks Avatar answered Oct 04 '22 16:10

arocks