Skip to main content

⏱️ LatencyWatch

PyPI version PyPI Downloads

License

LatencyWatch is a lightweight Python profiler for tracing nested function call latencies using sys.setprofile. It tracks execution time for every function call — including self-time (excluding children) — and presents a hierarchical report of where time was spent.


🚀 Installation

Install the package directly from PyPI:

pip install latencywatch

🔍 Features

  • ✅ Zero Configuration - Start profiling with just a decorator
  • 📊 Hierarchical Reports - View nested call stacks with timing information
  • ⚡ Low Overhead - Minimal impact on application performance
  • 🧵 Thread-Safe - Built-in thread-local storage for multi-threaded apps
  • 🎯 Flexible Filtering - Include/exclude specific functions or modules
  • 📈 Performance Insights - Separate self-time vs total time measurements
  • 🔍 Root-Only Mode - Focus on top-level function calls for high-level analysis

🧪 Basic Usage

Simple Profiling

from latency_watch import LatencyWatch
import time

@LatencyWatch.watch
def process_data():
    def prepare():
        time.sleep(0.05)
    prepare()
    time.sleep(0.1)

process_data()
print(LatencyWatch.get_last_report(threshold_ms=1))

Advanced Configuration

from latency_watch import LatencyWatch, TraceConfig
import time

# Configure profiling options
TraceConfig.set(
    include_names=["my_module"],  # Only profile functions from my_module
    exclude_names=["test_"],      # Exclude test functions
    max_depth=3,                 # Limit call stack depth
    min_duration_ms=5,           # Only show calls taking >5ms
    root_only=False              # Show full call stack
)

def example():
    time.sleep(0.1)
    another_function()

def another_function():
    time.sleep(0.05)

# Profile with current configuration
LatencyWatch.watch(example)()
print(LatencyWatch.get_last_report())

📋 API Reference

@LatencyWatch.watch

Decorator to trace function execution time.

LatencyWatch.get_last_report(threshold_ms=0, as_dict=False)

Get the profiling report from the last execution.

  • threshold_ms: Minimum duration (in ms) to include in report
  • as_dict: Return structured dictionary instead of formatted string

LatencyWatch.reset()

Clear all recorded profiling data.

TraceConfig.set(**kwargs)

Configure profiling behavior:

  • include_names: List of function/module name patterns to include
  • exclude_names: List of function/module name patterns to exclude
  • max_depth: Maximum call stack depth to track
  • min_duration_ms: Minimum duration to include in reports (ms)
  • root_only: Only track direct children of the root function

🧪 Testing

Run the test suite:

python -m pytest tests/

Or use the test runner:

python tests/run_all_tests.py

📦 Project Structure

latencywatch/
├── latencywatch/
│   ├── __init__.py
│   ├── profiler.py
│   └── trace_config.py
├── tests/
│   ├── test_basic_hierarchy.py
│   ├── test_include_exclude.py
│   ├── test_max_depth.py
│   ├── test_min_duration.py
│   ├── test_root_only.py
│   └── run_all_tests.py
├── README.md
├── setup.py
├── pyproject.toml
└── LICENSE

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.


🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


📄 Changelog

0.2.0

  • Added advanced filtering with include/exclude patterns
  • Implemented max depth limiting
  • Added root-only profiling mode
  • Improved test coverage and documentation
  • Added thread-safety improvements

0.1.0

  • Initial release
  • Basic function profiling
  • Simple hierarchical reports
  • Thread-local storage support

Metadata

Release files for latencywatch 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for latencywatch 0.2.0
File Size Uploaded
latencywatch-0.2.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for latencywatch 0.2.0
File Interpreter ABI Platform
latencywatch-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.5 kB

Release files / latencywatch-0.2.0.tar.gz

Download URL latencywatch-0.2.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fbc0d094eaeb1b6634b2840ecc099c910ac5fdee2ab5688b18f60b40ce0b7d99
BLAKE2b-256 checksum
How to use checksums
5a52f0db4ff7670c84248d4ac205f0c44b61f4c5d0ec99f89b6e3d81cf6cee13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.7

Release files / latencywatch-0.2.0-py3-none-any.whl

Download URL latencywatch-0.2.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fce4c3616b1cba23dbf7307d9b942e0b8c861f64f4a7c84b5f8cd0abb2757f58
BLAKE2b-256 checksum
How to use checksums
cda0f235ac8800124f9f409ae181bf8d439cf31764ee4024be7dddb7b7c9f73d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page