pytest-line-profiler


Namepytest-line-profiler JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/mgaitan/pytest-line-profiler
SummaryProfile code executed by pytest
upload_time2023-08-10 14:21:07
maintainerMartín Gaitán
docs_urlNone
authorMartín Gaitán
requires_python>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-line-profiler

[![tests](https://github.com/mgaitan/pytest-line-profiler/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/mgaitan/pytest-line-profiler/actions/workflows/test.yml)
[![PyPI version](https://img.shields.io/pypi/v/pytest-line-profiler)](https://pypi.org/project/pytest-line-profiler/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pytest-line-profiler)](https://libraries.io/pypi/pytest-line-profiler)


line-by-line profiling for code executed by pytest, using [line-profiler](https://github.com/pyutils/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:

```python
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.
  

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mgaitan/pytest-line-profiler",
    "name": "pytest-line-profiler",
    "maintainer": "Mart\u00edn Gait\u00e1n",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "gaitan@gmail.com",
    "keywords": "",
    "author": "Mart\u00edn Gait\u00e1n",
    "author_email": "gaitan@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0d/e0/c20039d9d74f4f4fc537801b0698d8e6141e4c9507e951aadbf40e36830e/pytest-line-profiler-0.2.1.tar.gz",
    "platform": null,
    "description": "# pytest-line-profiler\n\n[![tests](https://github.com/mgaitan/pytest-line-profiler/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/mgaitan/pytest-line-profiler/actions/workflows/test.yml)\n[![PyPI version](https://img.shields.io/pypi/v/pytest-line-profiler)](https://pypi.org/project/pytest-line-profiler/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/pytest-line-profiler)](https://libraries.io/pypi/pytest-line-profiler)\n\n\nline-by-line profiling for code executed by pytest, using [line-profiler](https://github.com/pyutils/line_profiler).\n\n## Why?\n\nLine profiler is a wonderful tool to easily identify bottlenecks inside specific functions of your code, and quantify the improvements after a refactor. \n\nUsing it is straightforward but required to instrument the functions you want to profile with a \"virtual\" `@profile` decorator\nand 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.   \n\nAltought it does its job, is a bit invasive: you need to have an special \"instrumented\" version of your code, \nand 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.)   \n\nMoreover, frequently in real case scenarios, \"a trigger script\" isn't just a simple function call. \nYou need to prepare input data, connect to external resources, etc.  And that's exactly what a test can do, right?    \n\n## Installation \n\nYou can install \"pytest-line-profiler\" via pip from PyPI.\n\n```\n$ pip install pytest-line-profiler\n```\n\n## Usage\n\n\nMark your test passing the functions you wants to profile as positional arguments, \nlike `@pytest.mark.line_profile.with_args(function1, function2, [...])`\n\nIf your test exercises any of those functions, you'll get the profile result as a report.  \n\nFor example:\n\n```python\nimport pytest\n\ndef f(i):\n    return i * 10\n\ndef g(n=10):\n    return sum(f(i) for i in range(10))\n\n\n@pytest.mark.line_profile.with_args(f, g)\ndef test_as_mark():\n    assert g() == 450\n\n```\n\n\nAfter that test is executed, you'll get the stats from the line profiler instance. \n\n```\n============ Line Profile result for tests/test_line_profiler.py::test_as_mark ============\nTimer unit: 1e-06 s\n\nTotal time: 4e-06 s\nFile: /home/tin/lab/pytest-line-profiler/tests/test_line_profiler.py\nFunction: f at line 4\n\nLine #      Hits         Time  Per Hit   % Time  Line Contents\n==============================================================\n     4                                           def f(i):\n     5        10          4.0      0.4    100.0      return i * 10\n\nTotal time: 3e-05 s\nFile: /home/tin/lab/pytest-line-profiler/tests/test_line_profiler.py\nFunction: g at line 7\n\nLine #      Hits         Time  Per Hit   % Time  Line Contents\n==============================================================\n     7                                           def g(n=10):\n     8         1         30.0     30.0    100.0      return sum(f(i) for i in range(10))\n```\n\n\nAlternatively, you can run any test passing the function/s to profile from the command line\n\n```\n$ pytest --line-profile path.to.function_to_be profiled [...] \n```\n\n\n## Contributing\n\nContributions are very welcome. Tests can be run with [pytest][https://github.com/pytest-dev/pytest], please\nensure the coverage at least stays the same before you submit a pull\nrequest.\n\n## License\n\nDistributed under the terms of the [MIT][http://opensource.org/licenses/MIT] license,\n\"pytest-line-profiler\" is free and open source software\n\n## Issues\n\nIf you encounter any problems, please [file an issue][https://github.com/mgaitan/pytest-line-profiler/issues] along with a\ndetailed description.\n  \n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Profile code executed by pytest",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://github.com/mgaitan/pytest-line-profiler"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6b783a38fefecfe0b1da8d1a626b69b3a1aa5b36ff4fc5eff8729d9fd8630aae",
                "md5": "d2ffa310ae9c6456fd4ea8c0930d37cb",
                "sha256": "3cfc38e729fb867ad5ab2674163dcc74eb3256ba77163c248781240851d28705"
            },
            "downloads": -1,
            "filename": "pytest_line_profiler-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d2ffa310ae9c6456fd4ea8c0930d37cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5119,
            "upload_time": "2023-08-10T14:21:05",
            "upload_time_iso_8601": "2023-08-10T14:21:05.936715Z",
            "url": "https://files.pythonhosted.org/packages/6b/78/3a38fefecfe0b1da8d1a626b69b3a1aa5b36ff4fc5eff8729d9fd8630aae/pytest_line_profiler-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0de0c20039d9d74f4f4fc537801b0698d8e6141e4c9507e951aadbf40e36830e",
                "md5": "c56b8717d18faa192115740fc0b96d83",
                "sha256": "a3d00809c322ea58e0f1701643d7ad16f2cd3133db4d9f5a4893122cc1593f77"
            },
            "downloads": -1,
            "filename": "pytest-line-profiler-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c56b8717d18faa192115740fc0b96d83",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 4891,
            "upload_time": "2023-08-10T14:21:07",
            "upload_time_iso_8601": "2023-08-10T14:21:07.291846Z",
            "url": "https://files.pythonhosted.org/packages/0d/e0/c20039d9d74f4f4fc537801b0698d8e6141e4c9507e951aadbf40e36830e/pytest-line-profiler-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-10 14:21:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mgaitan",
    "github_project": "pytest-line-profiler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pytest-line-profiler"
}
        
Elapsed time: 0.10116s