luaprofiler探索


什么是luaprofiler?

http://luaprofiler.luaforge.net/manual.html

LuaProfiler is a time profiler designed to help finding bottlenecks on your Lua program.

If you use LuaProfiler into your program, it will generate a log of all your function calls and their respective running times. You can use this log to generate a summary of the functions your program uses, along with how long it stayed in each of them, or you can import the log into a spreadsheet for further analysis.

LuaProfiler is free software and uses the same license as Lua 5.1.

此工具是檢測lua代碼中, 對於lua執行速度有影響代碼點, 即程序的效率瓶頸。

此工具會產生一個 函數調用的 log, 每個調用會產生響應的運行時間。

可以使用工具產生 總結性質的表, 此表能夠統計各個函數調用的耗時, 並可以將此表導入 電子表格軟件,然后進一步分析。

 

最重要的一點是, 此工具僅僅與lua5.1 兼容。

Status

Current version is 2.0.2. It was developed for Lua 5.1.

 

github網址:

https://github.com/luaforge/luaprofiler

 

軟件下載:

release bin

http://files.luaforge.net/releases/luaprofiler/LuaProfilerbinaries/version1.1forlinux

code

http://files.luaforge.net/releases/luaprofiler/LuaProfiler/LuaProfiler2.0.2

luaprofiler構成

http://luaprofiler.luaforge.net/manual.html

For flexibility reasons, LuaProfiler is divided in 2 parts: the Lua code profiler, written in C to maximize the precision of timing, and the analyzer that generates information about the program execution, written in Lua to allow greater variance in the analysis process.

 

analyzer 中是 summary.lua

image

 

安裝

The easies way to install LuaProfiler is through LuaRocks. Just do luarocks install luaprofiler and LuaRocks will download and install LuaProfiler on all major platforms.

If you want to install by hand, LuaProfiler source is distributed as a group of C files and some makefile templates. LuaProfiler follows the package model for Lua 5.1, therefore it should be "installed" in your package.path.

http://luaprofiler.luaforge.net/manual.html

luarocks install luaprofiler

 

luaprofiler使用

http://luaprofiler.luaforge.net/manual.html

Just require"profiler" in your script and this will define a profiler module with two functions:

start([filename])
Starts the profiler using the optional filename as the log file.
stop()
Stops the profiler.

You can restart the profiler after a stop with another call to start.

 

All you need now is to run your program and get the output generated by the profiler. The default log file is written to the working directory of the program, in a file like lprof_randomid.out where randomid is a random number. The log format uses a line for every function call in your program which may result in huge files, so be careful with disk space if your program runs for a long time, or use localized profiling wrapping the target section with a start()/stop() call sequence.

 

示例

在 replace.lua中添加 profiler檢測代碼:

local profiler = require("profiler")

profiler.start()

for _,file in ipairs(targetFiles) do
    print("handling file =".. file)
    handle_file(file)
end

profiler.stop()

------------------------------------------  handle file end ------------------------------------------

 

運行 replace.lua,發現確實產生一個 lprof_fileYLIo8z.out:

root@fqs:/home/share/luascript/replace# lua replace.lua
parse trans_table starting .....
line=    you  lucy
well formed line=    you  lucy
line=
parse trans_table ending .....
handling file =./replaceFiles/test.txt
you==>lucy
root@fqs:/home/share/luascript/replace# ls
config.lua  lprof_fileYLIo8z.out  replaceFiles  replace.lua

 

查看日志內容:

root@fqs:/home/share/luascript/replace# cat lprof_fileYLIo8z.out
stack_level    file_defined    function_name    line_defined    current_line    local_time    total_time
0    (C)    profiler_init    -1    -1    0.000010    0.000010
0    =[C]    ipairs    -1    232    0.000002    0.000002
0    =[C]    (for generator)    -1    232    0.000002    0.000001
1    =[C]    called from print    -1    -1    0.000003    0.000003
0    =[C]    print    -1    233    0.000024    0.000028
1    =[C]    open    -1    151    0.000010    0.000010
1    =[C]    assert    -1    151    0.000001    0.000001
1    =[C]    read    -1    152    0.000014    0.000014
1    =[C]    close    -1    153    0.000007    0.000008
1    =[C]    pairs    -1    155    0.000001    0.000002
1    =[C]    (for generator)    -1    155    0.000002    0.000002
2    =[C]    called from print    -1    -1    0.000003    0.000002
1    =[C]    print    -1    156    0.000012    0.000016
1    =[C]    gsub    -1    157    0.000003    0.000003
1    =[C]    (for generator)    -1    155    0.000001    0.000001
1    =[C]    open    -1    223    0.000033    0.000033
1    =[C]    assert    -1    223    0.000005    0.000004
1    =[C]    write    -1    224    0.000009    0.000008
1    =[C]    close    -1    225    0.000096    0.000096
0    @replace.lua    handle_file    147    234    0.000114    0.000300
0    =[C]    (for generator)    -1    232    0.000002    0.000002
0    =[C]    stop    -1    237    0.000001    0.000001
root@fqs:/home/share/luascript/replace#

 

使用summary統計下:

root@fqs:/home/share/luascript/replace# /usr/local/bin/summary.lua -v lprof_fileYLIo8z.out
Node name    Calls    Average per call    Total time    %Time
handle_file    1    0.000114    0.000114    32.112676056338
close    2    5.15e-05    0.000103    29.014084507042
open    2    2.15e-05    4.3e-05    12.112676056338
print    2    1.8e-05    3.6e-05    10.140845070423
read    1    1.4e-05    0.000014    3.943661971831
profiler_init    1    1e-05    0.000010    2.8169014084507
write    1    9e-06    0.000009    2.5352112676056
(for generator)    4    1.75e-06    7e-06    1.9718309859155
called from print    2    3e-06    6e-06    1.6901408450704
assert    2    3e-06    6e-06    1.6901408450704
gsub    1    3e-06    0.000003    0.84507042253521
ipairs    1    2e-06    0.000002    0.56338028169014
pairs    1    1e-06    0.000001    0.28169014084507
stop    1    1e-06    0.000001    0.28169014084507
root@fqs:/home/share/luascript/replace#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM