apifrom.performance.profilerο
Profiling utilities for APIFromAnything.
This module provides tools for profiling API endpoints, measuring performance, and generating reports to help identify and fix performance bottlenecks.
Overviewο
Classes
BaseMiddlewareProfileMiddleware
Classesο
Profiles API endpoints to measure performance and identify bottlenecks.
This class provides tools for profiling API endpoints, measuring response times, memory usage, and CPU usage, and generating profile reports.
Initialize an API profiler.
- param output_dir:
The directory to save profile reports to (defaults to current directory)
- param enabled:
Whether profiling is enabled
- apifrom.performance.profiler.clear()ο
Clear all profile data.
- apifrom.performance.profiler.disable()ο
Disable profiling.
- apifrom.performance.profiler.enable()ο
Enable profiling.
- apifrom.performance.profiler.get_all_reports()ο
Get profile reports for all endpoints.
- returns:
A list of ProfileReport instances
- apifrom.performance.profiler.get_report(endpoint_name)ο
Get a profile report for an endpoint.
- param endpoint_name:
The name of the endpoint
- returns:
A ProfileReport instance or None if no profile data exists
- apifrom.performance.profiler.profile_endpoint(endpoint_name)ο
Decorator for profiling an API endpoint.
- param endpoint_name:
The name of the endpoint being profiled
- returns:
A decorated function
- apifrom.performance.profiler.save_reports(prefix=None)ο
Save all profile reports to files.
- param prefix:
A prefix to add to the filenames
- returns:
A list of file paths where reports were saved
- BaseMiddleware(**options):bases: abc.ABC
Base middleware class for APIFromAnything.
- ProfileMiddleware(output_dir = None, save_interval = 300, enabled = True, profiler = None):bases: apifrom.middleware.base.BaseMiddleware
Middleware for profiling API requests and responses.
This middleware profiles API requests and responses, collecting performance metrics and generating profile reports. It can be used to identify performance bottlenecks and optimize API performance.
Initialize the profile middleware.
- param output_dir:
The directory to save profile reports to
- param save_interval:
The interval to save profile reports in seconds
- param enabled:
Whether profiling is enabled
- param profiler:
The profiler to use (creates a new one if None)
- apifrom.performance.profiler.clear()ο
Clear all profile data.
- apifrom.performance.profiler.disable()ο
Disable profiling.
- apifrom.performance.profiler.dispatch(request, call_next)ο
- :async:
Dispatch a request, profiling the execution time.
- param request:
The request to process
- param call_next:
The next middleware or route handler
- returns:
The response
- apifrom.performance.profiler.enable()ο
Enable profiling.
- apifrom.performance.profiler.get_all_reports()ο
Get all profile reports.
- returns:
A list of profile reports
- apifrom.performance.profiler.get_report(endpoint)ο
Get a profile report for an endpoint.
- param endpoint:
The endpoint to get the report for
- returns:
The profile report, or None if not found
- apifrom.performance.profiler.process_request(request)ο
- :async:
Process a request (required by BaseMiddleware).
- param request:
The request to process
- returns:
The processed request
- apifrom.performance.profiler.process_response(response)ο
- :async:
Process a response (required by BaseMiddleware).
- param response:
The response to process
- returns:
The processed response
Represents a performance profile report for an API endpoint.
This class provides methods for analyzing and visualizing profile data, as well as exporting it to various formats.
Initialize a profile report.
- param endpoint_name:
The name of the endpoint being profiled
- param profile_data:
The raw profile data
- apifrom.performance.profiler.get_recommendations()ο
Get performance optimization recommendations based on the profile data.
- returns:
A list of recommendations
- apifrom.performance.profiler.print_summary()ο
Print a summary of the report to the console.
- apifrom.performance.profiler.save(file_path)ο
Save the report to a file.
- param file_path:
The path to save the report to
- apifrom.performance.profiler.to_dict()ο
Convert the report to a dictionary.
- returns:
A dictionary representation of the report
- apifrom.performance.profiler.to_json(pretty=True)ο
Convert the report to a JSON string.
- param pretty:
Whether to format the JSON with indentation
- returns:
A JSON string representation of the report
- class apifrom.performance.profiler.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]ο
Request class for APIFromAnything.
This class wraps a Starlette request and provides methods for accessing request data in a convenient way.
- apifrom.performance.profiler._requestο
The underlying Starlette request.
- apifrom.performance.profiler.path_paramsο
Path parameters extracted from the URL.
- apifrom.performance.profiler.query_paramsο
Query parameters extracted from the URL.
- apifrom.performance.profiler.headersο
HTTP headers.
- apifrom.performance.profiler.methodο
HTTP method.
- apifrom.performance.profiler.pathο
Request path.
- apifrom.performance.profiler._bodyο
Cached request body.
Initialize a new Request instance.
- param request:
The underlying Starlette request.
- param path_params:
Path parameters extracted from the URL.
- param method:
The HTTP method.
- param path:
The request path.
- param query_params:
Query parameters.
- param headers:
HTTP headers.
- param body:
Request body.
- param client_ip:
Client IP address.
- class apifrom.performance.profiler.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]ο
Response class for APIFromAnything.
This class represents an HTTP response and provides methods for setting response data, status code, and headers.
- apifrom.performance.profiler.contentο
The response content.
- apifrom.performance.profiler.status_codeο
The HTTP status code.
- apifrom.performance.profiler.headersο
HTTP headers.
- apifrom.performance.profiler.content_typeο
The content type of the response.
Initialize a new Response instance.
- param content:
The response content.
- param status_code:
The HTTP status code.
- param headers:
HTTP headers.
- param content_type:
The content type of the response.