【问题标题】:How to use cProfile with nosetest --with-profile?如何将 cProfile 与 nosetest --with-profile 一起使用?
【发布时间】:2012-08-27 12:27:05
【问题描述】:

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

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

st = hotshot.stats.load('输出')

st.dump_stats('output_new')

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

【问题讨论】:

标签: python nose cprofile


【解决方案1】:

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

python -m cProfile -o outputfile nosetest

那么,

runsnake outputfile

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

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

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

【讨论】:

  • 需要注意的是,runsnakerun 在分析 Python 3 代码时不起作用,并且似乎不再维护。相反,我建议使用 pyprof2calltreekcachegrind
【解决方案2】:

@cihanpesend 的回答对我来说不太奏效(cProfile 找不到 'nosetests'),但我确实在 Linux 上使用:

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

生成的输出在 runnake 中运行良好。

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

认为你是对的,nosetests 的热点分析器的输出与 runnake 不兼容。当然,对我来说,这两个开箱即用也不是很好。

【讨论】:

    【解决方案3】:

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

    它用 cProfile 代替 hotshot

    【讨论】:

      【解决方案4】:

      根据@squid 的答案不断发展,您可以使用名为nose-cprofnose 插件将nose 默认分析器hotshot 替换为cProfile。

      安装它:

      pip install nose-cprof
      

      然后像这样调用鼻子:

      nosetests --with-cprofile
      

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

      【讨论】:

      • 我没有让插件正常工作:pip install 看起来不错,但是在运行 --with-cprofile 时,找不到标志。我选择了 --with-profile。
      【解决方案5】:

      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
      

      【讨论】:

        猜你喜欢
        • 2010-12-26
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        相关资源
        最近更新 更多