Skip to main content

A timer for profiling a Python function or snippet.

Project description

auto_profiler

A timer for profiling a Python function or snippet.

Features

  • Filtering external libraries profiling.
  • Filtering very short time functions-> threshold
  • Allow depth: you can easily find the time consuming function
  • Allow loop or multiple function call
  • Allow recursive function call
  • Disable it globaly by Profiler.GlobalDisable=True to save time :)

Installation

Release version:

$ pip install auto_profiler

Development version:

$ pip install -e git+https://github.com/modaresimr/auto_profiler.git#egg=auto_profiler

Quick start

Auto profiling

More commonly, chances are that we want to measure the execution time of an entry function and all its subfunctions. In this case, it's too tedious to do it manually, and we can leverage Profiler to inject all the timing points for us automatically:

import time # line number 1
import random

from auto_profiler import Profiler, Tree

def f1():
    mysleep(.6+random.random())

def mysleep(t):
    time.sleep(t)

def fact(i):
    f1()
    if(i==1):
        return 1
    return i*fact(i-1)


def show(p):
    print('Time   [Hits * PerHit] Function name [Called from] [Function Location]\n'+\
          '-----------------------------------------------------------------------')
    print(Tree(p.root, threshold=0.5))

@Profiler(depth=4, on_disable=show)
def main():
    for i in range(5):
        f1()

    fact(3)


if __name__ == '__main__':
    main()

Example Output


Time   [Hits * PerHit] Function name [Called from] [function location]
-----------------------------------------------------------------------
8.974s [1 * 8.974]  main  [auto-profiler/profiler.py:267]  [/test/t2.py:30]
├── 5.954s [5 * 1.191]  f1  [/test/t2.py:34]  [/test/t2.py:14]
│   └── 5.954s [5 * 1.191]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
│       └── 5.954s [5 * 1.191]  <time.sleep>
|
|
|   # The rest is for the example recursive function call fact
└── 3.020s [1 * 3.020]  fact  [/test/t2.py:36]  [/test/t2.py:20]
    ├── 0.849s [1 * 0.849]  f1  [/test/t2.py:21]  [/test/t2.py:14]
    │   └── 0.849s [1 * 0.849]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
    │       └── 0.849s [1 * 0.849]  <time.sleep>
    └── 2.171s [1 * 2.171]  fact  [/test/t2.py:24]  [/test/t2.py:20]
        ├── 1.552s [1 * 1.552]  f1  [/test/t2.py:21]  [/test/t2.py:14]
        │   └── 1.552s [1 * 1.552]  mysleep  [/test/t2.py:15]  [/test/t2.py:17]
        └── 0.619s [1 * 0.619]  fact  [/test/t2.py:24]  [/test/t2.py:20]
            └── 0.619s [1 * 0.619]  f1  [/test/t2.py:21]  [/test/t2.py:14]

Manual profiling

Sometimes, we only want to measure the execution time of partial snippets or a few functions, then we can inject all timing points into our code manually by leveraging Timer:

# manual_example.py

import time

from auto_profiler import Timer, Tree


def main():
    t = Timer('sleep1', parent_name='main').start()
    time.sleep(1)
    t.stop()

    t = Timer('sleep2', parent_name='main').start()
    time.sleep(1.5)
    t.stop()

    print(Tree(Timer.root))


if __name__ == '__main__':
    main()

Run the example code:

$ python manual_example.py

and it will show you the profiling result:

2.503s  main
├── 1.001s  sleep1
└── 1.501s  sleep2

Supported frameworks

While you can do profiling on normal Python code, as a web developer, chances are that you will usually do profiling on web service code.

Currently supported web frameworks:

Examples

For profiling web service code (involving web requests), check out examples.

License

MIT

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

auto_profiler-1.4.tar.gz (8.8 kB view details)

Uploaded Source

Built Distributions

auto_profiler-1.4-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

auto_profiler-1.4-py2.py3-none-any.whl (15.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file auto_profiler-1.4.tar.gz.

File metadata

  • Download URL: auto_profiler-1.4.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

File hashes

Hashes for auto_profiler-1.4.tar.gz
Algorithm Hash digest
SHA256 e79511f1ac4e7147e3900cb1aac4ddd518ba1167501c3f80aebeac9ff7f8d203
MD5 eca64f3624686375124ea2510413e192
BLAKE2b-256 d69d888c6b4b27cb31b832a9789e792738451bd99eddb4e1fc9c22cbce5dc79f

See more details on using hashes here.

File details

Details for the file auto_profiler-1.4-py3-none-any.whl.

File metadata

  • Download URL: auto_profiler-1.4-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

File hashes

Hashes for auto_profiler-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 91183fd53cf12ba8f42d57d34e0daaabe32de816ca34d3f03bcf617e31248126
MD5 f17e2fc63809af8d50215ff2a738f941
BLAKE2b-256 eed2e5f0a5ce34825ce3555f231a4242779f1c8f60ecb84cdffb1cd4da3ddcb2

See more details on using hashes here.

File details

Details for the file auto_profiler-1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: auto_profiler-1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

File hashes

Hashes for auto_profiler-1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ef17d9955c7af6da58ad6658cde776c1fb6cab8921d2c03a452e96bee404b04c
MD5 1cc42360026c716c5255c5486efd7f92
BLAKE2b-256 7778c22ea3a9094ffcb3ac0424339389790a461abfc28ef526786b18fb22a12b

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