Skip to content

Commit

Permalink
统计性能优化 nginx调优
Browse files Browse the repository at this point in the history
  • Loading branch information
songweihang committed Apr 11, 2016
1 parent b1c7f53 commit 190b13f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 94 deletions.
40 changes: 0 additions & 40 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,44 +1,4 @@
# Compiled Lua sources
luac.out

# luarocks build files
*.src.rock
*.zip
*.tar.gz

# Object files
*.o
*.os
*.ko
*.obj
*.elf

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo
*.def
*.exp

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

client_body_temp
proxy_temp
uwsgi_temp
Expand Down
12 changes: 10 additions & 2 deletions apps/http/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local statsMatchConf = systemConf.statsMatchConf
local statsCache = ngx.shared.stats
local statsAllCache = ngx.shared.statsAll

--ngx.say(statsCache:get(statsPrefixConf.http_success_time))
--ngx.say(statsCache:get(statsPrefixConf.http_total))

function dump(o)
if type(o) == 'table' then
Expand All @@ -29,4 +29,12 @@ end

-- 过滤nginx无法处理请求

--ngx.say(ngx.var.request_time)
--ngx.say(ngx.var.request_time)




--local stats = require "apps.lib.stats"
--local a = stats:new(1,2,3,4,5)
--ngx.say(dump(a))
--a:incrStatsNumCache(statsCache,statsPrefixConf)
27 changes: 26 additions & 1 deletion apps/http/init.lua
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
local stats = require "apps.lib.stats".init()
--local stats = require "apps.lib.stats".init()

local statsCache = ngx.shared.stats
local systemConf = require "config.init"
local statsPrefixConf = systemConf.statsPrefixConf

--[[
初始化统计缓存
@param cache ngx_shared
@param table conf
@return
]]--
local function intStatsNumCache(cache,conf)

local ok, err = cache:add(conf.http_total,0)
if ok then
cache:add(conf.http_fail,0)
cache:add(conf.http_success_time,0)
cache:add(conf.http_fail_time,0)
cache:add(conf.http_success_upstream_time,0)
cache:add(conf.http_fail_upstream_time,0)
end
end


intStatsNumCache(statsCache,statsPrefixConf)
32 changes: 29 additions & 3 deletions apps/http/log.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
--[[
]]--
local statsCache = ngx.shared.stats
local statsAllCache = ngx.shared.statsAll
local statsMatchCache = ngx.shared.statsMatch

local systemConf = require "config.init"
local statsPrefixConf = systemConf.statsPrefixConf
local statsMatchConf = systemConf.statsMatchConf
local statsAllSwitchConf = systemConf.statsAllSwitchConf

local ngx_var_status = ngx.var.status
local ngx_var_uri = ngx.var.uri or ''
local ngx_var_host = ngx.var.host
local ngx_var_request_time = ngx.var.request_time or 0
local ngx_var_upstream_response_time = ngx.var.upstream_response_time or 0

local stats = require "apps.lib.stats"

stats.run()
stats.statsAll()
stats.statsMatch()
local statsRun = stats:new(
ngx_var_status,
ngx_var_uri,
ngx_var_host,
ngx_var_request_time,
ngx_var_upstream_response_time
)

statsRun:incrStatsNumCache(statsCache,statsPrefixConf)
--stats.statsAll()
--stats.statsMatch()
103 changes: 59 additions & 44 deletions apps/lib/stats.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
local modulename = "appsLibStats"

local setmetatable = setmetatable
local ngxmatch = ngx.re.match
local match = string.match
local tonumber = tonumber
local assert = assert

local _M = {}

_M._VERSION = '0.1'
local _M = { _VERSION = '0.01' }

local ngx_var = ngx.var
local systemConf = require "config.init"
local statsPrefixConf = systemConf.statsPrefixConf
local statsMatchConf = systemConf.statsMatchConf
local statsAllSwitchConf = systemConf.statsAllSwitchConf
local mt = { __index = _M }

-- ngx.shared.DICT
local statsCache = ngx.shared.stats
local statsAllCache = ngx.shared.statsAll
local statsMatchCache = ngx.shared.statsMatch

local ngxmatch = ngx.re.match
local match = string.match
function _M.new(self,status,uri,host,request_time,upstream_response_time)

--assert(status >0 and uri >= 0 and request_time >0 and upstream_response_time > 0 and host >0 )

local self = {
status = status,
uri = uri,
host = host,
request_time = request_time,
upstream_response_time = upstream_response_time,
}

return setmetatable(self, mt)
end

