Giter Club home page Giter Club logo

pytest-line-profiler's Introduction

pytest-line-profiler

tests PyPI version PyPI - Downloads

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

pytest-line-profiler's People

Contributors

dnouri avatar mgaitan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pytest-line-profiler's Issues

Profiling methods in other files?

the example shows how to get profiles of functions defined in the same test file, but what if i'm writing tests for a library and there's a particular function in my library i want to profile?

Profiling methods in other files?

the example shows how to get profiles of functions defined in the same test file, but what if i'm writing tests for a library and there's a particular function in my library i want to profile?

Functions called twice within tests when calling pytest (not using line profiling)

I noticed that when I have this package installed that each line in my tests are executed twice. For example

def test_simple():
    print("Hello")
    return 1

If I run the above test using pytest -k test_simple -s, I notice that "Hello" is printed twice. When I only have pytest installed then I get the expected behavior of "Hello" is printed once.

NameError: name '...' is not defined

Directly after installation my doctests don't find themself anymore and I have to explicitly import the corresponding module/package.
After uninstalling pytest-line-profiler the previous working behavior of doctest was restored.

Profiling class methods

When passing a function name to --line-profile, it appears to attempt to import it. This makes it impossible to profile a class method, which causes a not found or is not callable exception.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.