2

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?

Pavel Komarov
  • 1,153
  • 12
  • 26

2 Answers2

1

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 _
georgexsh
  • 15,984
  • 2
  • 37
  • 62
  • A modification of this is working: import line_profiler, then declare a static profiler = line_profiler.LineProfiler for the class. Then decorate functions with @profiler. Then call ClassName.profiler.dump_stats('method.lprof') at the ends of those functions. Then read them with python -m line_profiler method.lprof. But isn't this goofy? I don't want to add that many lines to my code that I'll have to remove later. Is there a cleaner way? – Pavel Komarov Oct 03 '17 at 22:12
  • which part is not clean? – georgexsh Oct 04 '17 at 03:06
  • For memory_profiler I don't need to instantiate an object or worry about calling its dump_stats method some time after a profiled function has been run. I can work with this method of time profiling, but it's more work than it needs to be. – Pavel Komarov Oct 04 '17 at 14:14
  • @pvlkmrv you can always wrap around, I've written an example above. – georgexsh Oct 04 '17 at 18:53
0

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.

tin_nqn
  • 106
  • 4