Lua 中的性能分析器 |
|
(它是在大约 12 分钟内写成的,所以请耐心等待。--Pedro)
用法:调用 Profiler:activate()
开始测量,Profiler:deactivate()
停止测量,然后调用 Profiler:print_results()
查看结果。您可以根据需要调整打印函数,例如排序等。
请注意,通过使用非 ANSI 的毫秒精度函数重载 os.time()
,可以增强该功能。但对于我的应用程序来说,秒级精度已经足够用来识别导致时间呈二次增长而不是线性增长的代码行。
还要注意,目前它只能测量代码行执行次数和时间。如果需要,可以扩展它来测量函数调用执行时间。已经做了一些准备,但还远远不够。
--| Profiler module. --b "Pedro Miller Rabinovitch" <[email protected]> --$Id: prof.lua,v 1.4 2003/10/17 00:17:21 miller Exp $ --TODO add function call profiling. Some of the skeleton is already in --- place, but call profiling behaves different, so a couple of new --- functionalities must be added. --TODO add methods for proper result retrieval --TODO use optional clock() function for millisecond precision, if it's --- available local E, I = {}, {} --& Profiler module. Profiler = E --. Keeps track of the hit counts of each item E.counts = { line = {} } --. These should be inside the _line_ table above. E.last_line = nil E.last_time = os.time() E.started, E.ended = nil, nil --% Activates the profiling system. --@ [kind] (string) Optional hook kind. For now, only 'line' works, --- so just avoid it. >: ) function E:activate( kind ) kind = kind or 'line' local function hook_counter( hk_name, param,... ) local t = self.counts[hk_name][param] if t == nil then t = { count=0, time = 0 } self.counts[hk_name][param] = t end self.counts[hk_name][param].count = self.counts[hk_name][param].count + 1 if self.last_line then local delta = os.time() - self.last_time if delta > 0 then self.counts[hk_name][self.last_line].time = self.counts[hk_name][self.last_line].time + delta self.last_time = os.time() end end self.last_line = param end self.started = os.time() debug.sethook( hook_counter, kind ) end --% Deactivates the profiling system. --@ [kind] (string) Optional hook kind. For now, only 'line' works, --- so just avoid it. function E:deactivate( kind ) kind = kind or 'line' self.ended = os.time() debug.sethook( nil, kind ) end --% Prints the results. --@ [kind] (string) Optional hook... Aah, you got it by now. --TODO add print output formatting and sorting function E:print_results( kind ) kind = kind or 'line' print( kind, 'count', 'approx. time (s)' ) print( '----', '-----', '----------------' ) for i,v in pairs( self.counts[kind] ) do print( i, v.count, v.time ) end print( self.ended - self.started, ' second(s) total (approximately).' ) end