如何使用带有nosetest的cProfile --with-profile?

问题描述 投票:11回答:5

nosetest --with-profile --profile-stats-file输出

runsnake无法读取输出,因为nosetest使用hotshot,如果我想生成一个可以使用runsnake读取的文件,我需要将其转换为:

st = hotshot.stats.load('output')

st.dump_stats( 'output_new')

我可以直接使用cProfile运行测试以使用runsnake进行读取吗?

python nose cprofile
5个回答
9
投票

根据@squid的答案,你可以使用一个名为nose-cprof的鼻子插件用cProfile替换鼻子默认探查器hotshot。

要安装它:

pip install nose-cprof

然后像这样打电话给鼻子:

nosetests --with-cprofile

它应该生成一个cProfile输出文件,然后您可以使用runsnakerun等工具进行分析。


3
投票

@ cihanpesend的回答对我来说不太有用(cProfile找不到'nosetests'),但我确实在Linux上取得了成功:

python -m cProfile -o profile.out `which nosetests` .

结果输出在runsnake中正常工作。

(据推测,在Windows上,您可以将which nosetests替换为您的nosetests顶级python脚本的硬编码路径。)

我认为你是对的,鼻子测试'hotshot profiler的输出与runsnake不兼容。当然,对于我来说,这两个人并没有一起打得很漂亮。


2
投票

我没有关于nosetest的信息,除了它是python项目。所以;

python -m cProfile -o outputfile nosetest

然后,

runsnake outputfile

RunSnakeRun对于可视化分析器非常有用。

注意:要运行runsnake,必须安装wx和numpy。

更新:来自omikron的评论; runsnakerun不能支持python3配置文件输出。 (我没试过)


1
投票

或者你可以试试nose-cprof插件:https://github.com/msherry/nose-cprof

它用cProfile取代了hotshot


0
投票

使用pyprof2calltree

$ pip install pyprof2calltree
$ nosetests --with-cprofile --profile-stats=profile.out tests/
$ pyprof2calltree -i profile.out -k

使用xdot

$ sudo apt install xdot
$ gprof2dot -f pstats profile.out | dot -Tpng -o profile.png
© www.soinside.com 2019 - 2024. All rights reserved.