Skip to main content

WSGI middleware for line-by-line profiling

Project description

PyPI version Build Status

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

wsgi_lineprof shows results of line-by-line profiling per request. You can use this project with many WSGI-compatible applications and frameworks:

  • Django

  • Pyramid

  • Flask

  • Bottle

  • etc…

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.

The results are outputted to stdout by default. You can see the results like this:

... (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.3

  • Python 3.4

  • Python 3.5

  • Python 3.6 (beta)

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

Stream

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

with open("lineprof.log", "w") as f:
    app = LineProfilerMiddleware(app, stream=f)
    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.2.0.tar.gz (32.4 kB view details)

Uploaded Source

Built Distribution

wsgi_lineprof-0.2.0-cp35-cp35m-macosx_10_12_x86_64.whl (23.3 kB view details)

Uploaded CPython 3.5m macOS 10.12+ x86-64

File details

Details for the file wsgi_lineprof-0.2.0.tar.gz.

File metadata

File hashes

Hashes for wsgi_lineprof-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0735aafb2515c853d567a41532e1c7485e14da9d54b5dd93da1da21350877c4c
MD5 9685330b1fba8744592cac1224454cb4
BLAKE2b-256 38793d56a983e7d80f279a975b1a262386f5cb49c5a190de813e29479e65a441

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.2.0-cp35-cp35m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.2.0-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0b14fbca4ebd53da0050513cd903aa04e722bb7183fe6661b17be3a8efad2209
MD5 d1e0108131656069f0486fc5f9aecc10
BLAKE2b-256 d0d2c77d04c01afc68b94773b28b0207b68ae3391cd46e339c9c8d607c4c8d49

See more details on using hashes here.

Provenance

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