Skip to content

Diagnostics Profiling

Performance profiling tools.

Profiling Module

Performance profiling utilities.

This submodule provides utilities for measuring execution time, memory usage, and function call counts.

Examples

from rite.diagnostics.profiling import ( ... profiling_timer, ... profiling_stopwatch, ... profiling_memory ... ) @profiling_timer() ... def slow_function(): ... pass with profiling_stopwatch("task") as sw: ... pass

Modules

profiling_count_calls

Function Call Counter

Count how many times a function is called.

Examples

from rite.diagnostics.profiling import profiling_count_calls @profiling_count_calls() ... def api_call(): ... pass

Functions

profiling_count_calls(print_every: int | None = None) -> Callable[[Callable[..., T]], Callable[..., T]]

Decorator to count function calls.

Parameters:

Name Type Description Default
print_every int | None

Print count every N calls (None = don't print).

None

Returns:

Type Description
Callable[[Callable[..., T]], Callable[..., T]]

Decorated function with call counter.

Examples:

>>> @profiling_count_calls()
... def process():
...     pass
>>> process()
>>> process.call_count
1
>>> @profiling_count_calls(print_every=10)
... def api_request():
...     pass

profiling_memory

Memory Profiler

Measure memory usage of function.

Examples

from rite.diagnostics.profiling import profiling_memory @profiling_memory() ... def memory_intensive(): ... data = [0] * 1000000

Functions

profiling_memory(print_result: bool = True) -> Callable[[Callable[..., T]], Callable[..., T]]

Decorator to measure memory usage of function.

Parameters:

Name Type Description Default
print_result bool

If True, print memory usage.

True

Returns:

Type Description
Callable[[Callable[..., T]], Callable[..., T]]

Decorated function that measures memory.

Examples:

>>> @profiling_memory()
... def create_large_list():
...     return [0] * 1000000
>>> @profiling_memory(print_result=False)
... def process_data():
...     pass
Notes

Uses sys.getsizeof for basic measurement. For detailed profiling, use memory_profiler package.

profiling_stopwatch

Timer Context Manager

Context manager to measure elapsed time.

Examples

from rite.diagnostics.profiling import profiling_stopwatch with profiling_stopwatch("operation") as sw: ... time.sleep(0.1) print(sw.elapsed)

Classes

profiling_stopwatch(name: str = 'operation')

Context manager to measure elapsed time.

Attributes:

Name Type Description
name

Operation name.

elapsed float

Elapsed time in seconds.

start_time float

Start timestamp.

end_time float

End timestamp.

Examples:

>>> with profiling_stopwatch("task") as sw:
...     time.sleep(0.1)
>>> sw.elapsed > 0.1
True

Initialize stopwatch.

Parameters:

Name Type Description Default
name str

Operation name for display.

'operation'

profiling_timer

Timer Decorator

Decorator to measure function execution time.

Examples

from rite.diagnostics.profiling import profiling_timer @profiling_timer() ... def slow_function(): ... time.sleep(1)

Functions

profiling_timer(name: str | None = None, print_result: bool = True) -> Callable[[Callable[..., T]], Callable[..., T]]

Decorator to measure function execution time.

Parameters:

Name Type Description Default
name str | None

Optional name for the operation being timed.

None
print_result bool

If True, print timing result.

True

Returns:

Type Description
Callable[[Callable[..., T]], Callable[..., T]]

Decorated function that measures execution time.

Examples:

>>> @profiling_timer()
... def compute():
...     sum(range(1000000))
>>> @profiling_timer(name="API Call", print_result=False)
... def api_request():
...     pass

options: show_root_heading: true show_source: false heading_level: 2