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

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 some useful filters in wsgi_lineprof.filters.

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.3.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distributions

wsgi_lineprof-0.3.0-cp36-cp36m-manylinux1_x86_64.whl (72.5 kB view details)

Uploaded CPython 3.6m

wsgi_lineprof-0.3.0-cp36-cp36m-macosx_10_12_x86_64.whl (23.8 kB view details)

Uploaded CPython 3.6m macOS 10.12+ x86-64

wsgi_lineprof-0.3.0-cp35-cp35m-manylinux1_x86_64.whl (72.3 kB view details)

Uploaded CPython 3.5m

wsgi_lineprof-0.3.0-cp35-cp35m-macosx_10_12_x86_64.whl (23.8 kB view details)

Uploaded CPython 3.5m macOS 10.12+ x86-64

wsgi_lineprof-0.3.0-cp34-cp34m-manylinux1_x86_64.whl (72.9 kB view details)

Uploaded CPython 3.4m

wsgi_lineprof-0.3.0-cp34-cp34m-macosx_10_12_x86_64.whl (23.9 kB view details)

Uploaded CPython 3.4m macOS 10.12+ x86-64

wsgi_lineprof-0.3.0-cp33-cp33m-manylinux1_x86_64.whl (69.6 kB view details)

Uploaded CPython 3.3m

wsgi_lineprof-0.3.0-cp33-cp33m-macosx_10_10_x86_64.whl (23.9 kB view details)

Uploaded CPython 3.3m macOS 10.10+ x86-64

wsgi_lineprof-0.3.0-cp27-cp27mu-manylinux1_x86_64.whl (67.1 kB view details)

Uploaded CPython 2.7mu

wsgi_lineprof-0.3.0-cp27-cp27m-manylinux1_x86_64.whl (67.1 kB view details)

Uploaded CPython 2.7m

wsgi_lineprof-0.3.0-cp27-cp27m-macosx_10_12_x86_64.whl (23.4 kB view details)

Uploaded CPython 2.7m macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0.tar.gz
Algorithm Hash digest
SHA256 fee478117e253bc67d180a5af25606813fd0fc1e01f8c8f71f95a769feefc079
MD5 080fba289ad9cb3522679c061c45116f
BLAKE2b-256 e95e95064d1d3918547d9fde21e37cde6704c1d14af158c33deb98b9abeeeca2

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0ca21e363f9452d484b537138ceef07b6a1fef404751d2cf6b72ed9fb909441a
MD5 df060cb82cfcf97e096c4c9a3841268f
BLAKE2b-256 b47dc95b9738d36eb3471b8d675970fb6c92d74a8ab4e3e4cc908ccb4d8a55d0

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp36-cp36m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 45b623034ee60c8a727553653630ba7e258eacddef5096ee2d073f3219b69118
MD5 619aa8c4b8405d6c8f4ecb6689a37fd8
BLAKE2b-256 dbd3407899ce8c4bca332430f93dd34d00a4dd2479275b679574b19cb8f47a60

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f951b46492ca52b2a8053d4c2aa67ecf0fbcb4433895aae6c54106a06067ea02
MD5 2b7ffc74515fe901dd0942a78c302f26
BLAKE2b-256 8c417adbe887d3815af09c95af9b57877f8fe489439dbda3f0728cca738b5704

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d003a99db7335443e6caec4063a4ef2aea30983a58c9248f60111ab67070de0c
MD5 733014114ef7e6cf8c4fe4cecc8ec50d
BLAKE2b-256 90a051c0e2867ebe892cfdaf3021bb5c6ee764654b7193942e4f8c6bcdecd4c2

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0a7b14b14d435d125445b401e8bfb8ed5cde69b545f1d64974741eb5131bbedc
MD5 df22e3cdfa42f8629aa9eafc66b4db70
BLAKE2b-256 a3a9449c7f3d5188bc9d483f5bd979a8476ab27ca3ffbd832d111369dbe7ebd5

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp34-cp34m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp34-cp34m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5667d05e19b17f345015a93b268c49fdeb2e27e9348e495972abaa5723075ec7
MD5 0868189e0942999134f588eb7abb404d
BLAKE2b-256 e5a96f2c27c955b3eed3096ed11c87d8a8489460cffb73e3eb9b4e9433c669b3

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b66e03677a9ad24ff30255e1ff177df788a2c77302a89c66fdcbef1a707bbefe
MD5 9ef6d5a495ea38841ceaf1a985e7ae31
BLAKE2b-256 2e379c2aca075fcf9ae8610ec0c8948dd77b663ca70390111375bce0d3a33954

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp33-cp33m-macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp33-cp33m-macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 ca4d202dc57ca7f59978e1fac314bc87a3fde859bbd827312ba6c205db71edb7
MD5 c90a0ff7a19747c1b22fa2580663ac20
BLAKE2b-256 6a72b0536600c5ffc8777470e64550d55b2ab2f102c5bbe777ac1271c0a7acee

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 555a12f7775720deed13f453ebe2dbaf4f00236c55b475e8a92a4cbf028a3e40
MD5 32e8b75d8d5977b92da58648da521279
BLAKE2b-256 8fe48e39215b8e5d5fc3ce4b3c593c7cc463588abe7a1f7af2667fa211b52025

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9ce0e09cb1f54c63eb394ec7a35719947ec893b9db08fbe246352cc0a03f4191
MD5 d09d059d0439609b15f8e252c55267ff
BLAKE2b-256 d82a3dbe75e2de52e19ea7a2cc8bc7596fba4ce4ed8b40fb92c7ba039d06145b

See more details on using hashes here.

Provenance

File details

Details for the file wsgi_lineprof-0.3.0-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for wsgi_lineprof-0.3.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ead38e2ae97a83b50df02543623309089160a346d489e85fec99e7bc1db803a8
MD5 709bed7e52473631823c93694d17612d
BLAKE2b-256 dfd918719cd2355e1f3197d940c0a90334c7ae5ffda79d0d88f3c25c3ba19172

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