Simplifies Python code profiling by offering an easy-to-use decorator for measuring function performance. Customize profiling with options for sort criteria and output format. Perfect for quickly identifying bottlenecks.
Project description
@profile
Simplifies Method Profiling
The profile decorator is designed for easy profiling of Python functions, leveraging the built-in cProfile and pstats modules for performance analysis. It provides a flexible way to output profiling information either to the standard output, logger or to a file and allows for sorting of profiling data based on various criteria.
The decorator is designed to be a "no frills" implementation of CProfile that saves you time. Just add it to a method, call your endpoint or run your script, and see the output.
Features
Features Seamless Integration: Just decorate functions you wish to profile without altering their implementation.
- Flexible Output Options: Choose to print the profiling results to stdout, a logger or save them to a file for further analysis.
- Customizable Sorting: Sort profiling results by criteria like 'cumulative time', 'number of calls', and more, to focus on the most relevant performance metrics.
- Adjustable Detail Level: Control the amount of information displayed through optional parameters, tailoring the output to your needs.
Prerequisites
There are no external dependancies, profiling is done using the standard library
package CProfile
.
Installation
Install the package
pip install profile-decorator
Usage
Import the profile decorator:
from profile_decorator import profile
Basic Usage
Basic Usage To use the decorator without any customization, simply decorate your function. By default, it profiles the function, sorts the results by cumulative time, and prints the results to standard output.
@profile
def example_function():
# Function code to profile
pass
example_function()
Limiting Output
Limit the profiling output to a specific number of rows to focus on the top time-consuming operations.
@profile(n_rows=10)
def example_function():
# Function code to profile
pass
example_function()
Changing Sort Criteria
Customize the sorting criteria of the profiling report (e.g., by number of calls).
@profile(sort_by="calls")
def example_function():
# Function code to profile
pass
example_function()
Valid Sort Options
The valid options for the sort_by parameter align with the SortKey attributes in the pstats module and include:
- 'cumulative' - Total time spent in the function and all sub-functions (default).
- 'time' - Internal time spent in the function (excluding sub-functions).
- 'calls' - Number of calls to the function.
- 'ncalls' - Same as calls, but distinguishes between direct and indirect calls.
See the pstats module for a complete up to date list.
Output to File
To save the profiling results to a file, specify the output and filename parameters.
@profile(output='file', filename='profile_stats.txt')
def example_function():
# Function code to profile
pass
example_function()
Output Options
- 'stdout' - Print the profiling results to standard output (default).
- 'file' - Write the profiling results to a file. Requires specifying the filename parameter.
- 'log' - Log the profiling results using the configured logging system. This is useful for integrating profiling results into application logs.
Configuring Logging
Before using the log output option, ensure the logging system is configured to handle messages at the INFO level or lower. Here's a basic configuration example:
import logging
logging.basicConfig(level=logging.INFO)
@profile(output='log')
def example_function():
# Function code to profile
pass
example_function()
Async Support
The profile decorator is enhanced to support profiling of both synchronous and asynchronous functions. This feature allows developers to gain insights into the performance characteristics of their code, whether it operates synchronously or leverages Python's asyncio for asynchronous execution.
Usage is identical:
@profile
async def async_function():
# Async function code to profile
await some_async_operation()
await async_function()
License
Distributed under the Apache Software License. See LICENSE.txt
for more information.
Contact
Joshua Brumpton - ja.brumpton@gmail.com
Project Link: github repo
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for profiling_decorator-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b625f9b5a0f20185dc73d6b84036d34a61f2a837f3096647e800ac3eb42debc |
|
MD5 | 8daf1ca452235bcde4ad91f4aff274da |
|
BLAKE2b-256 | fa9e9a4443de96c05ae2d16f89466d063515d9a7861ea272641a6eb28787e0a8 |
Hashes for profiling_decorator-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 155f8ae4b61941a602049fe2356bfdaa5241ecdcc7a2d2f88c4c553b4bc43681 |
|
MD5 | e7a4e948ff71e26a7a8886e0c85c95de |
|
BLAKE2b-256 | 3fc0d4368b7dba7852f018c2b40682deb3041f5122358ed0e6aef3b1214791c8 |