Skip to main content

WSGI middleware for line-by-line profiling

Project description

PyPI version PyPI Supported Python Versions Build Status AppVeyor Build Status

wsgi_lineprof is a WSGI middleware for line-by-line profiling.

wsgi_lineprof has the following features:

  • WSGI middleware: It can be integrated with any WSGI-compatible applications and frameworks including Django, Pyramid, Flask, Bottle, and more.

  • Easily pluggable: All configurations for profiling in one place. Users don’t need to make any changes to their application.

wsgi_lineprof is not recommended to be used in production environment because of the overhead of profiling.

At a Glance

You can use wsgi_lineprof as a WSGI middleware of existing applications.

$ pip install wsgi_lineprof

Example usage with Bottle:

import time

import bottle
from wsgi_lineprof.middleware import LineProfilerMiddleware

app = bottle.default_app()


@app.route('/')
def index():
    time.sleep(1)
    return "Hello world!!"

if __name__ == "__main__":
    # Add wsgi_lineprof as a WSGI middleware!
    app = LineProfilerMiddleware(app)
    bottle.run(app=app)

Run the above script to start web server, then access http://127.0.0.1:8080.

wsgi_lineprof writes results to stdout every time an HTTP request is processed by default. You can see the output like this in your console:

... (snip) ...

File: ./app.py
Name: index
Total time: 1.00518 [sec]
  Line      Hits         Time  Code
===================================
     9                         @app.route('/')
    10                         def index():
    11         1      1005175      time.sleep(1)
    12         1            4      return "Hello world!!"

... (snip) ...

Results contain many other functions, you can remove unnecessary results by using filters.

Requirements

  • Python 2.7

  • Python 3.4

  • Python 3.5

  • Python 3.6

  • Python 3.7

Filters

You can get results from specific files or sort results by using filters. For example, use FilenameFilter to filter results with filename and use TotalTimeSorter to sort results by total_time.

import time

import bottle
from wsgi_lineprof.filters import FilenameFilter, TotalTimeSorter
from wsgi_lineprof.middleware import LineProfilerMiddleware

app = bottle.default_app()


def get_name():
    # Get some data...
    time.sleep(1)
    return "Monty Python"

@app.route('/')
def index():
    name = get_name()
    return "Hello, {}!!".format(name)

if __name__ == "__main__":
    filters = [
        # Results which filename contains "app2.py"
        FilenameFilter("app2.py"),
        # Sort by total time of results
        TotalTimeSorter(),
    ]
    # Add wsgi_lineprof as a WSGI middleware
    app = LineProfilerMiddleware(app, filters=filters)

    bottle.run(app=app)

Run the above script to start web server, then access http://127.0.0.1:8080. You can see results in stdout.

$ ./app2.py
Bottle v0.12.10 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8080/
Hit Ctrl-C to quit.

Time unit: 1e-06 [sec]

File: ./app2.py
Name: index
Total time: 1.00526 [sec]
  Line      Hits         Time  Code
===================================
    15                         @app.route('/')
    16                         def index():
    17         1      1005250      name = get_name()
    18         1           11      return "Hello, {}!!".format(name)

File: ./app2.py
Name: get_name
Total time: 1.00523 [sec]
  Line      Hits         Time  Code
===================================
    10                         def get_name():
    11                             # Get some data...
    12         1      1005226      time.sleep(1)
    13         1            4      return "Monty Python"

127.0.0.1 - - [30/Nov/2016 17:21:12] "GET / HTTP/1.1" 200 21

There are more useful filters in wsgi_lineprof.filters. Examples:

  • FilenameFilter("(file1|file2).py", regex=True)

  • NameFilter("(fun1|fun2).py", regex=True)

Stream

By using stream option, you can output results to a file. For example, you can output logs to lineprof.log.

f = open("lineprof.log", "w")
app = LineProfilerMiddleware(app, stream=f)
bottle.run(app=app)

Async Stream

By using async_stream option, wsgi_lineprof starts a new thread for writing results. This option is useful when you do not want the main thread blocked for writing results.

# Start a new thread for writing results
app = LineProfilerMiddleware(app, async_stream=True)
bottle.run(app=app)

Accumulate Mode

By default, wsgi_lineprof writes results every time a request is processed. By enabling accumulate option, wsgi_lineprof accumulate results of all requests and writes the result on interpreter termination.

app = LineProfilerMiddleware(app, accumulate=True)
bottle.run(app=app)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wsgi_lineprof-0.7.0.tar.gz (58.4 kB view hashes)

Uploaded Source

Built Distributions

