A FastAPI Middleware of joerick/pyinstrument to check your service performance.

Overview

fastapi_profiler

A FastAPI Middleware of joerick/pyinstrument to check your service performance.

Travis Build Status Appveyor Build Status Codecov Package version PyPI - Python Version

PyPI - Format Contributions welcome License

📣 Info

A FastAPI Middleware of pyinstrument to check your service code performance. Maybe is the best profiler of FastAPI extension.

🔰 Installation

Use pip

$ pip install fastapi_profiler -U

📝 Example

Jump to example folder.Example

Test the Code

Integration Test

Use Travis CI and AppVeyor continuously integrated environmen.

Code Style

Use flake8 and Codecov to improve the code quality.

💡 Author

📃 License

MIT ©sunhailin-Leo

Comments
  • PyInstrumentProfilerMiddleware Output to html

    PyInstrumentProfilerMiddleware Output to html

    look this:

    def __init__(
        self, app: ASGIApp,
        *,
        server_app: Optional[Router] = None,
        profiler_interval: float = 0.0001,
        profiler_output_type: str = "html",
        is_print_each_request: bool = True,
        **profiler_kwargs
    ):
        self.app = app
        self._profiler = Profiler(interval=profiler_interval)
        self._server_app = server_app
        self._output_type = profiler_output_type
        self._print_each_request = is_print_each_request
        self._profiler_kwargs: dict = profiler_kwargs
    
    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
          # register an event handler for profiler stop
          if self._server_app is not None:
              self._server_app.add_event_handler("shutdown", self.get_profiler_result)
    
    def register_middleware(app: FastAPI):
        if settings.PROFILER_ON:
            app.add_middleware(PyInstrumentProfilerMiddleware,server_app=app)
    
    opened by PY-GZKY 3
  • Question - I am not able to track every method under my fastapi endpoint

    Question - I am not able to track every method under my fastapi endpoint

    In the logs i just see the framework level tracing, where as i want to know it for every method under the endpoint call. Am i missing something or is that the expected output. this is how as per your example i am adding to the fastapi middleware. app.add_middleware(PyInstrumentProfilerMiddleware)

    opened by sandeeppilania 2
  • Output to html

    Output to html

    I am passing in the proper argument i believe but I can't find the html file

    app.add_middleware(PyInstrumentProfilerMiddleware, profiler_output_type='html')

    opened by Dirt-Nasty 1
  • What is the usage of

    What is the usage of "server_app"?

    class PyInstrumentProfilerMiddleware:
        def __init__(
            self,
            app: ASGIApp,
            *,
            server_app: Optional[Router] = None,
            profiler_interval: float = 0.0001,
            profiler_output_type: str = "text",
            is_print_each_request: bool = True,
            **profiler_kwargs,
        ):
            self.app = app
            self._profiler = Profiler(interval=profiler_interval)
            self._server_app = server_app
    

    It looks like the server_app is used nowhere, can you explain what it is?

    opened by kigawas 1
  • How you can save output to csv or log it in more readable way

    How you can save output to csv or log it in more readable way

    app = FastAPI() app.add_middleware(PyInstrumentProfilerMiddleware)

    Provides something like this image

    How we can get it in more readable way or save to html/logs , can you share an example ?

    opened by sandeeppilania 1
  • How to read/interpret the data of how much memory was consumed by the API?

    How to read/interpret the data of how much memory was consumed by the API?

    This is what the result looks like- image

    But I am unable to interpret this data, how do you know how much memory was consumed by the API from the above-mentioned data?

    opened by directlinks 1
  • Fix html mode; move PyInstrumentProfilerMiddleware to top-level module

    Fix html mode; move PyInstrumentProfilerMiddleware to top-level module

    Specifically:

    • Check for server_app variable to properly put shutdown handler
    • Add open_in_browser argument to open browser automatically

    Also, moved PyInstrumentProfilerMiddleware so you can just do:

    from fastapi_profiler import PyInstrumentProfilerMiddleware
    

    I bumped the version to 2.0.0 because of that last change because it is not backwards compatible, but let me know if it is too aggressive.

    opened by rgeoghegan 0
  • Profiling results are not saved in file after shutdown

    Profiling results are not saved in file after shutdown

    I'm testing the provided example with the following change:

    @@ -8,7 +8,7 @@ from fastapi_profiler.profiler_middleware import PyInstrumentProfilerMiddleware
     
     
     app = FastAPI()
    -app.add_middleware(PyInstrumentProfilerMiddleware)
    +app.add_middleware(PyInstrumentProfilerMiddleware, profiler_output_type="html", is_print_each_request=False, html_file_name="/tmp/out.html")
     
     
     @app.get("/test")
    

    And I'm running it as suggested (uvicorn==0.15.0) without errors:

    fastapi_profiler/example$ uvicorn fastapi_example:app --host="0.0.0.0" --port=8080
    INFO:     Started server process [3261]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
    INFO:     127.0.0.1:44126 - "GET /test HTTP/1.1" 200 OK
    ^CINFO:     Shutting down
    INFO:     Waiting for application shutdown.
    INFO:     Application shutdown complete.
    INFO:     Finished server process [3261]
    fastapi_profiler/example$ ls /tmp/out.html
    ls: cannot access '/tmp/out.html': No such file or directory
    

    However, nothing is saved into /tmp/out.html after . How can I fix this issue? Thanks

    opened by emilio-molina 3
  • Feature request: .prof output to interface with visualisation tool like snakeviz

    Feature request: .prof output to interface with visualisation tool like snakeviz

    Hello! Thanks for making this great project, I think this is the best profiler for Fast API applications out there.

    One feature that would be super useful is outputting the profiles in a format like .prof that can be used by visualisation tools like snakeviz. This is one of the outputs of Python's cProfile and implemented by werkzeug which Flask is built on. (cProfile docs, profiling in flask applications that produce .prof files)

    Adding the ability to visualise these profiles would really increase the impact of this tool!

    opened by weiann-tractable 0
