Skip to content

Instantly share code, notes, and snippets.

@emacsist
Created May 31, 2018 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emacsist/cb215ba5c3cc0bd5b38133a145668908 to your computer and use it in GitHub Desktop.
Save emacsist/cb215ba5c3cc0bd5b38133a145668908 to your computer and use it in GitHub Desktop.
local access = ngx.shared.access
local args = ngx.req.get_uri_args()
local host = "host"
local one_minute_ago = tonumber(os.date("%s")) - 60
local now = tonumber(os.date("%s"))
local status_fail_total = 0
local flow_total = 0
local reqt_total = 0
local req_total = 0
local req_banner_total = 0
local bid_count = 0
for status=100,600 do
local status_total = 0
for second_num=one_minute_ago,now do
local status_key = table.concat({host,"-",status,"-",second_num})
local status_sum = access:get(status_key) or 0
status_total = status_total + status_sum
end
-- if (status_total > 0) then
-- ngx.print("status[", status, "], total=>", status_total, "\n")
-- end
if (status == 200) then
bid_count = status_total
end
end
for second_num=one_minute_ago,now do
-- 失败请求数统计
local status_fail_key = table.concat({host,"-","error","-",second_num})
local status_fail_sum = access:get(status_fail_key) or 0
status_fail_total = status_fail_total + status_fail_sum
-- 流量统计
local flow_key = table.concat({host,"-flow-",second_num})
local flow_sum = access:get(flow_key) or 0
flow_total = flow_total + flow_sum
-- 总请求数统计
local total_req_key = table.concat({host,"-total_req-",second_num})
local req_sum = access:get(total_req_key) or 0
req_total = req_total + req_sum
-- 请求时间统计
local req_time_key = table.concat({host,"-reqt-",second_num})
local req_time_sum = access:get(req_time_key) or 0
reqt_total = reqt_total + req_time_sum
-- Banner 请求数统计
local req_banner_key = table.concat({host,"-banner-",second_num})
local req_banner_sum = access:get(req_banner_key) or 0
req_banner_total = req_banner_total + req_banner_sum
end
-- 向上取整数
local QPS = math.ceil(req_total / 60)
-- 单位是 ms
local TPS = math.ceil(reqt_total / 60 / 1000)
local FLOW = ""
local BANNER_QPS = math.ceil(req_banner_total / 60)
-- 竞价率
local BID_RATE = 0
if (req_total > 0 and bid_count > 0) then
BID_RATE = bid_count/req_total * 100
end
-- 错误率
local ERR_RATE = 0
if (req_total > 0 and status_fail_total > 0) then
ERR_RATE = status_fail_total / req_total * 100
end
-- ngx.say("reqt_total", reqt_total)
-- 带宽和流量
local BRAND_WIDTH = ""
if (flow_total > 1024 * 1024)
then
FLOW = ""..(flow_total / 1024 / 1024).." MB"
BRAND_WIDTH = ""..(flow_total / 1024 / 1024 / 8).." MB"
else
FLOW = ""..(flow_total / 1024).." KB"
BRAND_WIDTH = ""..(flow_total / 1024 / 8).." KB"
end
-- ngx.print("总请求数:", req_total, ", 错误数:", status_fail_total, ", QPS:", QPS, ", TPS:", TPS, ", 错误率:", ERR_RATE, "%", ", 流量:", FLOW, ", 相当于网络带宽: ", BRAND_WIDTH)
ngx.print(req_total, ",", QPS, ",", req_banner_total, ",", BANNER_QPS, ",", bid_count, ",", string.format("%.3f",BID_RATE) , "%,", status_fail_total, ",", string.format("%.3f",ERR_RATE), "%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment