Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest profiling mostly reporting on pytest functions

In order to profile my code, I've installed pytest-profiling and run

$ pytest --profile

However, almost all of the results in the summary are related to functions in pytest itself:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      159    0.001    0.000    0.170    0.001 runner.py:108(pytest_runtest_protocol)
      159    0.001    0.000    0.143    0.001 runner.py:116(runtestprotocol)
...
2615/1749    0.015    0.000    0.131    0.000 _callers.py:9(_multicall)
...

Is there a way to get the profiler to ignore pytest's functions and get instead the stats for my package?

like image 434
Daniel Walker Avatar asked Oct 18 '25 03:10

Daniel Walker


1 Answers

There currently appears to be no option in the plugin to cutomize which function calls are shown (source: plugin repo).

However, you could write a custom script to display only the profiling results you're interested in (which is saved at ./prof/combined.prof).

import pstats

stats = pstats.Stats('./prof/combined.prof')
stats.print_stats("path/to/function_calls/you/want/to/show")

# Or alternatively
stats.print_stats("local_path", 20) # Only show 20 of the listings
stats.sort_stats('cumulative').print_stats('dir_name', 20) # Sort by cumulative time

You can then write a script (.sh, .bat, ...) which calls pytest --profile followed by the above code snippet.

For more information on pstats: https://docs.python.org/3/library/profile.html

like image 112
Pavloski Avatar answered Oct 20 '25 17:10

Pavloski