From 4d9ee9403830f4ae92e0a99e6644fd67ba81aa90 Mon Sep 17 00:00:00 2001 From: midoks Date: Fri, 14 Oct 2022 11:36:54 +0800 Subject: [PATCH] up --- plugins/op_waf/t/bench/bench.sh | 5 +- .../op_waf/t/bench/test_find_server_name.lua | 75 +++++++++++++++++++ plugins/op_waf/t/bench/test_gsub.lua | 2 +- plugins/op_waf/t/bench/test_rand.lua | 72 ++++++++++++++++++ plugins/op_waf/waf/lua/common.lua | 18 +++++ plugins/op_waf/waf/lua/init.lua | 24 +----- 6 files changed, 173 insertions(+), 23 deletions(-) create mode 100644 plugins/op_waf/t/bench/test_find_server_name.lua create mode 100644 plugins/op_waf/t/bench/test_rand.lua diff --git a/plugins/op_waf/t/bench/bench.sh b/plugins/op_waf/t/bench/bench.sh index 16aa2f9a8..dbf59ea0f 100755 --- a/plugins/op_waf/t/bench/bench.sh +++ b/plugins/op_waf/t/bench/bench.sh @@ -20,6 +20,9 @@ fi # test # $RUN_CMD a.lua +# $RUN_CMD test_gsub.lua -$RUN_CMD test_gsub.lua +# $RUN_CMD --shdict 'limit 10m' test_find_server_name.lua +# $RUN_CMD --stap --shdict 'limit 10m' test_find_server_name.lua +$RUN_CMD test_rand.lua \ No newline at end of file diff --git a/plugins/op_waf/t/bench/test_find_server_name.lua b/plugins/op_waf/t/bench/test_find_server_name.lua new file mode 100644 index 000000000..89d50dd09 --- /dev/null +++ b/plugins/op_waf/t/bench/test_find_server_name.lua @@ -0,0 +1,75 @@ +local function target() + ngx.re.find("hello, world.", [[\w+\.]], "jo") +end +for i = 1, 100 do + target() +end + +-- 以上为预热操作 +collectgarbage() + +local config_domains = { + [1] = { + ["name"] = "t1.cn", + ["path"] = "/www/wwwroot/t1.cn", + ["domains"] = { + [1] = "t1.cn", + [2] = "t3.cn" + } + } +} + +local function get_server_name(request_name) + for _,v in ipairs(config_domains) + do + for _,cd_name in ipairs(v['domains']) + do + if request_name == cd_name then + return v['name'] + end + end + end + return request_name +end + + +local function get_server_name_cache(request_name) + local cache_name = ngx.shared.limit:get(request_name) + if cache_name then return cache_name end + + for _,v in ipairs(config_domains) + do + for _,cd_name in ipairs(v['domains']) + do + if request_name == cd_name then + ngx.shared.limit:set(cd_name,v['name'],3600) + return v['name'] + end + end + end + return request_name +end + + +ngx.update_time() +local begin = ngx.now() +local N = 1e7 +for i = 1, N do + get_server_name("t3.cn") +end +ngx.update_time() + +ngx.say("test get_server_name elapsed: ", (ngx.now() - begin) / N) + + + + +ngx.update_time() +local begin = ngx.now() +local N = 1e7 +for i = 1, N do + get_server_name_cache("t3.cn") +end +ngx.update_time() + +ngx.say("test get_server_name_cache elapsed: ", (ngx.now() - begin) / N) \ No newline at end of file diff --git a/plugins/op_waf/t/bench/test_gsub.lua b/plugins/op_waf/t/bench/test_gsub.lua index d45c4033e..6d9a7a972 100644 --- a/plugins/op_waf/t/bench/test_gsub.lua +++ b/plugins/op_waf/t/bench/test_gsub.lua @@ -5,7 +5,7 @@ end for i = 1, 100 do target() end - +-- 以上为预热操作 collectgarbage() local function test_string_gsub(str,reps) diff --git a/plugins/op_waf/t/bench/test_rand.lua b/plugins/op_waf/t/bench/test_rand.lua new file mode 100644 index 000000000..2c0b51f3c --- /dev/null +++ b/plugins/op_waf/t/bench/test_rand.lua @@ -0,0 +1,72 @@ +local function target() + ngx.re.find("hello, world.", [[\w+\.]], "jo") +end +for i = 1, 100 do + target() +end + +-- 以上为预热操作 +collectgarbage() + + + +local function get_random_t1(n) + math.randomseed(ngx.time()) + local t = { + "0","1","2","3","4","5","6","7","8","9", + "a","b","c","d","e","f","g","h","i","j", + "k","l","m","n","o","p","q","r","s","t", + "u","v","w","x","y","z", + "A","B","C","D","E","F","G","H","I","J", + "K","L","M","N","O","P","Q","R","S","T", + "U","V","W","X","Y","Z", + } + local s = "" + for i = 1, n do + s = s .. t[math.random(#t)] + end + return s +end + + + +local function get_random_t2(n) + local t = { + "0","1","2","3","4","5","6","7","8","9", + "a","b","c","d","e","f","g","h","i","j", + "k","l","m","n","o","p","q","r","s","t", + "u","v","w","x","y","z", + "A","B","C","D","E","F","G","H","I","J", + "K","L","M","N","O","P","Q","R","S","T", + "U","V","W","X","Y","Z", + } + local s = "" + for i = 1, n do + s = s .. t[math.random(#t)] + end + return s +end + +ngx.update_time() +local begin = ngx.now() +local N = 1e5 +for i = 1, N do + get_random_t1(16) +end +ngx.update_time() + +ngx.say("test get_random_t1 elapsed: ", (ngx.now() - begin) / N) + + +ngx.update_time() +local begin = ngx.now() +local N = 1e5 +math.randomseed(ngx.time()) +for i = 1, N do + get_random_t2(16) +end +ngx.update_time() + +ngx.say("test get_random_t2 elapsed: ", (ngx.now() - begin) / N) + + diff --git a/plugins/op_waf/waf/lua/common.lua b/plugins/op_waf/waf/lua/common.lua index 68b9008bf..8ece6a2c4 100644 --- a/plugins/op_waf/waf/lua/common.lua +++ b/plugins/op_waf/waf/lua/common.lua @@ -390,6 +390,24 @@ function _M.get_server_name(self) return c_name end +function _M.get_random(self,n) + math.randomseed(ngx.time()) + local t = { + "0","1","2","3","4","5","6","7","8","9", + "a","b","c","d","e","f","g","h","i","j", + "k","l","m","n","o","p","q","r","s","t", + "u","v","w","x","y","z", + "A","B","C","D","E","F","G","H","I","J", + "K","L","M","N","O","P","Q","R","S","T", + "U","V","W","X","Y","Z", + } + local s = "" + for i =1, n do + s = s .. t[math.random(#t)] + end + return s +end + function _M.is_ngx_match_orgin(self,rule,match, sign) diff --git a/plugins/op_waf/waf/lua/init.lua b/plugins/op_waf/waf/lua/init.lua index 2c655dffc..0eede8d9d 100644 --- a/plugins/op_waf/waf/lua/init.lua +++ b/plugins/op_waf/waf/lua/init.lua @@ -8,6 +8,7 @@ local waf_root = "{$WAF_ROOT}" local config = require "config" local site_config = require "site" +local config_domains = require "domains" C:setConfData(config, site_config) C:setDebug(true) @@ -32,13 +33,12 @@ function get_server_name() local request_name = ngx.var.server_name local cache_name = ngx.shared.limit:get(request_name) if cache_name then return cache_name end - local config_domains = require "domains" for _,v in ipairs(config_domains) do for _,cd_name in ipairs(v['domains']) do if request_name == cd_name then - ngx.shared.limit:set(cd_name,v['name'],3600) + ngx.shared.limit:set(cd_name,v['name'], 86400) return v['name'] end end @@ -79,24 +79,6 @@ local function get_waf_drop_ip() return data end -local function get_random(n) - math.randomseed(ngx.time()) - local t = { - "0","1","2","3","4","5","6","7","8","9", - "a","b","c","d","e","f","g","h","i","j", - "k","l","m","n","o","p","q","r","s","t", - "u","v","w","x","y","z", - "A","B","C","D","E","F","G","H","I","J", - "K","L","M","N","O","P","Q","R","S","T", - "U","V","W","X","Y","Z", - } - local s = "" - for i =1, n do - s = s .. t[math.random(#t)] - end - return s -end - local function is_chekc_table(data,strings) if type(data) ~= 'table' then return 1 end if not data then return 1 end @@ -334,7 +316,7 @@ local function waf_cc_increase() local cache_rand_key = ip..':rand' local cache_rand = ngx.shared.limit:get(cache_rand_key) if not cache_rand then - cache_rand = get_random(8) + cache_rand = C:get_random(8) ngx.shared.limit:set(cache_rand_key,cache_rand,30) end