4dc77717
zhouhaihai
压测
|
1
2
3
4
5
6
7
|
require("utils.StringUtil")
require("utils.TableUtil")
require "utils.MathUtil"
local config = require "robot_config"
local skynet = require "skynet"
local netpack = require "skynet.netpack"
local socketdriver = require "skynet.socketdriver"
|
cbfd0513
zhouhaihai
增加池子
|
8
|
local deque = require "deque"
|
4dc77717
zhouhaihai
压测
|
9
10
|
local string_format = string.format
|
cbfd0513
zhouhaihai
增加池子
|
11
12
|
local poold, pooldObj, onlineCount, startId, endId, inpre
local factory
|
3a646dea
zhouhaihai
拆分网关
|
13
|
|
4dc77717
zhouhaihai
压测
|
14
15
16
17
|
local queue = {}
local fd2serv = {}
local id2fd = {}
local MSG = {}
|
cbfd0513
zhouhaihai
增加池子
|
18
|
local id_max
|
4dc77717
zhouhaihai
压测
|
19
20
21
|
function MSG.open( ... )
|
a5fa0074
zhouhaihai
robot
|
22
|
log("open", ...)
|
4dc77717
zhouhaihai
压测
|
23
24
25
|
end
function MSG.close(fd)
|
7d31daa8
zhouhaihai
调整压测
|
26
27
|
if fd2serv[fd] and not fd2serv[fd].closing then
fd2serv[fd].closing = true
|
7d31daa8
zhouhaihai
调整压测
|
28
|
skynet.call(fd2serv[fd].agent, "lua", "exit")
|
cbfd0513
zhouhaihai
增加池子
|
29
|
pcall(skynet.send, poold, "lua", "feed")
|
4dc77717
zhouhaihai
压测
|
30
|
|
cbfd0513
zhouhaihai
增加池子
|
31
|
log(string_format("logout %s", fd2serv[fd].id))
|
4dc77717
zhouhaihai
压测
|
32
33
34
35
36
37
|
id2fd[fd2serv[fd].id] = nil
fd2serv[fd] = nil
end
end
function MSG.error(fd, msg)
|
a5fa0074
zhouhaihai
robot
|
38
|
log("MSG.error", fd, msg)
|
4dc77717
zhouhaihai
压测
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
MSG.close(fd)
end
function MSG.data(fd, msg, sz)
if fd2serv[fd] then
skynet.redirect(fd2serv[fd].agent, 0, "client", 0, msg, sz)
end
end
local function dispatch_queue()
local fd, msg, sz = netpack.pop(queue)
if fd then
skynet.fork(dispatch_queue)
MSG.data(fd, msg, sz)
for fd, msg, sz in netpack.pop, queue do
MSG.data(fd, msg, sz)
end
end
end
MSG.more = dispatch_queue
skynet.register_protocol {
name = "socket",
id = skynet.PTYPE_SOCKET,
unpack = function (msg, sz)
return netpack.filter(queue, msg, sz)
end,
dispatch = function (_, _, q, type, ...)
queue = q
if type then
MSG[type](...)
end
end
}
skynet.register_protocol {
name = "client",
id = skynet.PTYPE_CLIENT,
}
local function add_robot()
|
cbfd0513
zhouhaihai
增加池子
|
80
81
|
local time = skynet.time()
local robot = factory:pop()
|
4dc77717
zhouhaihai
压测
|
82
83
84
|
local fd = socketdriver.connect(config.host, config.port)
socketdriver.start(fd)
local id
|
3a646dea
zhouhaihai
拆分网关
|
85
|
if id_max >= endId then
|
4dc77717
zhouhaihai
压测
|
86
|
while true do
|
3a646dea
zhouhaihai
拆分网关
|
87
|
id = math.randomInt(startId, endId)
|
4dc77717
zhouhaihai
压测
|
88
89
90
91
92
93
94
95
96
97
|
if not id2fd[id] then
break
end
end
else
id_max = id_max + 1
id = id_max
end
fd2serv[fd] = {agent = robot, id = id}
id2fd[id] = fd
|
3a646dea
zhouhaihai
拆分网关
|
98
|
pcall(skynet.call, robot, "lua", "start", fd, id, prefix)
|
4dc77717
zhouhaihai
压测
|
99
|
log(string_format("login %s", fd2serv[fd].id))
|
4dc77717
zhouhaihai
压测
|
100
101
|
-- 定时下线
skynet.timeout(math.randomInt(config.online_time[1], config.online_time[2]) * 100, function()
|
4dc77717
zhouhaihai
压测
|
102
|
MSG.close(fd)
|
7d31daa8
zhouhaihai
调整压测
|
103
|
socketdriver.close(fd)
|
4dc77717
zhouhaihai
压测
|
104
105
106
107
108
109
110
111
112
113
|
end)
end
local function online()
local curCount = 0
for _, _ in pairs(fd2serv) do
curCount = curCount + 1
end
-- 及时补充人数
|
3a646dea
zhouhaihai
拆分网关
|
114
115
|
if curCount < onlineCount then
for i = curCount + 1, onlineCount do
|
4dc77717
zhouhaihai
压测
|
116
117
118
119
120
121
122
123
|
add_robot()
end
end
-- log(string_format("online %s", curCount))
skynet.timeout(100, online)
end
|
cbfd0513
zhouhaihai
增加池子
|
124
|
local function start(pooldp, pooldObj, onlineCountp, startIdp, endIdp, inprep)
|
4dc77717
zhouhaihai
压测
|
125
126
|
log("start testing ...")
|
cbfd0513
zhouhaihai
增加池子
|
127
128
129
130
|
factory = deque.clone(pooldObj)
poold, onlineCount, startId, endId, inpre = pooldp, onlineCountp, startIdp, endIdp, inprep
id_max = startId - 1
|
3a646dea
zhouhaihai
拆分网关
|
131
132
|
for i = 1, onlineCount, inpre do
for j = 1, inpre do
|
d9c023b8
zhouhaihai
压测
|
133
134
135
|
if i + j - 1 > onlineCount then
break
end
|
4dc77717
zhouhaihai
压测
|
136
137
138
139
140
141
142
143
144
145
|
add_robot()
end
skynet.sleep(100)
end
-- 每秒检查补充在线人数
online()
end
skynet.start(function()
|
cbfd0513
zhouhaihai
增加池子
|
146
147
148
149
150
151
|
skynet.dispatch("lua", function (_, _, command, ...)
if command == "start" then
start(...)
return
end
end)
|
4dc77717
zhouhaihai
压测
|
152
|
end)
|