[lua][openresty]代码覆盖率检测的解决方式

废话在前

什么是代码覆盖率

来自百度百科

代码覆盖(Code coverage)是软件测试中的一种度量,描述程式中源代码被测试的比例和程度,所得比例称为代码覆盖率。

开发人员为何关注?

在我们的开发过程中,经常要用各种方式进行自测,或是各种 xUnit 系列,或是 postman,或是直接curl,在我们的代码交给 QA 同学之前,我们有必要知道自己的自测验过了多少内容,在这种情况,代码覆盖率就是一个重要的衡量指标。

openresty 中的代码覆率解决方案

我们如果想得到每一次执行的代码覆率,需要搞定两件事情:

  1. 可以外在的记录每一行的代码

  2. 在记录的同时,可以知道这一行的代码上下文是什么

  3. 更重要的是,我们需要尽可能的不动现有业务代码

对于第一点,lua的debug库中有一个非常神奇的钩子函数 sethook,其官方文档如下:

debug.sethook ([thread,] hook, mask [, count])

Sets the given function as a hook. The string mask and the number count describe when the hook will be called. The string mask may have any combination of the following characters, with the given meaning:

  • 'c': the hook is called every time Lua calls a function;

  • 'r': the hook is called every time Lua returns from a function;

  • 'l': the hook is called every time Lua enters a new line of code.Moreover, with a count different from zero, the hook is called also after every count instructions.

When called without arguments, debug.sethook turns off the hook. When the hook is called, its first argument is a string describing the event that has triggered its call: "call" (or "tail call"), "return", "line", and "count". For line events, the hook also gets the new line number as its second parameter. Inside a hook, you can call getinfo with level 2 to get more information about the running function (level 0 is the getinfo function, and level 1 is the hook function).

其中文翻译大体如下:

将给定的方法设定为钩子,参数 mask和 count决定了什么时候钩子方法被调用.参数 mask可以是下列字符的组合:

  • 'c' 当lua开始执行一个方法时调用;

  • 'r' 当lua执行一个方法在返回时调用;

  • 'l' 当lua每执行到一行代码时调用.即lua从0开始执行一个方法的每一行时,这个钩子都会被调用.

如果调用时不传任何参数,则会移除相应的钩子.当一个钩子方法被调用时,第一个参数表明了调用这个钩子的事件: "call"(或 "tail call"), "return""line"和 "count".对于执行代码行的事件,新代码的行号会作为第二个参数传入钩子方法,可以用 debug.getinfo(2)得到其他上下文信息.

在这个官方的说明里,lua已经贴心的告诉我们使用方式————配合debug.getinfo,那么debug.getinfo是什么?其实我们在之前讨论错误输出时已经使用过这个方法,其官方文档如下:

debug.getinfo ([thread,] f [, what])

Returns a table with information about a function. You can give the function directly or you can give a number as the value of f, which means the function running at level f of the call stack of the given thread: level 0 is the current function (getinfo itself); level 1 is the function that called getinfo (except for tail calls, which do not count on the stack); and so on. If f is a number larger than the number of active functions, then getinfo returns nil.

The returned table can contain all the fields returned by lua_getinfo, with the string what describing which fields to fill in. The default for what is to get all information available, except the table of valid lines. If present, the option 'f' adds a field named func with the function itself. If present, the option 'L' adds a field named activelines with the table of valid lines.

For instance, the expression debug.getinfo(1,"n").name returns a name for the current function, if a reasonable name can be found, and the expression debug.getinfo(print) returns a table with all available information about the print function.

这个API的说明中文含义大体如下:

以table的形式返回一个函数的信息,可以直接调用这个方法或是传入一个表示调用堆栈深度的参数f,0表示当前方法(即getinfo本身),1表示调用getinfo的方法(除了最顶层的调用,即不在任何方法中),以此类推。如果传入的值比当前堆栈深度大,则返回nil.

返回的table内字段包含由lua_info返回的所有字段。默认调用会除了代码行数信息的所有信息。当前版本下,传入 'f'会增加一个 func字段表示方法本身,传入 'L'会增加一个 activelines字段返回函数所有可用行数。

例如如果当前方法是一个有意义的命名, debug.getinfo(1,"n").name可以得到当前的方法名,而 debug.getinfo(print)可以得到print方法的所有信息。

OK,有了这两个方法,我们的思路就变得很清析了:

  1. 在生命周期开始时注册钩子函数.

  2. 将每一次调用情况记录汇总.

