Skip to main content

pytest-line-profiler

PyPI version Python versions See Build Status on Travis CI

line-by-line profiling for code executed by pytest, using line-profiler.

Why?

Line profiler is a wonderful tool to easily identify bottlenecks inside specific functions of your code, and quantify the improvements after a refactor.

Using it is straightforward but required to instrument the functions you want to profile with a "virtual" @profile decorator and then execute "a trigger script" (code that calls the decorated functions somehow) via kernprof.py which works as a python wrapper that understands the decorator, register the functions to be profiled, and print the stats when the script finishes.

Altought it does its job, is a bit invasive: you need to have an special "instrumented" version of your code, and execute it in a way that potentially clashes with the way you do normally (for instance, through a shortcut command from your editor, a test runner, another script, etc.)

Moreover, frequently in real case scenarios, "a trigger script" isn't just a simple function call. You need to prepare input data, connect to external resources, etc. And that's exactly what a test can do, right?

Installation

You can install "pytest-line-profiler" via pip from PyPI:

$ pip install pytest-line-profiler

Usage

Mark your test passing the functions you wants to profile as positional arguments, like @pytest.mark.line_profile.with_args(function1, function2, [...])

If your test exercises any of those functions, you'll get the profile result as a report.

For example:

import pytest

def f(i):
    return i * 10

def g(n=10):
    return sum(f(i) for i in range(10))


@pytest.mark.line_profile.with_args(f, g)
def test_as_mark():
    assert g() == 450

After that test is executed, you'll get the stats from the line profiler instance.

============ Line Profile result for tests/test_line_profiler.py::test_as_mark ============
Timer unit: 1e-06 s

Total time: 4e-06 s
File: /home/tin/lab/pytest-line-profiler/tests/test_line_profiler.py
Function: f at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def f(i):
     5        10          4.0      0.4    100.0      return i * 10

Total time: 3e-05 s
File: /home/tin/lab/pytest-line-profiler/tests/test_line_profiler.py
Function: g at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def g(n=10):
     8         1         30.0     30.0    100.0      return sum(f(i) for i in range(10))

Alternatively, you can run any test passing the function/s to profile from the command line

$ pytest --line-profile path.to.function_to_be profiled [...] 

Contributing

Contributions are very welcome. Tests can be run with pytest, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-line-profiler" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Download files

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

Source Distribution

pytest-line-profiler-apn-0.1.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_line_profiler_apn-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-line-profiler-apn-0.1.1.tar.gz.

File metadata

File hashes

Hashes for pytest-line-profiler-apn-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7487bafa9ad5b1d53602b5ed821dbe1f7df82ddb419202f1db6856f98e7ec398
MD5 9bd9f38affc6059917db2763dd3517f4
BLAKE2b-256 3bc9a58d175aee49dd02c32f9c448f35d52178e7e16ed5ee60b214d1c893ba88

See more details on using hashes here.

File details

Details for the file pytest_line_profiler_apn-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_line_profiler_apn-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13cb88f540042f12e0c908f634d32428594c4327f3c02dbc070da8f8e6fdd612
MD5 a36f61fcb2d025fd35e6c81545719d96
BLAKE2b-256 2d384cd25b280483bc9a70c1966ac315cacd9650bc11798ea44fc86fb2a22573

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5

2 files

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page