STRESS_TEST_D = {}
setmetatable(STRESS_TEST_D, {__index = _G})
local _ENV = STRESS_TEST_D
local is_system_busy = false
local start_time = 0
local login_number = 0
MAX_MEMCORY = 1024
TIME_LOGIN = 500
function watch()
print("账号前缀:%o\n", start_time)
end
function get_memory()
local memory = memory_use() / 1024
return memory
end
function check_system()
local memory = get_memory()
local online_number = online_number()
trace("\nprocess memory : %o MB\nlogin number : %o\nonline number : %o\n",
memory, login_number, online_number)
if memory > MAX_MEMCORY then
is_system_busy = true
print("Console is Busy!! rest for a while..")
else
is_system_busy = false
trace("Console is Working...")
end
end
function online_number()
local player_list = child_objects(PLAYER_TDCLS)
return (sizeof(player_list))
end
function heartbeat_handler(player)
if is_system_busy then
return
end
if not is_object(player) then
return
end
local interval = player:query("interval")
if not interval then
return
end
local accumulate = player:query("accumulate") or {}
local test_modules = player:query("test_modules") or {}
for test_module, _ in pairs(test_modules) do
if interval[test_module] then
if not accumulate[test_module] then
accumulate[test_module] = 0
end
accumulate[test_module] = accumulate[test_module] + HEARTBEAT_INTERVAL
if accumulate[test_module] >= interval[test_module] then
local child_module = _G[test_module]
if child_module and type(child_module.operation) == "function" then
child_module.operation(player)
else
print("找不到压力测试子模块(%o) 或者 该子模块未定义'operation'接口!\n", test_module)
test_modules[test_module] = nil
end
accumulate[test_module] = 0
end
else
print("要求测试的压力子模块(%o)并未定义!\n", test_module)
test_modules[test_module] = nil
end
end
player:set("accumulate", accumulate)
end
function login(arg)
login_number = login_number + 1
arg.number = arg.number - 1
local account = string.format("%d_%d", start_time, login_number)
LOGIN_D.login(account, "default_password", arg.extra_data)
if arg.number > 0 then
set_timer(TIME_LOGIN, login, arg)
end
end
function logout(number)
local player_list = child_objects(PLAYER_TDCLS)
for i, player in ipairs(player_list) do
destruct_object(player)
if number and i >= number then
break
end
end
end
function start(number, modules_str)
if number and number <= 0 then
return
end
_G["_DEBUG"] = nil
_G["START_STREE_TEST"] = true
local test_modules = {}
if modules_str then
local temp = explode(string.gsub(modules_str, " ", ""), ",")
for _, start_module in ipairs(temp) do
test_modules[start_module] = true
end
end
if number then
local arg = {
number = number,
extra_data = {test_modules = test_modules}
}
login(arg)
else
local player_list = child_objects(PLAYER_TDCLS)
for _, player in ipairs(player_list) do
player:set("test_modules", test_modules)
end
end
end
function stop(modules_str)
local player_list = child_objects(PLAYER_TDCLS)
if not modules_str then
for _, player in ipairs(player_list) do
player:set("test_modules", {})
end
else
local stop_modules = explode(string.gsub(modules_str, " ", ""), ",")
for _, player in ipairs(player_list) do
local test_modules = player:query("test_modules") or {}
for _, stop_module in ipairs(stop_modules) do
test_modules[stop_module] = nil
end
end
end
end
function add_module(player, modules_str)
modules_str = modules_str or ""
local start_modules = explode(string.gsub(modules_str, " ", ""), ",")
local test_modules = player:query("test_modules") or {}
for _, start_module in ipairs(start_modules) do
test_modules[start_module] = true
end
player:set("test_modules", test_modules)
end
function del_module(player, modules_str)
if not modules_str then
player:set("test_modules", {})
else
local stop_modules = explode(string.gsub(modules_str, " ", ""), ",")
local test_modules = player:query("test_modules") or {}
for _, stop_module in ipairs(stop_modules) do
test_modules[stop_module] = nil
end
end
end
local function get_random_interval(test_modules)
local interval = {};
for child_name, _ in pairs(test_modules) do
if _G[child_name] and _G[child_name].random_interval then
interval[child_name] = _G[child_name].random_interval()
end
end
return interval;
end
function func_login_ok(player)
trace("%o登陆成功!\n", player)
local extra_data = player:query_temp("extra_data") or {}
local test_modules = extra_data.test_modules
if test_modules then
local interval = get_random_interval(test_modules)
for test_module, _ in pairs(test_modules) do
if not interval[test_module] then
test_modules[test_module] = nil
end
end
player:set("interval", interval)
player:set("test_modules", test_modules)
end
end
function destruct()
remove_audience_from_raiser("STRESS_TEST_D", {SF_LOGIN_OK})
end
local function init()
set_timer(100000, check_system, {}, true)
end
function create()
load_folder("client/daemons/stress_test")
register_heartbeat("PLAYER_TDCLS", heartbeat_handler)
register_post_init(init)
register_as_audience("STRESS_TEST_D", {EVENT_LOGIN_OK = func_login_ok})
start_time = string.sub(tostring(os.time()), 8)
end
create()