[英]What is the best way to profile a view in Django?
我使用Django開發了一個應用程序,一切正常,但我不知道幕后發生了什么。 我想知道:
是否有可以安裝的中間件來處理這個問題? 哪些是描述我的觀點的最佳做法?
謝謝
三個字: Django Debug Toolbar
除了性能分析之外,滿足所有要求的一個項目是優秀的django調試工具欄 。
對於標准分析,您需要使用repoze.profile (這意味着您必須使用WSGI接口運行Django,例如mod_wsgi)。
如果您是硬核並且需要調試內存泄漏,請使用dozer (也是WSGI組件)。
{% if debug %}
<div id="debug">
<h2>Queries</h2>
<p>
{{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }}
{% ifnotequal sql_queries|length 0 %}
(<span style="cursor: pointer;" onclick="var s=document.getElementById('debugQueryTable').style;s.display=s.display=='none'?'':'none';this.innerHTML=this.innerHTML=='Show'?'Hide':'Show';">Show</span>)
{% endifnotequal %}
</p>
<table id="debugQueryTable" style="display: none;">
<col width="1"></col>
<col></col>
<col width="1"></col>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">SQL</th>
<th scope="col">Time</th>
</tr>
</thead>
<tbody>
{% for query in sql_queries %}<tr class="{% cycle odd,even %}">
<td>{{ forloop.counter }}</td>
<td>{{ query.sql|escape }}</td>
<td>{{ query.time }}</td>
</tr>{% endfor %}
</tbody>
</table>
</div>
{% endif %}
對於2019年到達的人來說, django-debug-toolbar
仍然很棒,但就像單挑一樣,大多數模板分析窗格在現代Django版本(2.0+)中被打破。
django-silk
另一個不錯的選擇是django-silk
,它具有一些漂亮的計時配置文件可視化和圖形功能,還有django-live-profiler
帶有Django v2.0 +的工作叉 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.