为了分析我的代码,我安装了pytest-profiling并运行
$ pytest --profile然而,摘要中的几乎所有结果都与pytest本身的功能有关:
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)
...有没有办法让分析器忽略pytest的函数,而得到我的包的统计数据呢?
发布于 2022-11-05 04:47:57
目前,插件中似乎没有任何选项可以对显示的函数调用进行裁剪(源:插件回购)。
但是,您可以编写一个自定义脚本,只显示您感兴趣的分析结果(它保存在./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然后您可以编写一个脚本(.sh,.bat,.)它调用pytest --profile,后面跟着上面的代码片段。
有关pstats的更多信息:https://docs.python.org/3/library/profile.html
https://stackoverflow.com/questions/74198259
复制相似问题