Skip to main content

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 _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

easyprofile-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

easyprofile-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file easyprofile-0.1.0.tar.gz.

File metadata

  • Download URL: easyprofile-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for easyprofile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 80b044d50d7528b46df590516a552e1a0d260913bc224ced09435acea8193188
MD5 9226d977665ab150648209f8370dd430
BLAKE2b-256 e6d531b47330e34e5ea8013a26d83d8bee2b62a19aa3a9ea6c9a32db23ca3982

See more details on using hashes here.

File details

Details for the file easyprofile-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: easyprofile-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for easyprofile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f12ad6a6330ab63c549b9b3cffd390661c9ee61ef73d7b8f929e279ab62188f6
MD5 3f6a785abac91c696212ba5b27ae37c4
BLAKE2b-256 84b4bed97c0cf87f559caa0a97eb87b34e13326e74eba22dd94d6a59f87e66d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page