Skip to content

Feature: Asynchronous Profiling Support #7

Description

@vigsun19

Feature: Asynchronous Profiling Support

Description

Extend SmartProfiler to support profiling of asynchronous code (e.g., async def functions, asyncio tasks). Add decorators and context managers that work with async/await syntax, measuring execution time, I/O waits, and other metrics in async contexts.

Why This is Needed

Modern Python applications, especially web servers (e.g., FastAPI, aiohttp), heavily rely on asynchronous programming. Currently, SmartProfiler only supports synchronous code, limiting its usability in async environments. Adding async support will make the library more versatile and appealing to developers working on async projects.

Proposed Implementation

  • Add async versions of profiling decorators (e.g., profile_async_function) and context managers (e.g., async_profile_block) to BaseProfiler.
  • Update each profiler (CPUProfiler, DiskProfiler, etc.) to support async profiling:
    • Use asyncio.get_running_loop().time() for timing in async contexts.
    • Ensure thread-safety for async operations.
  • Add unit tests for async profiling scenarios using asyncio.
  • Update examples_general_usage.py to include an async profiling example (e.g., profiling an async HTTP request with aiohttp).

Acceptance Criteria

  • Users can profile an async def function using a decorator (e.g., @cpu_profiler.profile_async_function).
  • Users can profile an async code block using a context manager (e.g., async with cpu_profiler.async_profile_block("task"):).
  • Profiling metrics (e.g., execution time, network I/O) are accurate in async contexts.
  • Unit tests pass for async profiling scenarios.
  • Update README.md with an async profiling example.

Complexity

High (requires handling async context managers, integrating with asyncio, and ensuring thread-safety).

User Value

High (broadens applicability to async applications).

Related Issues

  • None

Additional Notes

  • Test with popular async frameworks like aiohttp and FastAPI to ensure compatibility.
  • Consider adding async-specific metrics (e.g., I/O wait time) in future releases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions