带有日志支持的简单基于上下文的性能分析
项目描述
easyprofile
— Python的简单基于上下文的性能分析
easyprofile
包提供了在本地上下文中设置Python的sys.setprofile()
的功能,以及一系列辅助工具来自动化性能分析和日志记录。
安装
使用pip按常规方式安装
pip install easyprofile
用法
替换本地sys.setprofile()
如果您希望在本地上下文中安装一个原本会传递给sys.setprofile()
的性能函数,请使用easyprofile.profile
上下文管理器
>>> import easyprofile
>>>
>>> # profile function of the sys.setprofile() form
>>> def myfunc(frame, event, arg):
... print('profile:', event)
...
>>> # a test function to be profiled
>>> def profile_this(n):
... # this inner function call will not show in the profile
... return sum(range(n))
...
>>> # replace the profile function locally
>>> with easyprofile.profile(myfunc):
... # only this call will be profiled
... profile_this(100_000_000)
...
profile: call
profile: return
4999999950000000
忽略调用
如果您希望在本地忽略一些调用,可以使用easyprofile.ignore
上下文管理器
>>> with easyprofile.profile(myfunc):
... # this call will be profiled
... profile_this(100_000_000)
...
... with easyprofile.ignore:
... # this call will be ignored
... profile_this(100)
...
profile: call
profile: return
4999999950000000
4950
上下文管理器通过暂时将sys.setprofile()
设置为None
来实现,因此与任何性能分析器都兼容。
请注意,easyprofile.ignore
不是一个可调用的对象!
标记函数为忽略
可以使用@easyprofile.ignored
装饰器将函数标记为始终忽略
>>> # this function is ignored by easyprofile
>>> @easyprofile.ignored
... def ignored_func():
... return 42
...
>>> with easyprofile.profile(myfunc):
... ignored_func()
...
42
请注意,与easyprofile.ignore
上下文管理器不同,装饰器只与easyprofile
一起使用。
基于类的性能分析器
为了便于创建自定义性能分析器,easyprofile.BaseProfile
类提供了一个基于方法的事件处理接口。当遇到事件<event>
时,会调用名称为_<event>
(即下划线—事件名称)的方法,带有签名frame, arg
。
>>> class MyProfile(easyprofile.BaseProfile):
... def __init__(self, arg, *, kwarg):
... print(f'MyProfile: init, {arg=}, {kwarg=}')
...
... def _attach(self, frame, arg):
... print('MyProfile: attached')
...
... def _detach(self, frame, arg):
... print('MyProfile: detached')
...
... def _call(self, frame, arg):
... print('MyProfile: function called')
...
... def _return(self, frame, arg):
... print('MyProfile: function returned')
...
... # profile C extension calls using the same methods
... _c_call = _call
... _c_return = _return
...
基于类的性能分析器可以通过其profile()
类方法轻松调用,该方法将其参数传递给构造函数
>>> # use the MyProfile class for local profiling
>>> with MyProfile.profile('hello', kwarg='world'):
... profile_this(100_000_000)
...
MyProfile: init, arg='hello', kwarg='world'
MyProfile: function called
MyProfile: function returned
4999999950000000
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码发行版
easyprofile-0.1.5.tar.gz (4.0 kB 查看哈希值)
构建发行版
easyprofile-0.1.5-py3-none-any.whl (4.5 kB 查看哈希值)
关闭
easyprofile-0.1.5.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d3824b1d4a5b654671aeeef8f26934e765ff3995f81a4b38e405f1b86c6ad146 |
|
MD5 | d411cc95b5c944f99b2444dd53410528 |
|
BLAKE2b-256 | 5a40834fae685adb225401eb3fe03148519907cf67221b4ed32b2470757b9af0 |
关闭
easyprofile-0.1.5-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b58e0b8b1c72d93fa33d71b69a1de4a9e18d9b219c0c8fd23d87dbdc64770b45 |
|
MD5 | 241fc503ce67b1e71b9f286088269e30 |
|
BLAKE2b-256 | 037202240174a0cb568c825e0e7235bfb580df943db3a484808b8beaad848000 |