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. .. py:currentmodule:: apifrom.performance.profiler Overview -------- **Classes** * :py:class:`APIProfiler` * :py:class:`BaseMiddleware` * :py:class:`ProfileMiddleware` * :py:class:`ProfileReport` * :py:class:`Request` * :py:class:`Response` Classes ------- .. py:class:: APIProfiler(output_dir = None, enabled = True) 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 .. :: _lock .. :: enabled .. :: output_dir .. :: profiles .. method:: clear() Clear all profile data. .. method:: disable() Disable profiling. .. method:: enable() Enable profiling. .. method:: get_all_reports() Get profile reports for all endpoints. :returns: A list of ProfileReport instances .. method:: 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 .. method:: profile_endpoint(endpoint_name) Decorator for profiling an API endpoint. :param endpoint_name: The name of the endpoint being profiled :returns: A decorated function .. method:: 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 .. py:class:: BaseMiddleware(**options):bases: abc.ABC Base middleware class for APIFromAnything. This abstract class defines the interface for middleware components. Middleware components can process requests and responses. .. attribute:: options Options for the middleware. :type: dict Initialize a new BaseMiddleware instance. :param \*\*options: Options for the middleware. .. py:class:: 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) .. :: enabled .. :: last_save_time .. :: output_dir .. :: profiler .. :: save_interval .. method:: clear() Clear all profile data. .. method:: disable() Disable profiling. .. method:: 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 .. method:: enable() Enable profiling. .. method:: get_all_reports() Get all profile reports. :returns: A list of profile reports .. method:: 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 .. method:: process_request(request) :async: Process a request (required by BaseMiddleware). :param request: The request to process :returns: The processed request .. method:: process_response(response) :async: Process a response (required by BaseMiddleware). :param response: The response to process :returns: The processed response .. py:class:: ProfileReport(endpoint_name, profile_data) 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 .. :: created_at .. :: endpoint_name .. :: profile_data .. method:: get_recommendations() Get performance optimization recommendations based on the profile data. :returns: A list of recommendations .. method:: print_summary() Print a summary of the report to the console. .. method:: save(file_path) Save the report to a file. :param file_path: The path to save the report to .. method:: to_dict() Convert the report to a dictionary. :returns: A dictionary representation of the report .. method:: 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 .. py:class:: Request(request = None, path_params = None, method = None, path = None, query_params = None, headers = None, body = None, client_ip = None) Request class for APIFromAnything. This class wraps a Starlette request and provides methods for accessing request data in a convenient way. .. attribute:: _request The underlying Starlette request. .. attribute:: path_params Path parameters extracted from the URL. .. attribute:: query_params Query parameters extracted from the URL. .. attribute:: headers HTTP headers. .. attribute:: method HTTP method. .. attribute:: path Request path. .. attribute:: _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. .. py:class:: Response(content = None, status_code = 200, headers = None, content_type = 'application/json') Response class for APIFromAnything. This class represents an HTTP response and provides methods for setting response data, status code, and headers. .. attribute:: content The response content. .. attribute:: status_code The HTTP status code. .. attribute:: headers HTTP headers. .. attribute:: 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.