profiler • AI Agents Framework
PraisonAI Profiler Module
profiler
AI Agents Framework
PraisonAI Profiler Module
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
Usage:
from praisonai.profiler import Profiler, profile, profile_imports
# Profile a function
@profile
def my_function():
pass
# Profile a block
with Profiler.block("my_operation"):
do_something()
# Profile API calls
with Profiler.api_call("https://api.example.com") as call:
response = requests.get(...)
# Profile streaming
with Profiler.streaming("chat") as tracker:
tracker.first_token()
for chunk in stream:
tracker.chunk()
# Profile imports
with profile_imports():
import heavy_module
# Get report with statistics
Profiler.report()
stats = Profiler.get_statistics()
# Export
Profiler.export_json()
Profiler.export_html()Import
from praisonai import profilerClasses
Record of a single timing measurement.
APICallRecordRecord of an API/HTTP call.
StreamingRecordRecord of streaming operation (LLM responses).
MemoryRecordRecord of memory usage.
ImportRecordRecord of a module import.
FlowRecordRecord of execution flow.
StreamingTrackerTrack streaming operations (LLM responses).
ProfilerCompatCompatibility wrapper for old classmethod-based Profiler usage.
ImportProfilerContext manager to profile imports.
Functions
Get the current profiler (context-aware or default).
set_profiler()Set the current profiler in context.
profile()Decorator to profile a function.
profile_async()Decorator to profile an async function.
profile_imports()Create an import profiler context manager.
profile_api()Decorator to profile a function as an API call.
profile_api_async()Decorator to profile an async function as an API call.
profile_detailed()Decorator for detailed cProfile profiling.
profile_lines()Decorator for line-level profiling.
time_import()Time how long it takes to import a module.
check_module_available()Check if a module is available without importing it.
Constants
| Name | Value |
|---|---|
_PROFILER_MAX | _get_profiler_max() |
_TRACEMALLOC_LOCK | threading.Lock() |
_TRACEMALLOC_STARTERS | 0 |
_IMPORT_HOOK_LOCK | threading.Lock() |
_IMPORT_HOOK_ORIGINAL | None |