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

easyprofile-0.1.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

easyprofile-0.1.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: easyprofile-0.1.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for easyprofile-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a813ae00f7fa67b5577171a08f322f625d31decde0bfea397350791f081b82e2
MD5 548b48447bca92388c696cfd1f2fd05c
BLAKE2b-256 0530884f14b7c34a6997f811dfb55261697eae6e574938a2caff335f2a1fc443

See more details on using hashes here.

File details

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

File metadata

  • Download URL: easyprofile-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for easyprofile-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 46ff9a4df598495efcb91034b72d72f3c12af5db0a330e9a53c1dc207372fe9b
MD5 dcfa5efeee21b7505ec29674562e0f8c
BLAKE2b-256 1d35c65766bda7d894a8bfc0a68e61549956e8db4dd82228099d1c9f47613145

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