--[[
初始化统计缓存
@param cache ngx_shared
@param table conf
@return
]]--
local function intStatsNumCache(cache,conf)
local ok, err = cache:add(conf.http_total,0)
Expand All @@ -36,65 +46,68 @@ local function intStatsNumCache(cache,conf)
end
end
_M.init = function ()
--Initial stats
intStatsNumCache(statsCache,statsPrefixConf)
end
]]--

--[[
统计计数器
@param cache ngx_shared
@param table conf
@return
]]--
local function incrStatsNumCache(cache,conf)
function _M.incrStatsNumCache(self,cache,conf)

local request_time = self.request_time
local upstream_response_time = self.upstream_response_time
local status = self.status

if tonumber(ngx_var.status) == 499 then
-- 忽略 499 客户端链接中断的数据
if tonumber(status) == 499 then
return
end

cache:incr(conf.http_total,1)
-- ngx.HTTP_INTERNAL_SERVER_ERROR
if tonumber(ngx_var.status) >= 400 then
if tonumber(status) >= 400 then
-- HTTP FAIL
cache:incr(conf.http_fail,1)

cache:incr(conf.http_fail_time,ngx_var.request_time)
if ngx_var.upstream_response_time ~= nil then
cache:incr(conf.http_fail_upstream_time,ngx_var.upstream_response_time)
cache:incr(conf.http_fail_time,request_time)
if upstream_response_time > 0 then
cache:incr(conf.http_fail_upstream_time,upstream_response_time)
end
else
cache:incr(conf.http_success_time,ngx_var.request_time)
if ngx_var.upstream_response_time ~= nil then
cache:incr(conf.http_success_upstream_time,ngx_var.upstream_response_time)
cache:incr(conf.http_success_time,request_time)
if upstream_response_time > 0 then
cache:incr(conf.http_success_upstream_time,upstream_response_time)
end
end
return
end

_M.init = function ()
--Initial stats
intStatsNumCache(statsCache,statsPrefixConf)
end

_M.run = function()
-- http 请求总统计数据
incrStatsNumCache(statsCache,statsPrefixConf)
end

function _M.statsMatch()
function _M.statsMatch(self)

local body,method = '',ngx_var.request_method
local ngx_var_uri = self.ngx_var_uri
local request_method,body = ngx.var.request_method

if method ~= 'GET' and method ~= 'DELETE'
and method ~= 'HEAD' and method ~= 'OPTIONS' then
if request_method ~= 'GET' and request_method ~= 'DELETE'
and request_method ~= 'HEAD' and request_method ~= 'OPTIONS' then

body = ngx_var.request_body
body = ngx.var.request_body
if body ~= nil then
-- body + uri
local tmp = {}
table.insert(tmp, ngx_var.uri)
table.insert(tmp, ngx_var_uri)
table.insert(tmp, body)
table.concat(tmp, "")
else
-- uri
end
else
body = ngx_var.uri
body = ngx_var_uri
end

-- 获取正则特殊定制统计
Expand All @@ -109,20 +122,22 @@ function _M.statsMatch()
end
end

function _M.statsAll()
function _M.statsAll(self)
local statsAllConf = _M.initStatsAll()
incrStatsNumCache(statsAllCache,statsAllConf)
end

function _M.initStatsAll()
function _M.initStatsAll(self)

local StatsAllConf = {}
local ngx_var_uri = self.ngx_var_uri
local ngx_var_host = self.ngx_var_host
local StatsAllConf = {}

for k, v in pairs(statsPrefixConf) do
local tmp = {}
table.insert(tmp, v)
table.insert(tmp, ngx_var.host)
table.insert(tmp, ngx_var.uri)
table.insert(tmp, ngx_var_host)
table.insert(tmp, ngx_var_uri)
StatsAllConf[k] = table.concat(tmp, "")
end

Expand Down
10 changes: 6 additions & 4 deletions server/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#user nginx;
#user root;
worker_processes auto;

pid logs/nginx-knight.pid;

events {
worker_connections 1024;
worker_connections 32768;
accept_mutex off;
multi_accept on;
}

http {
Expand All @@ -17,7 +19,7 @@ http {
' $request_time $upstream_response_time';
access_log logs/access.log main;

sendfile on;
sendfile on;
#keepalive_timeout 0;

lua_package_path "/Users/apple/Jakin/knight/?.lua;;";
Expand Down Expand Up @@ -46,7 +48,7 @@ http {

server {

listen 8088;
listen 8088 backlog=16384;
server_name 127.0.0.1;
root html;
index index.html index.htm;
Expand Down

0 comments on commit 190b13f

Please sign in to comment.