Easy context-based profiling with logging support
Project description
easyprofile
— Easy context-based profiling for Python
The easyprofile
package provides facilities for setting Python's
sys.setprofile()
locally within a context, together with a number of helpers
to automate profiling and logging.
Installation
Install as usual with pip:
pip install easyprofile
Usage
Replacing sys.setprofile()
locally
If you wish to install a profile function that you would otherwise pass to
sys.setprofile()
within a local context, use the easyprofile.profile
context manager:
>>> 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
Ignoring calls
If you wish to ignore some calls locally, you can use the easyprofile.ignore
context manager:
>>> 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
The context manager works by temporarily setting sys.setprofile()
to None
,
and hence works with any profiler.
Note that easyprofile.ignore
is not a callable!
Marking functions as ignored
Functions can be marked as always ignored using the @easyprofile.ignored
decorator:
>>> # this function is ignored by easyprofile
>>> @easyprofile.ignored
... def ignored_func():
... return 42
...
>>> with easyprofile.profile(myfunc):
... ignored_func()
...
42
Note that unlike the easyprofile.ignore
context manager, the decorator only
works with easyprofile
.
Class-based profilers
To facilitate the creation of custom profilers, the easyprofile.BaseProfile
class provides a method-based interface to handling events. A method
with a name _<event>
(i.e. underscore -- event name) is called whenever the
event <event>
is encountered, with a signature of 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
...
The class-based profilers are easily invoked through their profile()
class
method, which forwards its arguments to the constructor:
>>> # 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file easyprofile-0.1.5.tar.gz
.
File metadata
- Download URL: easyprofile-0.1.5.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3824b1d4a5b654671aeeef8f26934e765ff3995f81a4b38e405f1b86c6ad146 |
|
MD5 | d411cc95b5c944f99b2444dd53410528 |
|
BLAKE2b-256 | 5a40834fae685adb225401eb3fe03148519907cf67221b4ed32b2470757b9af0 |
File details
Details for the file easyprofile-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: easyprofile-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b58e0b8b1c72d93fa33d71b69a1de4a9e18d9b219c0c8fd23d87dbdc64770b45 |
|
MD5 | 241fc503ce67b1e71b9f286088269e30 |
|
BLAKE2b-256 | 037202240174a0cb568c825e0e7235bfb580df943db3a484808b8beaad848000 |