Skip to main content

A script tracking utility to generate flamegraph

Project description

Preview

FlameTracker

Build Status PyPI Version License

The FlameTracker library provides a way to track and visualize function calls and execution times within Python programs. It includes features like:

  • Hierarchical action tracking
  • Timing of function calls
  • Flamegraph generation for performance visualization
  • JSON and string representation of call structures

Table of Contents

Installation

To use the FlameTracker library, install it in your Python environment:

pip install flametracker

Usage

Basic Tracking

This example demonstrates how to track a single action using the Tracker object.

import flametracker

# Create a tracker and track a single action
with flametracker.Tracker() as tracker:
    for _ in range(10):
        with tracker.action("example_action"):
            # Simulate some computation
            pass

# Print the tracked actions as a string
print(tracker.to_str(
    ignore_args=False # Remove args and results, may be useful in some cases to prevent bloat
))

Generated tracking:

@root()
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
│ example_action() 0.00ms
╰─> () 0.01ms {'@root': 1, 'example_action': 10}

Generating a Flamegraph

This example illustrates how to generate a flamegraph from tracked actions and save it as an HTML file.

import flametracker

# Create a tracker and track some actions
with flametracker.Tracker() as tracker:
    with tracker.action("example_action"):
        pass

# Generate a flamegraph and save it to a file
html_output = tracker.to_flamegraph(
    group_min_percent = 0.01, # If use_calls_as_value is False, group short actions together (percentage of total time)
    splited = False, # Split flamegraph by first action
    use_calls_as_value = False # Use number of tracked calls as node value, may be a dict of group value
)
with open("flamegraph.html", "w", encoding="utf-8") as f:
    f.write(html_output)

Nested Actions

This example shows how to track nested actions and set results for specific actions.

import flametracker

# Create a tracker and track nested actions
with flametracker.Tracker() as tracker:
    with tracker.action("parent_action") as parent:
        with tracker.action("child_action") as child:
            # Set a result for the child action
            child.set_result("Hello from child!")

print(tracker.to_str())

Generated tracking:

@root()
│ parent_action()
│ │ child_action() 0.00ms
│ ╰─> () 0.00ms {'parent_action': 1, 'child_action': 1}
╰─> () 0.01ms {'@root': 1, 'parent_action': 1, 'child_action': 1}

Function Wrapping

This example demonstrates how to use the @wrap decorator to automatically track function calls.

import flametracker

# Wrap a function to track its execution
@flametracker.wrap
def my_function(x):
    return x * 2

# Use the tracker to monitor the wrapped function
with flametracker.Tracker() as tracker:
    result = my_function(5)
    print(f"Result: {result}")

print(tracker.to_str())

Generated tracking:

@root()
│ my_function(5) 0.00ms
╰─> () 0.01ms {'@root': 1, 'my_function': 1}

Tracker Manual Activation

This example demonstrates how to manually activate and deactivate the tracker, and how to check its active state.

import flametracker

# Create a tracker
tracker = flametracker.Tracker()

@flametracker.wrap
def some_action():
    print("Some action done")

@flametracker.wrap
def perform_update():
    # Perform some actions
    some_action()
    some_action()

@flametracker.wrap
def update():
    # Check if the tracker is active
    if tracker.is_active():
        print("Tracker is active")
    else:
        print("Tracker is inactive")
    # Activate or deactivate the tracker based on user input
    inp = input("Enter 'a' to activate or 'd' to deactivate: ")
    if inp == "a":
        tracker.activate()
        print("Tracker activated")
    elif inp == "d":
        if tracker.try_deactivate():
            print("Tracker deactivated")
            print(tracker.to_str())
        else:
            print("Deactivation requested")
    perform_update()

# Continuously update based on user input
while True:
    update()

Running Tests

To run the base test suite using pytest, execute:

pytest tests/test_base.py

To generate render tests in tests/renders, execute:

pytest tests/test_renders.py

License

This project is licensed under the MIT License.

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

flametracker-0.1.9.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

flametracker-0.1.9-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file flametracker-0.1.9.tar.gz.

File metadata

  • Download URL: flametracker-0.1.9.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flametracker-0.1.9.tar.gz
Algorithm Hash digest
SHA256 2431424b5757431474d0463f680e1989f6fd67ff5f525911af6dc18cc645be90
MD5 e102a1d41c1eaef517d5f5217f7be3f3
BLAKE2b-256 2f4edbb4b8c2d06724dd62c02f0db99df357d52f10634a2cd7b9c0135699395c

See more details on using hashes here.

Provenance

The following attestation bundles were made for flametracker-0.1.9.tar.gz:

Publisher: deploy.yml on EtienneMR/flametracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flametracker-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: flametracker-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flametracker-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 31b2ce6e61d36d01dd7ea6f092d5b3fcda8abb4eaede36c551854d462ea5d7bd
MD5 9e7e02c981393fc121f87defbb564ef5
BLAKE2b-256 9c84aa7d6fde0d3d04620c9ca8510f34a4c0f00da1cd6977885f7bcb070287b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for flametracker-0.1.9-py3-none-any.whl:

Publisher: deploy.yml on EtienneMR/flametracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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