Skip to main content

A flexible profiling utility for Python.

Project description

banner

Python Version PyPI License PyPI Version

🚀 About

FlexProfiler is a lightweight, flexible profiling utility for Python that makes it easy to measure function and method performance with minimal code changes. It supports both function-level and line-level profiling, recursive class method tracking, and customizable statistics reporting. Designed for developers who want actionable insights into their code’s runtime behavior, FlexProfiler helps you identify bottlenecks and optimize performance with simple decorators and clear output.

📦 Installation

pip install flexprofiler

:computer: Usage

The most common usage involves adding simple decorators to your functions or classes, enabling you to profile execution time and other metrics with minimal changes to your codebase. This approach makes it easy to identify bottlenecks and optimize your code efficiently.

  • @track() - decorate a function to collect call counts and timing statistics. Args:
    • lines: boolean, collect per-line timing inside the function.
  • @track_all() - Decorate a class to profile all its methods. Args:
    • max_depth: integer, how deep to recursively profile composed objects (default: 10).
    • include: list of method names to track (default: None to track all).
    • exclude: list of method names to skip (default: None to skip none).
    • arg_sensitive: list of method names to track argument values (default: None).
  • stats() - print aggregated profiling statistics (function-level and, when enabled, line-level). Args:
    • unit: the time unit to use for displaying statistics (default: "ms").

⬇️ 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: flexprofiler command output screenshot

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: flexprofiler command output screenshot

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: flexprofiler command output screenshot

4. Track All lines in a function

import time
from flexprofiler import track, stats


@track(lines=True)  # with line tracking
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()  # no line tracking
def baz():
    time.sleep(0.1)

@track()  # no line tracking
def foo():
    for _ in range(3):
        bar(50)
    baz()

foo()
stats()  # display the profiling statistics

outputs: flexprofiler command output screenshot

🤝 Feedback and Contributions

Contributions, suggestions, and feedback are welcome! Feel free to open issues or submit pull requests on GitHub.

Author

Arthur Fender C Bucker

website

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.3.tar.gz (412.2 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.3-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flexprofiler-0.1.3.tar.gz
  • Upload date:
  • Size: 412.2 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.3.tar.gz
Algorithm Hash digest
SHA256 5e19c2d6e75f94efd632a72241be77dfd41a6b036ce10d54e9b5b7e4b76a69af
MD5 55978c1d9705805e57d9594b7b96653c
BLAKE2b-256 f1f46ed7d395d108d0207cca33f0560bae7628daeca3dbc43b497617703429c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flexprofiler-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 84167d6348141673d11af693f0f391442c3b57a44029575ebf3aa637ffc87a9e
MD5 a6ce7823325db1366f9557649cce3c04
BLAKE2b-256 1fc6083be4ba22fa35746b836e01f6342bf45d93d813b28ec6d40e89161004c8

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