Skip to main content

A flexible profiling utility for Python.

Project description

flexprofiler

A flexible profiling utility for Python.

Installation

pip install flexprofiler

Usage

from flexprofiler import track, stats

@track
def my_function():
    # Your code
    pass

for _ in range(3):
    my_function()
    
stats() # Display profiling statistics

Examples

1. Tracking a Simple Function

import time
from flexprofiler import stats, track

@track()  # Use @track() decorator to profile the function
def simple_func():
    time.sleep(0.1)

simple_func()
simple_func()

stats() # display the profiling statistics

Output

Detailed Function Call Statistics:
simple_func: ───2 calls, 200.43ms, Avg: 100.21ms, Std: 0.01ms, Median: 100.21ms

2. Tracking All Methods in a Class

import time
from flexprofiler import track_all, stats

@track_all()  # Use @track_all() decorator to profile all methods in the class
class Foo:
    def example_method(self):
        self.another_method()
        time.sleep(0.1)
    def another_method(self):
        time.sleep(0.2)

Foo().example_method()
Foo().another_method()

stats()

output:

Detailed Function Call Statistics:
Foo.example_method: ────────1 calls, 300.23ms, Avg: 300.23ms
  └──Foo.another_method: ───1 calls, 200.10ms, Avg: 200.10ms
Foo.another_method: ────────1 calls, 200.11ms, Avg: 200.11ms

3. Tracking All Methods in a Class Recursively

import time
from flexprofiler import track_all, stats

@track_all(max_depth=3)
class Foo:
    def __init__(self):
        self.sub_class = Bar()
    def example_method(self):
        self.another_method()
        time.sleep(0.1)
    def another_method(self):
        time.sleep(0.2)
    def calling_subclass_method(self):
        for i in range(3):
            self.sub_class.subclass_method_1()
            self.sub_class.subclass_method_2()
            self.sub_class.subclass_method_3()

class Bar:
    def subclass_method_1(self):
        time.sleep(0.05)
    def subclass_method_2(self):
        self.a()
        self.b()
    def subclass_method_3(self):
        self.a()
        self.b()
    def a(self):
        time.sleep(0.02)
    def b(self):
        time.sleep(0.01)

obj = Foo()
obj.example_method()
obj.calling_subclass_method()

stats()

output:

Detailed Function Call Statistics:
Foo.__init__: ───────────────────2 calls, 0.11ms, Avg: 0.06ms, Std: 0.08ms, Median: 0.06ms
Foo.example_method: ────────────1 calls, 300.29ms, Avg: 300.29ms
  └──Foo.another_method: ───────1 calls, 200.12ms, Avg: 200.12ms
Foo.calling_subclass_method: ───1 calls, 334.50ms, Avg: 334.50ms
  ├──Bar.subclass_method_1: ────3 calls, 150.31ms, Avg: 50.10ms, Std: 0.03ms, Median: 50.09ms
  ├──Bar.subclass_method_2: ────3 calls, 91.64ms, Avg: 30.55ms, Std: 0.04ms, Median: 30.56ms
  │    ├──Bar.a: ───────────────3 calls, 60.27ms, Avg: 20.09ms, Std: 0.02ms, Median: 20.10ms
  │    └──Bar.b: ───────────────3 calls, 30.36ms, Avg: 10.12ms, Std: 0.05ms, Median: 10.09ms
  └──Bar.subclass_method_3: ────3 calls, 91.45ms, Avg: 30.48ms, Std: 0.18ms, Median: 30.46ms
       ├──Bar.a: ───────────────3 calls, 60.29ms, Avg: 20.10ms, Std: 0.02ms, Median: 20.10ms
       └──Bar.b: ───────────────3 calls, 30.28ms, Avg: 10.09ms, Std: 0.01ms, Median: 10.09ms

4. Track All lines in a function

import time
from flexprofiler import track, stats

@track(lines=True)
def bar(n):
    total = 0
    for i in range(n):
        total += i
        if i % 2 == 0:
            time.sleep(0.001)
        else:
            time.sleep(0.0005)
    return total

@track()
def foo():
    for _ in range(3):
        bar(50)

outputs:

<title>stdin</title>
Detailed Function Call Statistics:
foo: ──────────────────────────1 calls, 129.46ms, Avg: 129.46ms
  └──bar: ─────────────────────3 calls, 128.98ms, Avg: 42.99ms, Std: 0.59ms, Median: 42.82ms
     12:     total = 0────────────────────3 calls, 0.14ms, Avg: 0.05ms
     13:     for i in range(n):───────────153 calls, 1.64ms, Avg: 0.01ms
     14:         total += i───────────────150 calls, 0.80ms, Avg: 0.01ms
     15:         if i % 2 == 0:───────────150 calls, 0.65ms, Avg: 0.00ms
     16:             time.sleep(0.001)────75 calls, 81.61ms, Avg: 1.09ms
     18:             time.sleep(0.0005)───75 calls, 43.88ms, Avg: 0.59ms
     19:     return total─────────────────3 calls, 0.01ms, Avg: 0.00ms

Author

Arthur Bucker (abucker@andrew.cmu.edu)

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

flexprofiler-0.1.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flexprofiler-0.1.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file flexprofiler-0.1.1.tar.gz.

File metadata

  • Download URL: flexprofiler-0.1.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.11

File hashes

Hashes for flexprofiler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 56128154d29b9bb2fff439f0380d71c25e5f40894d6c43f3292a0b697f34850a
MD5 32e73183cedb3cbc1672d51cc48fe2f2
BLAKE2b-256 81cfeb7237eeab36f762e5f1e0b1f8fddeb5059f7a343ee9a774bf11df9e1c14

See more details on using hashes here.

File details

Details for the file flexprofiler-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: flexprofiler-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.11

File hashes

Hashes for flexprofiler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7bd66c01868659ff58c00b50f3522b50c56bbdad40be5a894377f6cd8785ded
MD5 433b852a2d2d1fdcff805a7d9f9886a5
BLAKE2b-256 b175c1a75b4f22b8f723fab92ff44cbb8af0294e8650645941a5cd9e1ca2cb9a

See more details on using hashes here.

Supported by

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