Advertisement
airevent

fixed evoyager

Dec 9th, 2011
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.42 KB | None | 0 0
  1. -- stress test base class
  2.  
  3. require "std"
  4. require "oop"
  5. require "etc"
  6. require "net"
  7.  
  8.  
  9.  
  10. local c,p = class:TcpStress {
  11.     host = "localhost",
  12.     port = 7000,
  13.     count = 1000,
  14.     threads = 10,
  15.     objID_from = 0,
  16.     password = "12345678",
  17.     version = "VER 07.503.503",
  18.     complex_timeout_sec = 0,
  19.     complex_timeout_usec = 100000,
  20.     epolls = {},
  21.     clients = {},
  22.     cgroups = {},
  23.     timer = 0,
  24.     sent = 0,
  25.     received = 0,
  26.     total_timer = 0,
  27.     total_sent = 0,
  28.     total_received = 0,
  29.     eop = "\r\nOK\r\n",
  30. }
  31.  
  32. c.stages = {
  33.     READY   = 1,
  34.     VERSION = 2,
  35.     COMPLEX = 3,
  36.     SENDOK  = 4,
  37. }
  38.  
  39. c.packets = {
  40.     READY   = "\r\nREADY\r\n",
  41.     VERSION = "\r\n+v\r\n",
  42.     COMPLEX = "\r\n+complex\r\n",
  43.     SENDOK  = "\r\nsend ok\r\n",
  44. }
  45.  
  46. c.history = {
  47.     0x30, 0x30, 0x33, 0x43, 0x3e, 0x1e, 0x3e, 0x25,
  48.     0x25, 0x1c, 0x81, 0x81, 0x1c, 0x14, 0x16, 0x18,
  49.     0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26, 0x28,
  50.     0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x33, 0x35, 0x37,
  51.     0x39, 0x3b, 0x3d, 0x3f, 0x41, 0x43, 0x45, 0x47,
  52.     0x49, 0x4b, 0x4d, 0xb8, 0xac, 0xa0, 0x94, 0x88,
  53.     0x7c, 0x70, 0x64, 0x58, 0x4c, 0x40, 0x34, 0x28,
  54.     0x1c, 0x10, 0x04, 0xf8, 0xec, 0xe0, 0xd4, 0xc8,
  55.     0xbc, 0xb0, 0xa4, 0x98, 0x8c, 0x80, 0x74, 0x68,
  56.     0x5c, 0x2d, 0x2d, 0x1c, 0xda, 0xda, 0x1c, 0x2d,
  57.     0x31, 0x35, 0x39, 0x3d, 0x41, 0x45, 0x49, 0x4d,
  58.     0x51, 0x54, 0x58, 0x5c, 0x60, 0x64, 0x68, 0x6c,
  59.     0x70, 0x74, 0x78, 0x7c, 0x7f, 0x83, 0x87, 0x8b,
  60.     0x8f, 0x93, 0x97, 0x9b, 0x9f, 0xec, 0xd4, 0xbc,
  61.     0xa4, 0x8c, 0x74, 0x5c, 0x44, 0x2c, 0x14, 0xfc,
  62.     0xe4, 0xcc, 0xb4, 0x9c, 0x84, 0x6c, 0x54, 0x3c,
  63.     0x24, 0x0c, 0xf4, 0xdc, 0xc4, 0xac, 0x94, 0x7c,
  64.     0x64, 0x4c, 0x34, 0x01, 0x01, 0x1c, 0x64, 0x64,
  65.     0x1c, 0x4b, 0x4b, 0x1c, 0x01, 0x01, 0x1c, 0xb7,
  66.     0xb7, 0x1c, 0x7b, 0x7b, 0x1c, 0x00, 0x00, 0x1c,
  67.     0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00,
  68.     0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00,
  69.     0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c,
  70.     0x00, 0x00, 0x1c, 0x3b, 0x3b, 0x1c, 0x01, 0x01,
  71.     0x1c, 0x13, 0x30, 0x4d, 0x6a, 0x87, 0xa4, 0xc1,
  72.     0xde, 0xfb, 0x18, 0x36, 0x53, 0x70, 0x8d, 0xaa,
  73.     0xc7, 0xe5, 0x02, 0x1f, 0x3c, 0x59, 0x77, 0x94,
  74.     0xb1, 0xce, 0xeb, 0x08, 0x25, 0x42, 0x5f }
  75.  
  76. c.historychanged = false
  77.  
  78.  
  79.  
  80. function p:init()
  81.     self:prepare_history()
  82.     self:init_clients()
  83.     self:init_epolls()
  84.     thread.newthread(self.printer, {self})
  85.     self.timer = etc.microtime()
  86.     self.total_timer = self.timer
  87. end
  88.  
  89.  
  90.  
  91. function p:prepare_history()
  92.     if c.historychanged then return end
  93.     c.historychanged = true
  94.    
  95.     for i,chr in ipairs(c.history) do
  96.         c.history[i] = string.char(chr)
  97.     end
  98.    
  99.     c.history = table.concat(c.history, "")
  100. end
  101.  
  102.  
  103.  
  104. -- nonblock client socket creation + binding to epolls
  105. function p:init_clients()
  106.     local spt = math.ceil(self.count / self.threads) -- sockets per thread/epoll
  107.     local n = 1 -- socket number
  108.     local cid, tid -- client id, thread id
  109.    
  110.     for tid=1,self.threads do
  111.         self.epolls[tid] = assert(net.epoll())
  112.         self.cgroups[tid] = {}
  113.        
  114.         for i=1,spt do
  115.             cid = i + spt*(tid-1)
  116.             if cid > self.count then return end
  117.            
  118.             local s,fd = assert(net.tcp.client(self.host, self.port, 0))
  119.             assert(self.epolls[tid]:watch(s,1))
  120.            
  121.             local objID = tostring(self.objID_from + cid)
  122.             while #objID < 4 do objID = "0"..objID end
  123.            
  124.             local client = {
  125.                 socket = s,
  126.                 connected = false,
  127.                 stage = c.stages.READY,
  128.                 objID = objID,
  129.                 buffer = "",
  130.             }
  131.            
  132.             self.clients[fd] = client
  133.             self.cgroups[tid][fd] = client
  134.         end
  135.     end
  136. end
  137.  
  138.  
  139.  
  140. -- start all epolls
  141. function p:init_epolls()
  142.     local void = function() end
  143.     local onread, onwrite, onclose, ontimeout, onerror = void, void, void, void, void
  144.    
  145.     for tid=1,self.threads do
  146.         local epoll = self.epolls[tid]
  147.         if not epoll then return end
  148.        
  149.         onerror = function( err )
  150.             if type(err) == "number" then
  151.                 print("Fatal error: cant connect")
  152.             else
  153.                 print("Fatal error: "..err)
  154.             end
  155.             os.exit(1)
  156.         end
  157.        
  158.         onread = function( fd )
  159.             local client = self.clients[fd]
  160.             if not client.connected then client.connected = true end
  161.            
  162.             while true do
  163.                 local msg,len = client.socket:read()
  164.                 if not msg then break end
  165.                
  166.                 if len == 0 then
  167.                     error("client was dropped")
  168.                 end
  169.                
  170.                 client.buffer = client.buffer .. msg
  171.                 self:client_process(client)
  172.             end
  173.         end
  174.        
  175.         thread.newthread(epoll.start, {epoll, -1, onread, onwrite, onclose, ontimeout, onerror})
  176.     end
  177. end
  178.  
  179.  
  180.  
  181. function p:client_process( client )
  182.     if client.stage == c.stages.READY then
  183.         if client.buffer:sub(1,#c.packets.READY) == c.packets.READY then
  184.             client.buffer = client.buffer:sub(#c.packets.READY+1)
  185.             client.stage = c.stages.VERSION
  186.             self:client_process(client)
  187.         else
  188.             trace(client.buffer)
  189.             error("wrong buf")
  190.         end
  191.     elseif client.stage == c.stages.VERSION then
  192.         if client.buffer == c.packets.VERSION then
  193.             client.buffer = ""
  194.             client.socket:write(self.version..self.eop)
  195.             client.stage = c.stages.COMPLEX
  196.         else
  197.             trace(client.buffer)
  198.             error("wrong buf")
  199.         end
  200.     elseif client.stage == c.stages.COMPLEX then
  201.         if client.buffer == c.packets.COMPLEX then
  202.             client.buffer = ""
  203.             client.socket:write("complex>"..client.objID..self.password.."000000023F"..self.eop)
  204.             etc.sleep(self.complex_timeout_sec,self.complex_timeout_usec)
  205.             client.socket:write(c.history..self.eop)
  206.             self.sent = self.sent + 1
  207.             self.total_sent = self.total_sent + 1
  208.             client.stage = c.stages.SENDOK
  209.         else
  210.             trace(client.buffer)
  211.             error("wrong buf")
  212.         end
  213.     elseif client.stage == c.stages.SENDOK then
  214.         if client.buffer == c.packets.SENDOK then
  215.             client.buffer = ""
  216.             client.socket:write(c.history..self.eop)
  217.             self.sent = self.sent + 1
  218.             self.received = self.received + 1
  219.             self.total_sent = self.total_sent + 1
  220.             self.total_received = self.total_received + 1
  221.         else
  222.             trace(client.buffer)
  223.             error("wrong buf")
  224.         end
  225.     else
  226.         error("unknown stage")
  227.     end
  228. end
  229.  
  230.  
  231.  
  232. --
  233. function p:printer()
  234.     while true do
  235.         etc.sleep(1)
  236.        
  237.         local time = etc.microtime() - self.timer
  238.         local total_time = etc.microtime() - self.total_timer
  239.        
  240.         io.write("\r", string.timer(total_time),
  241.             "; t-recvd:", self.total_received,
  242.             "; t-rps:", math.precision(self.total_received/total_time),
  243.             "; recvd:", self.received,
  244.             "; rps:", math.precision(self.received/time),
  245.             "; t-sent:", self.total_sent,
  246.             "; t-sps:", math.precision(self.total_sent/total_time),
  247.         "  ")
  248.         io.flush()
  249.        
  250.         if time > 5 then
  251.             self.timer = etc.microtime()
  252.             self.received = 0
  253.             self.sent = 0
  254.         end
  255.     end
  256. end
  257.  
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement