跳转到主要内容

用于在Django项目中主要对Python代码进行性能分析的实用程序,但也适用于普通Python代码。它计算SQL查询并测量代码执行时间。它通过标准的Python日志库记录输出,并使用logger profiling。如果您的分析器名称不包含任何空格,例如Profiler('Profiler1'),则django-profiler会将所有输出记录到profiling.Profiler记录器。

项目描述

django-profiler是用于在Django项目中主要对Python代码进行性能分析的实用程序,但也适用于普通Python代码。它计算SQL查询并测量代码执行时间。它通过标准的Python日志库记录输出,并使用logger profiling。如果您的分析器名称不包含任何空格,例如Profiler('Profiler1'),则django-profiler会将所有输出记录到profiling.Profiler记录器。

需求

  • python 2.7+

安装

通过pip安装或将其模块复制到您的项目或PYTHON_PATH中。

配置

django settings.py 常量

PROFILING_LOGGER_NAME
PROFILING_SQL_QUERIES

您可以在django settings.py中定义PROFILING_LOGGER_NAME = 'logger_name'来更改默认的django-profiler记录器名称。

要将SQL查询也记录到分析器记录器中,请将django settings.py模块中的PROFILING_SQL_QUERIES设置为True。

示例

示例1

使用上下文管理器方法。输出将记录到profiling记录器。

from profiling import Profiler
with Profiler('Complex Computation'):
    # code with some complex computations

示例2

使用上下文管理器方法。输出将记录到 profiling.Computation 记录器。

from profiling import Profiler
with Profiler('Computation'):
    # code with some complex computations

示例 3

使用标准方法。输出将记录到 profiling 记录器。

from profiling import Profiler
profiler =  Profiler('Complex Computation')
profiler.start()
# code with some complex computations
profiler.stop()

示例 4

使用标准方法并在构造函数中直接开始。输出将记录到 profiling 记录器。

from profiling import Profiler
profiler =  Profiler('Complex Computation', start=True)
# code with some complex computations
profiler.stop()

示例 5

使用装饰器方法。输出将记录到 profiling.complex_computations 记录器。

from profiling import profile

@profile
def complex_computations():
    #some complex computations

示例 6

使用装饰器方法。输出将记录到 profiling.ComplexClass.complex_computations 记录器。

from profiling import profile

class ComplexClass(object):
    @profile
    def complex_computations():
        #some complex computations

示例 7

使用装饰器方法。输出将记录到 profiling.complex_computations 记录器。profile 执行统计将记录到 profiling.complex_computations 记录器。

from profiling import profile

@profile(stats=True)
def complex_computations():
    #some complex computations

示例 8

使用装饰器方法。输出将记录到 profiling.complex_computations 记录器。profile 执行统计将打印到 sys.stdout。

import sys

from profiling import profile

@profile(stats=True, stats_buffer=sys.stdout)
def complex_computations():
    #some complex computations

示例 9

使用装饰器方法。输出将记录到 profiling.ComplexClass.complex_computations 记录器。profile 统计将记录到 profiling.ComplexClass.complex_computations

from profiling import profile

class ComplexClass(object)
   @profile(stats=True)
   def complex_computations():
       #some complex computations

测试

在环境上测试

  • Xubuntu Linux 11.10 oneiric 64-bit

  • python 2.7.2+

  • python unittest

运行测试

要运行测试,请运行以下命令

$ python test.py
$ python setup.py test

作者

char0n (Vladimír Gorej, CodeScale s.r.o.)
电子邮件: gorej@codescale.net

参考

项目详情


下载文件

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

源分发

django-profiler-2.0.0.tar.gz (6.1 kB 查看散列)

上传日期

由以下组织支持