mdserver-web/plugins/op_waf/t/bench/test_get_cpu.lua

70 lines
1.5 KiB
Lua
Raw Permalink Normal View History

2023-02-12 11:26:31 -05:00
-- cd /www/server/mdserver-web/plugins/op_waf/t/bench && bash bench.sh
2023-02-12 04:29:29 -05:00
local function target()
ngx.re.find("hello, world.", [[\w+\.]], "jo")
end
for i = 1, 100 do
target()
end
2023-02-12 11:26:31 -05:00
local function file_exists(path)
2023-02-12 04:29:29 -05:00
local file = io.open(path, "rb")
if file then file:close() end
return file ~= nil
end
-- 以上为预热操作
collectgarbage()
2023-02-12 11:26:31 -05:00
local json = require "cjson"
2023-02-12 12:38:24 -05:00
local ngx_re = require "ngx.re"
2023-02-12 11:26:31 -05:00
local function data_split(self, str,reps )
local rsList = {}
string.gsub(str,'[^'..reps..']+',function(w)
table.insert(rsList,w)
end)
return rsList
end
2023-02-12 04:29:29 -05:00
2023-02-12 11:26:31 -05:00
local function get_cpu_stat()
2023-02-12 04:29:29 -05:00
local cpu_total = 0
local fp = io.open('/proc/stat','r')
local cpu_line = fp:read()
fp:close()
2023-02-12 11:26:31 -05:00
local list = ngx_re.split(cpu_line," ")
table.remove(list,1)
table.remove(list,1)
2023-02-12 04:29:29 -05:00
2023-02-12 11:26:31 -05:00
local idie = list[4]
for i,v in pairs(list)
do
cpu_total = cpu_total + v
2023-02-12 04:29:29 -05:00
end
2023-02-12 11:26:31 -05:00
local use_percent = tonumber(100-(idie/cpu_total)*100)
return cpu_total,idie,use_percent
2023-02-12 04:29:29 -05:00
end
2023-02-12 11:26:31 -05:00
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))
2023-02-12 12:38:24 -05:00
ngx.say("cpu_usage_percent:"..cpu_usage_percent)
2023-02-12 11:26:31 -05:00
return cpu_usage_percent
2023-02-12 04:29:29 -05:00
end
ngx.update_time()
local begin = ngx.now()
2023-02-12 11:26:31 -05:00
local N = 1e1
2023-02-12 04:29:29 -05:00
for i = 1, N do
get_cpu_stat()
end
ngx.update_time()
ngx.say("test_get_cpu elapsed: ", (ngx.now() - begin))