py-profiler


Namepy-profiler JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/andy1xx8/py-profiler
SummaryA library to measure your method, function execution time.
upload_time2024-05-08 18:15:07
maintainerNone
docs_urlNone
authorAndy Le
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements flask waitress jinja2 beautifultable
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PY Profiler

A library to measure method, function or your restful api execution time.

## Install

- Run `pip install py-profiler` or `pip3 install py-profiler` to install this library

## Usage

It comes with a really easy api to use, you can add `profiler(name = None)` decorator to any method or function you want
to measure its execution time.

E.g:

```python
from py_profiler import profiler, profiling_service


@profiler('hello')
def hello():
    print('hello')


class Foo:

    @profiler('Food.some_thing')
    def some_thing(self):
        print('some_thing')

    # By default, profiler name is f'{class_name}.{method_name}'
    @profiler()
    def method_2(self):
        print('method_2')

```

## Access Profiler

There are 3 ways to access the profiler:

    - View as a raw table in the console.
    
    - View as a HTML page using a provided Flask blueprint.
    
    - Integrate with your own RESTFul framework.

- Exec time is in milliseconds

1. **View as a table**

```python
from py_profiler import profiling_service

print(profiling_service.as_table())
```

| No | Name                           | Total Req  | Pending Req  | Total Exec Time | Last Exec Time  | Highest Exec Time | Request Rate (req/sec) | Avg Time/Request (millis/req) |
|----|--------------------------------|--------|----------|------------|------------|------------|------------|------------|
| 1  | Foo.method_2                   |   1    |    0     |   0.014    |   0.014    |   0.014    | 71428.571  |   0.014    |
| 2  | Food.some_thing                |   1    |    0     |   0.011    |   0.011    |   0.011    | 90909.091  |   0.011    |
| 3  | hello                          |   1    |    0     |   0.031    |   0.031    |   0.031    | 32258.065  |   0.031    |

2. **Integrate with FastAPI**

- If you are using FastAPI to implement your Restful API. You can add `profiler_router` to your FastAPI app.

E.g:

```python
from fastapi import FastAPI
from py_profiler.fastapi_profiler_controller import profiler_router

app = FastAPI()
app.include_router(profiler_router)

```

