简体   繁体   English

Django 调试工具栏:如何分析文件下载?

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

My Django webapp lets users download text files that are generated on the fly:我的 Django webapp 允许用户下载动态生成的文本文件:

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).我安装了 Django 调试工具栏(0.11.0,因为我无法让 1.0.1 工作),但是当我点击进行下载时,工具栏没有显示有关已下载文件的信息,大概是因为这是一个单独的页面/请求(或者可能因为它是一个非 HTML 文件)。 The downloaded file doesn't contain any debug info either.下载的文件也不包含任何调试信息。

How can I profile the performance of this file download?如何分析此文件下载的性能?

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).如果您专注于分析数据库查询,另一种临时解决方案是简单地不返回文件下载响应,而是加载模板(应用程序中的任何有效 Django 模板都应该工作)。 DDT still logs all the queries and you can see them on the subsequent page. DDT 仍会记录所有查询,您可以在后续页面上看到它们。 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.如果使用像 TemplateView 或 FormView 这样的 mixin,这可能只是简单地注释掉上面的内容,然后 Django 将呈现模板,就好像没有执行下载操作一样。 Or, just render any Django template in your application.或者,只需在您的应用程序中呈现任何 Django 模板。 Now, open the DDT toolbar -- there's all your queries!现在,打开 DDT 工具栏——这里有你所有的疑问!

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 .例如,如果您使用 Nginx,那么您可以使用其语法向日志文件添加额外信息 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 .如果您更喜欢 Django 应用程序解决方案,您可以查看django-timelog

You can use django-debug-toolbar-force .您可以使用django-debug-toolbar-force

It allows you to force template rendering for any url by appending ?debug-toolbar .它允许您通过附加?debug-toolbar来强制为任何 url 呈现模板。

This allows you to access the toolbar and all of its instrumentation during development for any view without any code changes.这使您可以在开发期间针对任何视图访问工具栏及其所有工具,而无需更改任何代码。

Caveat: I am not sure how actively maintained it is as of now, the last commit is from 2020.警告:我不确定到目前为止它的维护情况如何,最后一次提交是从 2020 年开始的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM