UTIL主要用于在Django项目中分析Python代码,但也可以用于普通的Python代码
django-profiler的Python项目详细描述
django profiler主要用于在django项目中分析python代码。 但也可以用于普通的python代码。它将SQL查询计算为一个度量值 代码执行时间。它通过标准记录输出 python日志库并使用loggerprofiling。如果您的探查器名称 不包含任何空格,例如profiler('profiler1')django profiler将 将所有输出记录到profiling.profiler记录器。
要求
- Python2.7+
安装
通过pip安装,或者将此模块复制到项目或python路径中。
配置
django settings.py常量
PROFILING_LOGGER_NAME PROFILING_SQL_QUERIES
可以通过定义 profiling_logger_name='logger_name'在djangosettings.py中。
要将sql查询也记录到profiler logger中,请将profileing_sql_querys设置为true 在djangosettings.py模块中。
示例
示例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
使用decorator方法。输出将记录到profiling.complex_computeslogger。
from profiling import profile @profile def complex_computations(): #some complex computations
示例6
使用decorator方法。输出将记录到profiling.complexclass.complex_computes记录器。
from profiling import profile class ComplexClass(object): @profile def complex_computations(): #some complex computations
示例7
使用decorator方法。输出将记录到profiling.complex_computeslogger。 配置文件执行状态记录到profiling.complex\u computes记录器。
from profiling import profile @profile(stats=True) def complex_computations(): #some complex computations
示例8
使用decorator方法。输出将记录到profiling.complex_computeslogger。 配置文件执行状态将打印到sys.stdout。
import sys from profiling import profile @profile(stats=True, stats_buffer=sys.stdout) def complex_computations(): #some complex computations
示例9
使用decorator方法。输出将记录到profiling.complexclass.complex_computes记录器。 配置文件统计信息将记录到profiling.complexclass.complex\u计算中。
from profiling import profile class ComplexClass(object) @profile(stats=True) def complex_computations(): #some complex computations
测试
在evnironment上测试
- Xubuntu Linux 11.10 OneIRIC 64位
- Python2.7.2+
- python单元测试
运行测试
运行测试运行命令:
$ python test.py $ python setup.py test