一个小型的Django和IPython兼容的应用程序,用于基准测试数据库和IO繁重的工作。
flame-analyzer的Python项目详细描述
这个包是我们在Publons调试环境中经常使用的部分。 它有助于基准测试和解释代码片段中的低效 我们对不同服务响应时间的依赖性。
这个包支持四种上下文管理器
- 火焰
- 在线火焰
- 琼格菲火焰
- 琼戈因林火焰
它们都服务于同一个用例,向您的机器输出火焰图 你要深入研究并调试你的代码。FileFlame/DjangoFileFlame保存 图形到SVG供您共享,而InlineFlame/djanginlineflame将 在IPython浏览器中渲染它。
示例
将Flame图形保存到SVG可以通过以下基准测试完成
fromflame_analyzerimportFileFlamewithFileFlame('./file_flame_test.svg'):# Some expensive piece of code.[len(u.email)foruinUser.objects.all()]
或者直接转到IPython笔记本
^{pr2}$也可以通过添加宽度kwarg来配置宽度
withFileFlame('./file_flame_test.svg',flame_width=1200,options={'title':'This is my test title'}):# some expensive piece of code[len(u.email)foruinUser.objects.all()]
扩展
默认情况下,IPython和Django都是可选的导入,这意味着您可以安装 并在终端中使用它来调试你的应用程序代码 安装。可以添加对其他数据库框架的支持,或者如果 希望通过创建自己的钩子钩住上下文enter/exit方法 例如,在输出火焰中添加你想要的类型
fromflame_analyzerimportInlineFlameclassCustomHook:""" Append the time taken to execute to the flame graphs title. """defbefore(self):self.called_before='< Called before code execution >'defafter(self):self.called_after='< Called after code execution >'defmodify_flame_options(self,flame_options):title=flame_options.get('title','')flame_options['title']=self.called_before+' --- '+self.called_afterreturnflame_optionsclassCustomInlineFlame(InlineFlame):hook_classes=(CustomHook,)withCustomInlineFlame(flame_width=500):total_email_length=0foruinUser.objects.all():total_email_length+=len(u.email)print(total_email_length)
输出IPython查看的图形
- 项目
标签: