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

Classes

class apifrom.performance.profiler.APIProfiler(output_dir=None, enabled=True)[source]
Parameters:
  • output_dir (Optional[str])

  • enabled (bool)

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.

This abstract class defines the interface for middleware components. Middleware components can process requests and responses.

apifrom.performance.profiler.options

Options for the middleware.

Type:

dict

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

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

class apifrom.performance.profiler.ProfileReport(endpoint_name, profile_data)[source]
Parameters:
  • endpoint_name (str)

  • profile_data (Dict[str, Any])

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]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

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]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

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.