Then you can access the profiler page at: `http://127.0.0.1:8080/profiler`
![Py Profiler Page](https://github.com/andy1xx8/py-profiler/blob/master/sample.png?raw=true)

3. **Integrate with Flask**

- If you are using Flask to implement your Restful API. You can add `profiler_blueprint` to your Flash app.

E.g:

```python
from flask import Flask
from waitress import serve
from py_profiler.profiler_controller import profiler_blueprint

app = Flask(__name__)
app.register_blueprint(profiler_blueprint)

serve(
    app,
    host="0.0.0.0",
    port=8080
)
```

Then you can access the profiler page at: `http://127.0.0.1:8080/profiler`
![Py Profiler Page](https://github.com/andy1xx8/py-profiler/blob/master/sample.png?raw=true)


4. **Integrate with 3rd restful library**.

You can build your custom profiler viewer by using `as_html()`

```python
from py_profiler import profiling_service

html_page = profiling_service.as_html()
```

Then, you can implement a html page and return this `html_page` to your client to see profiler viewer.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/andy1xx8/py-profiler",
    "name": "py-profiler",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Andy Le",
    "author_email": "tauit.dnmd@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/05/b9/f492f752229834ea6b07fb0d9310a58d7e131dff02a2bd3dcfe21c162ff1/py_profiler-0.3.1.tar.gz",
    "platform": null,
    "description": "# PY Profiler\n\nA library to measure method, function or your restful api execution time.\n\n## Install\n\n- Run `pip install py-profiler` or `pip3 install py-profiler` to install this library\n\n## Usage\n\nIt comes with a really easy api to use, you can add `profiler(name = None)` decorator to any method or function you want\nto measure its execution time.\n\nE.g:\n\n```python\nfrom py_profiler import profiler, profiling_service\n\n\n@profiler('hello')\ndef hello():\n    print('hello')\n\n\nclass Foo:\n\n    @profiler('Food.some_thing')\n    def some_thing(self):\n        print('some_thing')\n\n    # By default, profiler name is f'{class_name}.{method_name}'\n    @profiler()\n    def method_2(self):\n        print('method_2')\n\n```\n\n## Access Profiler\n\nThere are 3 ways to access the profiler:\n\n    - View as a raw table in the console.\n    \n    - View as a HTML page using a provided Flask blueprint.\n    \n    - Integrate with your own RESTFul framework.\n\n- Exec time is in milliseconds\n\n1. **View as a table**\n\n```python\nfrom py_profiler import profiling_service\n\nprint(profiling_service.as_table())\n```\n\n| No | Name                           | Total Req  | Pending Req  | Total Exec Time | Last Exec Time  | Highest Exec Time | Request Rate (req/sec) | Avg Time/Request (millis/req) |\n|----|--------------------------------|--------|----------|------------|------------|------------|------------|------------|\n| 1  | Foo.method_2                   |   1    |    0     |   0.014    |   0.014    |   0.014    | 71428.571  |   0.014    |\n| 2  | Food.some_thing                |   1    |    0     |   0.011    |   0.011    |   0.011    | 90909.091  |   0.011    |\n| 3  | hello                          |   1    |    0     |   0.031    |   0.031    |   0.031    | 32258.065  |   0.031    |\n\n2. **Integrate with FastAPI**\n\n- If you are using FastAPI to implement your Restful API. You can add `profiler_router` to your FastAPI app.\n\nE.g:\n\n```python\nfrom fastapi import FastAPI\nfrom py_profiler.fastapi_profiler_controller import profiler_router\n\napp = FastAPI()\napp.include_router(profiler_router)\n\n```\n\nThen you can access the profiler page at: `http://127.0.0.1:8080/profiler`\n![Py Profiler Page](https://github.com/andy1xx8/py-profiler/blob/master/sample.png?raw=true)\n\n3. **Integrate with Flask**\n\n- If you are using Flask to implement your Restful API. You can add `profiler_blueprint` to your Flash app.\n\nE.g:\n\n```python\nfrom flask import Flask\nfrom waitress import serve\nfrom py_profiler.profiler_controller import profiler_blueprint\n\napp = Flask(__name__)\napp.register_blueprint(profiler_blueprint)\n\nserve(\n    app,\n    host=\"0.0.0.0\",\n    port=8080\n)\n```\n\nThen you can access the profiler page at: `http://127.0.0.1:8080/profiler`\n![Py Profiler Page](https://github.com/andy1xx8/py-profiler/blob/master/sample.png?raw=true)\n\n\n4. **Integrate with 3rd restful library**.\n\nYou can build your custom profiler viewer by using `as_html()`\n\n```python\nfrom py_profiler import profiling_service\n\nhtml_page = profiling_service.as_html()\n```\n\nThen, you can implement a html page and return this `html_page` to your client to see profiler viewer.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A library to measure your method, function execution time.",
    "version": "0.3.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/andy1xx8/py-profiler/issues",
        "Homepage": "https://github.com/andy1xx8/py-profiler"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e3ced9dcea5e948c4281fb142d3eb0e09da8b4a82d017f6a0ccafca184e85a1f",
                "md5": "f51973783e1ef15e52dba1c20ac06b39",
                "sha256": "62a3101fa45f2d7b37abfc706e3820c0137a36e6474139126631964ef3ea2cdb"
            },
            "downloads": -1,
            "filename": "py_profiler-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f51973783e1ef15e52dba1c20ac06b39",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8246,
            "upload_time": "2024-05-08T18:15:05",
            "upload_time_iso_8601": "2024-05-08T18:15:05.751024Z",
            "url": "https://files.pythonhosted.org/packages/e3/ce/d9dcea5e948c4281fb142d3eb0e09da8b4a82d017f6a0ccafca184e85a1f/py_profiler-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "05b9f492f752229834ea6b07fb0d9310a58d7e131dff02a2bd3dcfe21c162ff1",
                "md5": "4132146fdd4db70922d4bc0a1cd68088",
                "sha256": "cc3b7a3f8c04c17f9776a03878a9ed47d243f84102c7dfa54016c8a69b86c427"
            },
            "downloads": -1,
            "filename": "py_profiler-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "4132146fdd4db70922d4bc0a1cd68088",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 8039,
            "upload_time": "2024-05-08T18:15:07",
            "upload_time_iso_8601": "2024-05-08T18:15:07.916653Z",
            "url": "https://files.pythonhosted.org/packages/05/b9/f492f752229834ea6b07fb0d9310a58d7e131dff02a2bd3dcfe21c162ff1/py_profiler-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-08 18:15:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "andy1xx8",
    "github_project": "py-profiler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "flask",
            "specs": []
        },
        {
            "name": "waitress",
            "specs": []
        },
        {
            "name": "jinja2",
            "specs": []
        },
        {
            "name": "beautifultable",
            "specs": []
        }
    ],
    "lcname": "py-profiler"
}
        
Elapsed time: 0.36225s