Skip to main content

A simple decorator to profile memory usage in production apps

Project description

profile-decorator

Problem/Motivation

Find memory leaks in Python code quickly without significant code changes in a quickly digestible manner such that the root cause can be easily diagnosed.

Until open-telemetry Adds metrics API, I want a quick and dirty decorator that will give me the memory usage of the process in scope before and after a function is run.

Usage

# example.py
from profile_decorator import profile_decorator

profile_decorator.init()


@profile_decorator.profile_memory
def my_func():
    print("hello, world")


if __name__ == "__main__":
    my_func()
$ poetry run python example.py
hello, world
{
  "start_time": "2021-08-29T13:20:31.435036",
  "uss_memory_before": 6852608,
  "end_time": "2021-08-29T13:20:31.435892",
  "uss_memory_after": 6995968,
  "lines": [
    {
      "filename": "/home/harry/projects/GitHub/hkiang01/profile-decorator/example.py",
      "lineno": 7,
      "size": 752,
      "size_diff": 0
    },
    {
      "filename": "/home/harry/projects/GitHub/hkiang01/profile-decorator/example.py",
      "lineno": 12,
      "size": 560,
      "size_diff": 0
    }
  ]
}

Design

The decorator will collect the following information:

  • name of function
  • filepath
  • line number
  • memory usage before function is run
  • timestamp when function is called
  • timestamp when function is finished
  • memory usage after function is run

It will expose this in json or other easily digestble format for reporting purposes.

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

profile-decorator-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

profile_decorator-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file profile-decorator-0.1.0.tar.gz.

File metadata

  • Download URL: profile-decorator-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-31-generic

File hashes

Hashes for profile-decorator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c260c0ecbab054070c1f7d941d7139cf945c644dc07b1e060d165060020a2684
MD5 f39b68b508809c767a5963126d998d10
BLAKE2b-256 c9f8087550d97c083575067fefc802a0e4e4bc6dc29ff3d53cedab0a5e716614

See more details on using hashes here.

File details

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

File metadata

  • Download URL: profile_decorator-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-31-generic

File hashes

Hashes for profile_decorator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d3c117192dd482fa99ecd7a2d0fbd04f157e769eb3c100242d6b52ff247ee73
MD5 3cbf6df182294517d98dd79534cf223e
BLAKE2b-256 972a6c0f2483878a524356533b448c25e7949e2b40481363ed9db1b14b785994

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