wsgi_lineprof-0.7.0-cp37-cp37m-win_amd64.whl (40.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

wsgi_lineprof-0.7.0-cp37-cp37m-win32.whl (36.1 kB view hashes)

Uploaded CPython 3.7m Windows x86

wsgi_lineprof-0.7.0-cp37-cp37m-manylinux1_x86_64.whl (127.5 kB view hashes)

Uploaded CPython 3.7m

wsgi_lineprof-0.7.0-cp37-cp37m-manylinux1_i686.whl (118.3 kB view hashes)

Uploaded CPython 3.7m

wsgi_lineprof-0.7.0-cp37-cp37m-macosx_10_14_x86_64.macosx_10_13_x86_64.macosx_10_12_x86_64.macosx_10_11_x86_64.macosx_10_10_x86_64.whl (39.4 kB view hashes)

Uploaded CPython 3.7m macOS 10.10+ x86-64 macOS 10.11+ x86-64 macOS 10.12+ x86-64 macOS 10.13+ x86-64 macOS 10.14+ x86-64

wsgi_lineprof-0.7.0-cp36-cp36m-win_amd64.whl (40.3 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

wsgi_lineprof-0.7.0-cp36-cp36m-win32.whl (36.1 kB view hashes)

Uploaded CPython 3.6m Windows x86

wsgi_lineprof-0.7.0-cp36-cp36m-manylinux1_x86_64.whl (127.5 kB view hashes)

Uploaded CPython 3.6m

wsgi_lineprof-0.7.0-cp36-cp36m-manylinux1_i686.whl (118.4 kB view hashes)

Uploaded CPython 3.6m

wsgi_lineprof-0.7.0-cp36-cp36m-macosx_10_13_x86_64.macosx_10_14_x86_64.macosx_10_12_x86_64.macosx_10_11_x86_64.macosx_10_10_x86_64.whl (39.5 kB view hashes)

Uploaded CPython 3.6m macOS 10.10+ x86-64 macOS 10.11+ x86-64 macOS 10.12+ x86-64 macOS 10.13+ x86-64 macOS 10.14+ x86-64

wsgi_lineprof-0.7.0-cp35-cp35m-win_amd64.whl (39.8 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

wsgi_lineprof-0.7.0-cp35-cp35m-win32.whl (35.8 kB view hashes)

Uploaded CPython 3.5m Windows x86

wsgi_lineprof-0.7.0-cp35-cp35m-manylinux1_x86_64.whl (125.3 kB view hashes)

Uploaded CPython 3.5m

wsgi_lineprof-0.7.0-cp35-cp35m-manylinux1_i686.whl (115.7 kB view hashes)

Uploaded CPython 3.5m

wsgi_lineprof-0.7.0-cp35-cp35m-macosx_10_13_x86_64.macosx_10_14_x86_64.macosx_10_12_x86_64.macosx_10_11_x86_64.macosx_10_10_x86_64.whl (38.9 kB view hashes)

Uploaded CPython 3.5m macOS 10.10+ x86-64 macOS 10.11+ x86-64 macOS 10.12+ x86-64 macOS 10.13+ x86-64 macOS 10.14+ x86-64

wsgi_lineprof-0.7.0-cp34-cp34m-win_amd64.whl (38.4 kB view hashes)

Uploaded CPython 3.4m Windows x86-64

wsgi_lineprof-0.7.0-cp34-cp34m-win32.whl (35.0 kB view hashes)

Uploaded CPython 3.4m Windows x86

wsgi_lineprof-0.7.0-cp34-cp34m-manylinux1_x86_64.whl (124.0 kB view hashes)

Uploaded CPython 3.4m

wsgi_lineprof-0.7.0-cp34-cp34m-manylinux1_i686.whl (115.3 kB view hashes)

Uploaded CPython 3.4m

wsgi_lineprof-0.7.0-cp34-cp34m-macosx_10_13_x86_64.macosx_10_14_x86_64.macosx_10_12_x86_64.macosx_10_11_x86_64.macosx_10_10_x86_64.whl (38.4 kB view hashes)

Uploaded CPython 3.4m macOS 10.10+ x86-64 macOS 10.11+ x86-64 macOS 10.12+ x86-64 macOS 10.13+ x86-64 macOS 10.14+ x86-64

wsgi_lineprof-0.7.0-cp27-cp27mu-manylinux1_x86_64.whl (113.3 kB view hashes)

Uploaded CPython 2.7mu

wsgi_lineprof-0.7.0-cp27-cp27mu-manylinux1_i686.whl (105.1 kB view hashes)

Uploaded CPython 2.7mu

wsgi_lineprof-0.7.0-cp27-cp27m-manylinux1_x86_64.whl (113.3 kB view hashes)

Uploaded CPython 2.7m

wsgi_lineprof-0.7.0-cp27-cp27m-manylinux1_i686.whl (105.1 kB view hashes)

Uploaded CPython 2.7m

wsgi_lineprof-0.7.0-cp27-cp27m-macosx_10_14_x86_64.macosx_10_13_x86_64.macosx_10_12_x86_64.macosx_10_11_x86_64.macosx_10_10_x86_64.whl (38.5 kB view hashes)

Uploaded CPython 2.7m macOS 10.10+ x86-64 macOS 10.11+ x86-64 macOS 10.12+ x86-64 macOS 10.13+ x86-64 macOS 10.14+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page