This commit is contained in:
parent
083fe59fa5
commit
bf281107b2
|
|
@ -1,3 +1,5 @@
|
|||
-- cd /www/server/mdserver-web/plugins/op_waf/t/bench && bash bench.sh
|
||||
|
||||
local function target()
|
||||
ngx.re.find("hello, world.", [[\w+\.]], "jo")
|
||||
end
|
||||
|
|
@ -5,7 +7,7 @@ for i = 1, 100 do
|
|||
target()
|
||||
end
|
||||
|
||||
function file_exists(path)
|
||||
local function file_exists(path)
|
||||
local file = io.open(path, "rb")
|
||||
if file then file:close() end
|
||||
return file ~= nil
|
||||
|
|
@ -14,42 +16,48 @@ end
|
|||
-- 以上为预热操作
|
||||
collectgarbage()
|
||||
|
||||
local json = require "cjson"
|
||||
|
||||
function get_cpu_stat()
|
||||
local function data_split(self, str,reps )
|
||||
local rsList = {}
|
||||
string.gsub(str,'[^'..reps..']+',function(w)
|
||||
table.insert(rsList,w)
|
||||
end)
|
||||
return rsList
|
||||
end
|
||||
|
||||
local function get_cpu_stat()
|
||||
local cpu_total = 0
|
||||
local fp = io.open('/proc/stat','r')
|
||||
local cpu_line = fp:read()
|
||||
fp:close()
|
||||
|
||||
local list = ngx_re.split(cpu_line," ")
|
||||
table.remove(list,1)
|
||||
table.remove(list,1)
|
||||
|
||||
local iterator, err = ngx.re.gmatch(cpu_line,"(d+)")
|
||||
while true do
|
||||
local m, err = iterator()
|
||||
if not m then
|
||||
break
|
||||
end
|
||||
cpu_total = cpu_total + m[0]
|
||||
local idie = list[4]
|
||||
for i,v in pairs(list)
|
||||
do
|
||||
cpu_total = cpu_total + v
|
||||
end
|
||||
return cpu_total
|
||||
|
||||
local use_percent = tonumber(100-(idie/cpu_total)*100)
|
||||
|
||||
return cpu_total,idie,use_percent
|
||||
end
|
||||
|
||||
function get_cpu_percent()
|
||||
local fp = io.open("/proc/stat","r")
|
||||
local cpu_line = fp:read()
|
||||
fp:close()
|
||||
local iterator, err = ngx.re.gmatch(cpu_line,"(d+)")
|
||||
while true do
|
||||
local m, err = iterator()
|
||||
if not m then
|
||||
break
|
||||
end
|
||||
cpu_total1 = cpu_total1 + m[0]
|
||||
end
|
||||
local function get_cpu_percent()
|
||||
local cpu_total,idie,use_percent = get_cpu_stat()
|
||||
ngx.sleep(2)
|
||||
local cpu_total2,idie2,use_percent2 = get_cpu_stat()
|
||||
local cpu_usage_percent = tonumber(100-(((idie2-idie)/(cpu_total2-cpu_total))*100))
|
||||
return cpu_usage_percent
|
||||
end
|
||||
|
||||
ngx.update_time()
|
||||
local begin = ngx.now()
|
||||
local N = 1e2
|
||||
local N = 1e1
|
||||
for i = 1, N do
|
||||
get_cpu_stat()
|
||||
end
|
||||
|
|
|
|||
|
|
@ -32,6 +32,11 @@ end
|
|||
|
||||
ngx.shared.waf_limit:set("cpu_usage", 0, 10)
|
||||
function waf_timer_every_get_cpu(premature)
|
||||
if WAF_C:file_exists('/proc/stat') then
|
||||
local lua_cpu_percent = WAF_C:get_cpu_percent()
|
||||
WAF_C:D("lua_cpu_percent:"..tostring(lua_cpu_percent))
|
||||
end
|
||||
|
||||
local cpu_percent = WAF_C:read_file_body(waf_root.."/cpu.info")
|
||||
-- WAF_C:D("cpu_usage:"..tostring(cpu_percent ))
|
||||
if cpu_percent then
|
||||
|
|
|
|||
|
|
@ -765,6 +765,34 @@ function _M.is_key(self, arr, key)
|
|||
return false
|
||||
end
|
||||
|
||||
function _M.get_cpu_stat(self)
|
||||
local cpu_total = 0
|
||||
local fp = io.open('/proc/stat','r')
|
||||
local cpu_line = fp:read()
|
||||
fp:close()
|
||||
|
||||
local list = ngx_re.split(cpu_line," ")
|
||||
table.remove(list,1)
|
||||
table.remove(list,1)
|
||||
|
||||
local idie = list[4]
|
||||
for i,v in pairs(list)
|
||||
do
|
||||
cpu_total = cpu_total + v
|
||||
end
|
||||
|
||||
local use_percent = tonumber(100-(idie/cpu_total)*100)
|
||||
return cpu_total,idie,use_percent
|
||||
end
|
||||
|
||||
function _M.get_cpu_percent(self)
|
||||
local cpu_total,idie,use_percent = self:get_cpu_stat()
|
||||
ngx.sleep(2)
|
||||
local cpu_total2,idie2,use_percent2 = self:get_cpu_stat()
|
||||
local cpu_usage_percent = tonumber(100-(((idie2-idie)/(cpu_total2-cpu_total))*100))
|
||||
return cpu_usage_percent
|
||||
end
|
||||
|
||||
|
||||
function _M.return_post_data(self)
|
||||
if method ~= "POST" then return false end
|
||||
|
|
|
|||
Loading…
Reference in New Issue