消息分發複用TDEngine底層的邏輯,包括鏈接創建,消息讀取,消息發送,消息執行,定時器執行git
加載時操做github
1.加載壓測文件夾模塊,目錄stress_testdom
2.註冊玩家登錄成功回調事件,並註冊玩家心跳回調函數
3.帳號名起始字段測試
4.註冊壓測定時器lua
啓動壓測模塊code
調用STRESS_TEST_D.start(500, "CHAT_TESTD"),第一個參數是須要登錄的人數,第二個參數是須要測試的模塊,模塊名以逗號隔開,若是有設置登錄的人數,則進行登錄,若是沒有設置登錄人數則對在線的玩家設置測試模塊orm
-- 有則新登錄number玩家再進行操做 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
玩家登錄接口
一次調用函數只登錄一個,若是還有須要登錄的用戶,則啓動定時器,間隔必定時間後登錄事件
-- 批量登錄(number個玩家) 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 func_login_ok(player)
登錄成功主要設置玩家的測試模塊及模塊的測試間隔
心跳函數處理
-- 玩家心跳函數(負責玩家全部測試子模塊的操做) function heartbeat_handler(player)
心跳函數主要負責定時調用相應的測試模塊,每一個模塊都有本身的獨立計時,若是累計時間超過執行一次須要的時間則進的一次調用,並把累計時間清0
-- 每一個玩家的子模塊的時間間隔 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 trace("找不到壓力測試子模塊(%o) 或者 該子模塊未定義'operation'接口!", test_module) test_modules[test_module] = nil end -- 累計時間清零,從新計算 accumulate[test_module] = 0 end else trace("要求測試的壓力子模塊(%o)並未定義!", test_module) test_modules[test_module] = nil end end -- 保存累計時間 player:set("accumulate", accumulate)
測試模塊要求
測試模塊需實現下面兩個方法
-- 達到每一個玩家的該模塊間隔時間,則調用該函數 function operation(player) --操做的時間間隔,毫秒 function random_interval()
經過豐富測試模塊,及豐富PLAYER_CLASS的數據,能夠達模擬一個玩家正常的操做流程,能夠達到更真實的測試效果。