pytest-line-profiler
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][https://github.com/pytest-dev/pytest], please ensure the coverage at least stays the same before you submit a pull request.
License
Distributed under the terms of the [MIT][http://opensource.org/licenses/MIT] license, "pytest-line-profiler" is free and open source software
Issues
If you encounter any problems, please [file an issue][https://github.com/mgaitan/pytest-line-profiler/issues] along with a detailed description.
Metadata
Release files for pytest-line-profiler 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest-line-profiler-0.2.1.tar.gz | 4.9 kB | Without provenance | Details for pytest-line-profiler-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_line_profiler-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for pytest_line_profiler-0.2.1-py3-none-any.whl |
Total release size: 10.0 kB
Release files / pytest-line-profiler-0.2.1.tar.gz
| Download URL | pytest-line-profiler-0.2.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a3d00809c322ea58e0f1701643d7ad16f2cd3133db4d9f5a4893122cc1593f77
|
|
BLAKE2b-256 checksum How to use checksums |
0de0c20039d9d74f4f4fc537801b0698d8e6141e4c9507e951aadbf40e36830e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / pytest_line_profiler-0.2.1-py3-none-any.whl
| Download URL | pytest_line_profiler-0.2.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3cfc38e729fb867ad5ab2674163dcc74eb3256ba77163c248781240851d28705
|
|
BLAKE2b-256 checksum How to use checksums |
6b783a38fefecfe0b1da8d1a626b69b3a1aa5b36ff4fc5eff8729d9fd8630aae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for pytest-line-profiler 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.