How can I time-profile unittests in Python?

2.6k Views Asked by At

I have a test suite and code it is testing. If I put from memory_profiler import profile at the tops of the appropriate files, decorate the functions I want profiled with @profile, and run in the standard way with python TestThing.py, I get great line-by-line results.

But line_profiler doesn't come in a package this way, and the only way I have been able to profile anything with it is with kernprof -l -v thing.py. If I use it on my unit tests, none of the tests are run (unsurprising, really), and no results are generated. How can I time-profile my tests and the code they use in a way analogous to memory_profiler?

2

There are 2 best solutions below

4
On BEST ANSWER

of course, you could import line_profiler:

import line_profiler

profiler = line_profiler.LineProfiler()


@profiler
def foo():
    for i in range(10):
        print(i)


foo()

profiler.dump_stats('foo.lprof')

result stored in foo.lprof.

or, you could wrap a memory_profiler like decorator, print result after calling:

def profile_and_show(func):
    profiler = line_profiler.LineProfiler(func)
    def _(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        finally:
            lstats = profiler.get_stats()
            line_profiler.show_text(lstats.timings, lstats.unit)
    return _
0
On

Please check pytest-line-profiler.

You only need to decorate (mark) one or more tests with the functions to be profiled, and will get the line-by-line report at the end.

PS: I'm the author of this plugin and I'd really appreciate your feedback.