这里有一个新的问题,就是,我们的汇总是按调用累加还是只针对每一次调用计算,本着实用的立场,我们是需要进行累加的,那么,需要使用ngx.share_dict 来保存汇总信息.

基于以上考虑,封装一个libs/test/hook.lua文件,内容如下:

 
 
  1. local debug = load "debug"

  2. local cjson = load "cjson"

  3. local M = {}

  4. local mt = { __index = M }

  5. local sharekey = 'test_hook'

  6. local cachekey = 'test_hook'

  7. function M:new()

  8.    local ins = {}

  9.    local share = ngx.shared[sharekey]

  10.    local info ,ret = share:get(cachekey)

  11.    if info then

  12.        info = cjson.decode(info)

  13.    else

  14.        info = {}

  15.    end

  16.    ins.info = info

  17.    setmetatable(ins,mt)

  18.    return ins

  19. end

  20. function M:sethook ()

  21.    debug.sethook(function(event,line)

  22.        local info = debug.getinfo(2)

  23.        local s = info.short_src

  24.        local f = info.name

  25.        local startline = info.linedefined

  26.        local endline = info.lastlinedefined

  27.        if  string.find(s,"lualib") ~= nil then

  28.            return

  29.        end

  30.        if self.info[s] == nil then

  31.            self.info[s]={}

  32.        end

  33.        if f == nil then

  34.            return

  35.        end

  36.        if self.info[s][f] ==nil then

  37.            self.info[s][f]={

  38.                start = startline,

  39.                endline=endline,

  40.                exec = {},

  41.                activelines = debug.getinfo(2,'L').activelines

  42.                }

  43.        end

  44.        self.info[s][f].exec[tostring(line)]=true


  45.    end,'l')

  46. end

  47. function M:save()

  48.     local share = ngx.shared[sharekey]

  49.     local ret = share:set(cachekey,cjson.encode(self.info),120000)

  50. end

  51. function M:delete()

  52.     local share = ngx.shared[sharekey]

  53.     local ret = share:delete(cachekey)

  54.     self.info = {}

  55. end

  56. function M:get_report()

  57.    local res = {}

  58.    for f,v in pairs(self.info) do

  59.        item = {

  60.            file=f,

  61.            funcs={}

  62.        }

  63.        for m ,i in pairs(v) do

  64.                local cover = 0

  65.                local index = 0

  66.                for c,code in pairs(i.activelines) do

  67.                    if i.activelines[c] then

  68.                        index = index + 1

  69.                    end

  70.                    if i.exec[tostring(c)] or i.exec[c] then

  71.                        cover = cover +1

  72.                    end

  73.                end

  74.                item.funcs[#item.funcs+1] =

      1. { name = m ,

      2. coverage=   string.format("%.2f",cover / index*100 ) .."%"}

  75.        end

  76.        res[#res+1]=item

  77.   end

  78.   return res

  79. end

  80. return M

这样,我们只需要在contentbylua的最开始加上:

 
 
  1. local hook = load "libs.test.hook"

  2. local test = hook:new()

  3. test:sethook()

  4. --other code ..

在最末加上:

 
 
  1. test:save()

即可统计代码覆盖率。

是的,没错,我们至今只增加了4行业务代码

但是统计了,应该怎么进行输出呢?

加个接口好了:)

因为现在lor用的多,所以,干脆加个lor的路由文件(libs/test/lorapi.lua):

 
 
  1. local hook = require 'libs.test.hook'

  2. local router =  lor:Router ()

  3. local M = {}

  4. router:get('/test/coverage/json-report',

  5. function(req,res,next)

  6.    local t = hook:new()

  7.    res:json(t:get_report())

  8. end)

  9. router:get('/test/coverage/txt-report',

  10. function(req,res,next)

  11.    local t = hook:new()

  12.    local msg = "Report"

  13.    local rpt = t:get_report()

  14.    for i ,v in pairs(rpt) do

  15.        msg =msg.."\r\n"..v.file

  16.        for j,f in pairs(v.funcs) do

  17.            msg = msg .."\r\n\t function name:"

        1. .. f.name .."\tcoverage:"..f.coverage

  18.        end

  19.    end

  20.    msg =msg .."\r\nEnd"

  21.    res:send(msg)

  22. end)

  23. return router

这样,在我们的lor路由文件里加个requre,加个use,两行改动,而且是增加!!就达到我们的需求,检查代码的覆盖率.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值