Skip to main content

一个小型 Django 和 IPython 兼容应用程序,用于基准测试数据库和 IO 繁重的工作。

项目描述

https://travis-ci.com/publons/flame-analyzer.svg?branch=master

这个包是我们在 Publons 调试环境中经常使用的一部分。它有助于基准测试和解释代码片段中的低效率以及我们对不同服务响应时间的依赖。

我们通过这个包支持四种上下文管理器

  • 文件火焰

  • 内联火焰

  • DjangoFileFlame

  • DjangoInlineFlame

它们都服务于相同的用例,将火焰图输出到您的机器,供您深入研究和调试代码。FileFlame / DjangoFileFlame将图形保存为 SVG 以供您共享,而InlineFlame / DjangoInlineFlame将在您的 IPython 浏览器中呈现它。

例子

可以通过以下基准测试将火焰图保存到 SVG

from flame_analyzer import FileFlame


with FileFlame('./file_flame_test.svg'):
    # Some expensive piece of code.
    [len(u.email) for u in  User.objects.all()]

或者直接到 IPython notebook

from flame_analyzer import InlineFlame


with InlineFlame():
    # Some expensive piece of code.
    [len(u.email) for u in  User.objects.all()]

您还可以通过添加 width kwarg 来选择配置宽度

with FileFlame(
    './file_flame_test.svg', flame_width=1200,
    options={'title': 'This is my test title'}
):
    # some expensive piece of code
    [len(u.email) for u in  User.objects.all()]

扩展

默认情况下,IPython 和 Django 都是可选导入,这意味着您可以安装此库并在终端中使用它来调试您的应用程序代码,而无需安装它们。可以为其他数据库框架添加支持,或者如果您想通过创建自己的钩子并添加到您想要的输出火焰类型来挂钩上下文进入/退出方法

from flame_analyzer import InlineFlame

class CustomHook:
    """
    Append the time taken to execute to the flame graphs title.
    """
    def before(self):
        self.called_before = '< Called before code execution >'

    def after(self):
        self.called_after = '< Called after code execution >'

    def modify_flame_options(self, flame_options):
        title = flame_options.get('title', '')
        flame_options['title'] = self.called_before + ' --- ' + self.called_after
        return flame_options


class CustomInlineFlame(InlineFlame):
    hook_classes = (CustomHook,)

with CustomInlineFlame(flame_width=500):
    total_email_length = 0
    for u in User.objects.all():
        total_email_length += len(u.email)
    print(total_email_length)

输出 IPython 查看的 Graph

https://user-images.githubusercontent.com/6813352/68050764-c1107800-fd4a-11e9-94a2-8ab0bc564617.png
归功于以下项目:

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

flame-analyzer-0.1.5.tar.gz (5.9 kB 查看哈希)

已上传 source

内置分布

flame_analyzer-0.1.5-py3-none-any.whl (19.5 kB 查看哈希

已上传 py3