Coder Social home page Coder Social logo

py-profiler's Introduction

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:

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
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
  1. Integrate with Flask
  • If you are using Flask to implement your Restful API. You can add profiler_blueprint to your Flash app.

E.g:

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

  1. Integrate with 3rd restful library.

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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.