profile/cprofile、gprof2dot和dot的包装器,提供了一个简单的上下文管理器,用于分析python代码的部分并生成分析结果的可视化图形。

bprofile的Python项目详细描述


Chris Billington,2015年2月28日

bprofilecprofilegprof2dotgraphviz的包装, 提供一个简单的上下文管理器来分析python的各个部分 编写并生成分析结果的可视化图形。它工作在 Windows和Unix。

View on PyPI |Get the source from BitBucket |Read the docs at readthedocs

安装

要安装bprofile,请运行:

$ pip install bprofile

或从源安装:

$ python setup.py install
注意:
bprofile需要安装graphviz。bprofile查找 graphviz安装文件夹位于C:\Program FilesC:\Program Files (x86)在windows上,对于graphviz UNIX上PATH中的可执行文件。

简介

每次我需要分析一些python代码时,我都会经历同样的过程 步骤:查找cprofile的文档,然后阅读 gprof2dotgraphviz。然后我发现我想要的代码 profile是一个gui回调或其他东西,我不想分析 整个程序因为它大部分时间都不做任何事情。

cprofile当然有这个功能,我看了一眼, 和思想:^ {EM1}$这应该是一个上下文管理器,当它退出时,它 应该自动调用gprof2dot和graphviz,这样我就不必 记住他们的命令行参数,这样我就不会不小心 将.png打印到标准输出,并且必须听所有ascii 哔声字符。

BProfile提供此功能。

示例用法

# example.py

import os
import time
import pylab as pl
from bprofile import BProfile

def do_some_stuff():
    for i in range(100):
        time.sleep(.01)

def do_some_stuff_that_wont_be_profiled():
    os.system('ping -c 5 google.com')

def do_some_more_stuff(n):
    x = pl.rand(100000)
    for i in range(100):
        time.sleep(.01)
        x = pl.fft(x)


profiler = BProfile('example.png')

with profiler:
    do_some_stuff()

do_some_stuff_that_wont_be_profiled()

with profiler:
    do_some_more_stuff(5)

上面的输出如下图像example.png在当前 工作目录:

https://bitbucket.org/cbillington/bprofile/raw/default/doc/example.png

有关用法的详细信息,请参见BProfile

类引用

class bprofile.bprofile(输出路径,阈值=2.5, 报告间隔=5,启用=真)

A profiling context manager.

A context manager that after it exits, outputs a .png file of a graph made via cProfile, gprof2dot and graphviz. The context manager can be used multiple times, and if used repeatedly, regularly updates its output to include cumulative results.

An instance can also be used as a decorator, it will simply wrap calls to the decorated method in the profiling context.

Parameters:
  • output_path (str) – The name of the .png report file you would like to output. ‘.png’ will be appended if not present.
  • threshold_percent (int or float) – Nodes in which execution spends less than this percentage of the total profiled execution time will not be included in the output.
  • report_interval (int or float) – The minimum time, in seconds, in between output file generation. If the context manager exits and it has not been at least this long since the last output was generated, output generation will be delayed until it has been. More profiling can run in the meantime. This is to decrease overhead on your program, (even though this overhead will only be incurred when no code is being profiled), while allowing you to have ongoing results of the profiling while your code is still running. If you only use the context manager once, then this argument has no effect. If you set it to zero, output will be produced after every exit of the context.
  • enabled (bool) – Whether the profiler is enabled or not. Equivalent to calling ^{tt7}$ with this argument after instantiation. Useful for enabling and disabling profiling with a global flag when you do not have easy access to the instance - for example when using as a decorator.

-[ Notes ]-

The profiler will return immediately after the context manager, and will generate its .png report in a separate thread. If the same context manager is used multiple times output will be generated at most every ^{tt8}$ seconds (default: 5). The delay is to allow blocks to execute many times in between reports, rather than slowing your program down with generating graphs all the time. This means that if your profile block is running rapidly and repeatedly, a new report will be produced every ^{tt8}$ seconds.

Pending reports will be generated at interpreter shutdown.

Note that even if ^{tt8}$ is short, reporting will not interfere with the profiling results themselves, as a lock is acquired that will prevent profiled code from running at the same time as the report generation code. So the overhead produced by report generation does not affect the results of profiling - this overhead will only affect portions of your code that are not being profiled.

The lock is shared between instances, and so you can freely instantiate many ^{tt4}$ instances to profile different parts of your code. Instances with the same ^{tt12}$ will share an underlying cProfile profiler, and so their reports will be combined. Profile objects are thread safe, so a single instance can be shared as well anywhere in your program.

Warning:
Since only one profiler can be running at a time, two profiled pieces of code in different threads waiting on each other in any way will deadlock.

__call__(function)

Returns a wrapped version of ^{tt13}$ with profiling. Intended for use as a decorator.

do_report()

Collect statistics and output a .png file of the profiling report.

-[ Notes ]-

This occurs automatically at a rate of ^{tt8}$, but one can call this method to report results sooner. The report will include results from all ^{tt4}$ instances that have the same ^{tt12}$ and no more automatic reports (if further profiling is done) will be produced until after the minimum ^{tt8}$ of those instances.

This method can be called at any time and is threadsafe. It is not advisable to call it during profiling however as this will incur overhead that will affect the profiling results. Only automatic reports are guaranteed to be generated when no profiling is taking place.

set_enabled(enabled)

Set whether profiling is enabled.

if enabled==True, all methods work as normal. Otherwise ^{tt18}$, ^{tt19}$, and ^{tt20}$ become dummy methods that do nothing. This is useful for having a global variable to turn profiling on or off, based on whether one is debugging or not, or to enable or disable profiling of different parts of code selectively.

If profiling is running when this method is called to disable it, the profiling will be stopped.

start()

Begin profiling.

stop()

Stop profiling.

Stop profiling and outptut a profiling report, if at least ^{tt8}$ has elapsed since the last report. Otherwise output the report after a delay.

Does not preclude starting profiling again at a later time. Results are cumulative.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java FloatingAction按钮与RecyclerView中的CardView重叠   java如何计算CardLayout中的卡数   从远程系统上传MySQL数据库并访问Java应用程序   java调用堆栈如何处理带或不带返回类型的递归?   Springboot中的java组计数聚集   java如何在javafx textarea中使用richtextfx   获取与Mockito相关的错误时出现Java问题   java如何将JaxRS响应转换为Wiremock响应   Hadoop集群java。net ConnectionException:连接被拒绝错误   java如何加载文件私有文件类型是pem   java在元空间中的提升和加载的类   如何将系统属性传递给从HTML启动的Java小程序   java如何从网页中获取值并在主类中使用它?安卓应用   java在春天,advisor和aspect之间有什么区别?   java如何检测文件是否已重命名?   java消息驱动Bean何时使用