Owner
LeoSun
Better and better. Like Python & Go forever!
LeoSun
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.

Supported tags and respective Dockerfile links python3.8, latest (Dockerfile) python3.7, (Dockerfile) python3.6 (Dockerfile) python3.8-slim (Dockerfil

Sebastián Ramírez 2.1k Dec 31, 2022
FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by django-admin, and has as many powerful functions as django-admin.

简体中文 | English 项目介绍 FastAPI-Amis-Admin fastapi-amis-admin是一个拥有高性能,高效率,易拓展的fastapi管理后台框架. 启发自Django-Admin,并且拥有不逊色于Django-Admin的强大功能. 源码 · 在线演示 · 文档 · 文

AmisAdmin 318 Dec 31, 2022
Starlette middleware for Prerender

Prerender Python Starlette Starlette middleware for Prerender Documentation: https://BeeMyDesk.github.io/prerender-python-starlette/ Source Code: http

BeeMyDesk 14 May 2, 2021
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

starlette context Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automat

Tomasz Wójcik 300 Dec 26, 2022
Starlette middleware for Prerender

Prerender Python Starlette Starlette middleware for Prerender Documentation: https://BeeMyDesk.github.io/prerender-python-starlette/ Source Code: http

BeeMyDesk 13 Jul 27, 2020
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

starlette context Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automat

Tomasz Wójcik 110 Feb 16, 2021
Dead simple CSRF security middleware for Starlette ⭐ and Fast API ⚡

csrf-starlette-fastapi Dead simple CSRF security middleware for Starlette ⭐ and Fast API ⚡ Will work with either a <input type="hidden"> field or ajax

Nathaniel Sabanski 9 Nov 20, 2022
FastAPI application and service structure for a more maintainable codebase

Abstracting FastAPI Services See this article for more information: https://camillovisini.com/article/abstracting-fastapi-services/ Poetry poetry inst

Camillo Visini 309 Jan 4, 2023
SuperSaaSFastAPI - Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAPI, Vue.js & Tailwind

Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAP

Rudy Bekker 31 Jan 10, 2023
High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).

fastapi-gino-arq-uvicorn High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (powered by Redis & PostgreSQL). Contents Get Star

Leo Sussan 351 Jan 4, 2023
:rocket: CLI tool for FastAPI. Generating new FastAPI projects & boilerplates made easy.

Project generator and manager for FastAPI. Source Code: View it on Github Features ?? Creates customizable project boilerplate. Creates customizable a

Yagiz Degirmenci 1k Jan 2, 2023
Simple FastAPI Example : Blog API using FastAPI : Beginner Friendly

fastapi_blog FastAPI : Simple Blog API with CRUD operation Steps to run the project: git clone https://github.com/mrAvi07/fastapi_blog.git cd fastapi-

Avinash Alanjkar 1 Oct 8, 2022
Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI

FastAPI Ariadne Example Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI - GitHub ###Запуск на локальном окру

ZeBrains Team 9 Nov 10, 2022
Sample-fastapi - A sample app using Fastapi that you can deploy on App Platform

Getting Started We provide a sample app using Fastapi that you can deploy on App

Erhan BÜTE 2 Jan 17, 2022
Flask-vs-FastAPI - Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks.

Flask-vs-FastAPI Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks. IntroductionIn Flask is a popular mic

Mithlesh Navlakhe 1 Jan 1, 2022
FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management

FastAPI Server-sided Session FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management.

DevGuyAhnaf 5 Dec 23, 2022
fastapi-admin2 is an upgraded fastapi-admin, that supports ORM dialects, true Dependency Injection and extendability

FastAPI2 Admin Introduction fastapi-admin2 is an upgraded fastapi-admin, that supports ORM dialects, true Dependency Injection and extendability. Now

Glib 14 Dec 5, 2022
Fastapi-ml-template - Fastapi ml template with python

FastAPI ML Template Run Web API Local $ sh run.sh # poetry run uvicorn app.mai

Yuki Okuda 29 Nov 20, 2022
A dynamic FastAPI router that automatically creates CRUD routes for your models

⚡ Create CRUD routes with lighting speed ⚡ A dynamic FastAPI router that automatically creates CRUD routes for your models

Adam Watkins 950 Jan 8, 2023