2009-12-28 76 views
2

我开发了一个使用Django的应用程序,一切正常,但我不知道幕后发生了什么。我想知道:什么是在Django中剖析视图的最佳方式?

  • 为每个请求命中数据库多少次?
  • 每个查询的执行时间是多少?
  • 呈现模板需要多长时间?
  • 定期分析信息(ncalls,每个功能的总时间)。

是否有一个中间件来处理这个,我可以安装?哪些是描述我的观点的最佳实践?

感谢

回答

0
{% 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 %} 

Django Snippet: Template Query Debug

+0

非常有趣,谢谢!我会尝试。 – jbochi 2009-12-28 17:33:31

2

满足您的所有需求,有分析之外的一个项目,是出色的django debug toolbar

对于标准分析,您需要使用repoze.profile(这意味着您必须运行带有WSGI接口的Django,例如mod_wsgi)。

如果你是硬核,并需要调试内存泄漏使用dozer(也WSGI组件)。

相关问题