sgn.profile
¶
Utilities for profiling.
async_sgn_mem_profile(logger)
¶
Decorator for async functions to enable memory profiling based on logger level.
This decorator provides efficient memory profiling by checking the logger level only on the first function call, then using the appropriate wrapper (profiling or no-op) for all subsequent calls.
The memory profiling is enabled when the logger's effective level is at or below the MEMPROF level (5). When enabled, it uses Python's tracemalloc to capture memory snapshots before and after function execution, displaying detailed memory usage statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
The logger instance to use for determining profiling level and outputting memory statistics. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
A decorator function that wraps async functions with memory profiling |
Callable[[F], F]
|
capabilities. |
Example
logger = logging.getLogger("my_app") @async_sgn_mem_profile(logger) ... async def my_function(): ... # Function implementation ... pass
Source code in src/sgn/profile.py
display_top(logger, snapshot1, snapshot2, key_type='lineno', limit=10)
¶
Display comprehensive memory profiling information from two snapshots.
This function compares two memory snapshots to show both cumulative memory usage and the difference between snapshots. It filters out internal Python memory allocations and displays the top memory-consuming lines of code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
SGN logger instance with memprofile method for outputting statistics. |
required | |
snapshot1
|
Snapshot
|
First memory snapshot (taken before the operation). |
required |
snapshot2
|
Snapshot
|
Second memory snapshot (taken after the operation). |
required |
key_type
|
str
|
Grouping method for statistics ("lineno", "filename", or "traceback"). |
'lineno'
|
limit
|
int
|
Maximum number of top entries to display in each section. |
10
|
Note
This function tracks the first memory usage measurement globally and shows the change from that baseline in subsequent calls.
Source code in src/sgn/profile.py
display_topstats(logger, top_stats, limit, msg='cumulative')
¶
Display memory usage statistics in a formatted table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
SGN logger instance with memprofile method for outputting statistics. |
required | |
top_stats
|
list[Statistic] | list[StatisticDiff]
|
List of memory statistics to display. |
required |
limit
|
int
|
Maximum number of top entries to show. |
required |
msg
|
str
|
Description message for the statistics type (e.g., "cumulative", "diff"). |
'cumulative'
|
Returns:
| Type | Description |
|---|---|
int
|
Total memory size in bytes across all statistics. |