简体   繁体   English

Python分析-代码之外的汇总函数调用

[英]Python Profiling - Rollup function calls that are outside my code

I am trying to profile our django unittests (if the tests are faster, we'll run 'em more often). 我正在尝试分析django单元测试(如果测试速度更快,我们将更频繁地运行它们)。 I've ran it through python's built in cProfile profiler, producing a pstats file. 我已经通过python内置的cProfile Profiler运行了它,生成了一个pstats文件。

However the signal to noise ration is bad. 但是,信噪比很差。 There are too many functions listed. 列出的功能太多。 Lots and lots of django internal functions are called when I make one database query. 当我进行一个数据库查询时,将调用很多django内部函数。 This makes it hard to see what's going on. 这使得很难看到发生了什么。

Is there anyway I can "roll up" all function calls that are outside a certain directory? 无论如何,我可以“汇总”某个目录之外的所有函数调用吗?

eg if I call a python function outside my directory, and it then calls 5 other functions (all outside my directory), then it should roll all those up, so it looks like there was only one function call, and it should show the cumulative time for the whole thing. 例如,如果我在目录外调用python函数,然后再调用5个其他函数(都在目录外),则应该将所有这些函数汇总,因此看起来好像只有一个函数调用,并且应该显示累计整个事情的时间。

This, obviously, is bad if you want to profile (say) Django, but I don't want to do that. 显然,如果要分析(例如)Django,这很不好,但是我不想这样做。

I looked at the pstats.Stats object, but can't see an obvious way to modify this data. 我查看了pstats.Stats对象,但看不到修改该数据的明显方法。

I have little experience with python, but a lot in performance tuning, so here's a possibility: 我对python的经验很少,但是在性能调优方面很多,所以这里有一种可能:

  1. Rather than run profiling as part of the unit tests, just do overall execution time measurements. 而不是作为单元测试的一部分运行概要分析,而是进行总体执行时间测量。 If those don't change much, you're OK. 如果这些变化不大,则可以。

  2. When you do detect a change, or if you just want to make things run faster, use this method . 当您确实检测到更改时,或者如果您只是想让事情运行得更快,请使用此方法 It has a much higher "signal-to-noise ratio", as you put it. 正如您所说,它具有更高的“信噪比”。

The difference is you're not hoping the profiler can figure out what you need to be looking at. 不同之处在于您不希望探查器能够弄清楚您需要查看的内容。 It's more like a break point in a debugger, where the break occurs not at a place of your choosing, but with good probability at a time of unnecessary slowness. 它更像是调试器中的断点,断点不是在您选择的位置发生的,而是在不必要的缓慢情况下发生的可能性很大。 If on two or more occasions you see it doing something that might be replaced with something better, on average it will pay off, and fixing it will make other problems easier to find by the same method. 如果您看到它在两次或更多次执行中可能会被更好的东西所代替,则平均而言,它会有所收获,而修复它会使通过相同方法更容易发现其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM