Skip to main content

Profiler Module

The Profiler module provides standardized profiling for performance monitoring across praisonai and praisonai-agents.

Features

  • Import timing
  • Function execution timing
  • Flow tracking
  • File/module usage tracking
  • Memory usage (tracemalloc)
  • API call profiling (wall-clock time)
  • Streaming profiling (TTFT, total time)
  • Statistics (p50, p95, p99)
  • cProfile integration
  • Flamegraph generation
  • Line-level profiling
  • JSON/HTML export

Import

from praisonai.profiler import Profiler, profile, profile_imports

Quick Examples

Profile a Function

from praisonai.profiler import profile

@profile
def my_function():
    # Your code here
    pass

Profile a Block

from praisonai.profiler import Profiler

with Profiler.block("my_operation"):
    do_something()

Profile API Calls

from praisonai.profiler import Profiler
import requests

with Profiler.api_call("https://api.example.com") as call:
    response = requests.get("https://api.example.com/data")

Profile Streaming

from praisonai.profiler import Profiler

with Profiler.streaming("chat") as tracker:
    tracker.first_token()
    for chunk in stream:
        tracker.chunk()

Profile Imports

from praisonai.profiler import profile_imports

with profile_imports():
    import heavy_module

Profiler Class

Static Methods

Profiler.block(name)

Context manager for profiling a code block.
with Profiler.block("database_query"):
    results = db.query(...)

Profiler.api_call(url)

Context manager for profiling API calls.
with Profiler.api_call("https://api.openai.com/v1/chat") as call:
    response = openai.chat.completions.create(...)

Profiler.streaming(name)

Context manager for profiling streaming operations.
with Profiler.streaming("llm_stream") as tracker:
    tracker.first_token()  # Call when first token arrives
    for chunk in stream:
        tracker.chunk()    # Call for each chunk

Profiler.report()

Print a profiling report to console.
Profiler.report()

Profiler.get_statistics()

Get profiling statistics. Returns: dict with p50, p95, p99 percentiles
stats = Profiler.get_statistics()
print(f"p50: {stats['p50']}ms")
print(f"p95: {stats['p95']}ms")
print(f"p99: {stats['p99']}ms")

Profiler.export_json(path)

Export profiling data to JSON.
Profiler.export_json("profile_data.json")

Profiler.export_html(path)

Export profiling data to HTML report.
Profiler.export_html("profile_report.html")

Profiler.reset()

Reset all profiling data.
Profiler.reset()

Decorators

@profile

Decorator to profile a function.
from praisonai.profiler import profile

@profile
def expensive_operation():
    # This function will be profiled
    pass

@profile
async def async_operation():
    # Async functions are also supported
    pass

Context Managers

profile_imports()

Profile import times for modules.
from praisonai.profiler import profile_imports

with profile_imports():
    import pandas
    import numpy
    import tensorflow

# Check which imports were slow
Profiler.report()

Example: Full Profiling Session

from praisonai.profiler import Profiler, profile, profile_imports

# Profile imports
with profile_imports():
    from praisonaiagents import Agent

# Profile function
@profile
def run_agent(prompt):
    agent = Agent(name="Test")
    return agent.start(prompt)

# Profile blocks
with Profiler.block("full_workflow"):
    with Profiler.block("setup"):
        # Setup code
        pass
    
    with Profiler.block("execution"):
        result = run_agent("Hello")
    
    with Profiler.block("cleanup"):
        # Cleanup code
        pass

# Get report
Profiler.report()

# Export data
Profiler.export_json("profile.json")
Profiler.export_html("profile.html")

# Get statistics
stats = Profiler.get_statistics()
print(f"Median execution time: {stats['p50']}ms")