我想获得分析器的统计数据,不是通过调用次数来分组,而是通过花费的时间来分组,而不是通过自然执行顺序。cProfiler:如何通过执行顺序获取统计信息?
例如,如果我们使用cProfiler下一个简单的程序:
def my_sleep0():
sleep(0.05)
def my_sleep1():
sleep(0.15)
my_sleep2()
def my_sleep2():
my_sleep3()
def my_sleep3():
sleep(0.1)
if __name__ == '__main__':
p = Profiler('./') # a wrapper for cProfiler, cProfiler starts here
i = 10
for i in range(1, 5):
i += 100
my_sleep0()
my_sleep1()
my_sleep1()
# cProfiler destructs and ends here
我想自然的执行顺序的统计,例如:
Ordered by: execution
calling_function cumtime backtrace
test.py:36(my_sleep0) <cumtime> test.py:43()
test.py:39(my_sleep1) <cumtime> test.py:44()
test.py:43(my_sleep2) <cumtime> test.py:20 called by test.py:44(my_sleep1)
test.py:43(my_sleep3) <cumtime> test.py:25 called by test.py:20(my_sleep1)->test.py:26(my_sleep2)
test.py:39(my_sleep1) <cumtime> test.py:45()
test.py:43(my_sleep2) <cumtime> test.py:20 called by test.py:25(my_sleep1)
test.py:43(my_sleep3) <cumtime> test.py:25 called by test.py:20(my_sleep1)->test.py:26(my_sleep2)
...
(<cumtime>
是一个数字的时间。 )
Python中是否有方法实现它?怎么样?
加法。
我的探查的代码()不现在就做:
class Profiler:
def __init__(self, dir):
self._profiler = cProfile.Profile()
self._profiler.enable()
self._dir = dir # profiler output is "/tmp/profiler/{key}/<files_here>"
def stop(self):
profilerDir = self._dir
self._profiler.disable()
with open(os.path.join(profilerDir, datetime.now().strftime("%d-%m-%y %H.%M.%S")), 'w') as f:
stat = pstats.Stats(self._profiler, stream = f).sort_stats('pcalls')
stat.print_stats()
stat.print_callers()
stat.print_callees()
def __del__(self):
self.stop()
@KillianDS好的。但看起来cProfiler没有这个功能。我想知道是否有工具或方法来实现我想要的。 – sergzach
您可以按行排序。为什么要按(第一个?)执行时间排序?这对你有什么好处? – KillianDS
@KillianDS我想按执行顺序排序,而不是按执行时间排序。打扰一下。 – sergzach