用于在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
参考
项目详情
django-profiler-2.0.0.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | ee5e5619521f1ce896753716f0eda947c402850450577a3a1e9b7204d9cbcf20 |
|
MD5 | 1704a167cb0dadb1f77652d923d09032 |
|
BLAKE2b-256 | 6bc16a85b662c6812b3d51bd70d0624159ca80dd7085415e5080996c328833d9 |