Skip to content

Instantly share code, notes, and snippets.

@zhaozg
Created October 11, 2014 08:00
Show Gist options
  • Save zhaozg/ee6505773dbe6f7b632d to your computer and use it in GitHub Desktop.
Save zhaozg/ee6505773dbe6f7b632d to your computer and use it in GitHub Desktop.
luv_test_cli.lua
local uv = require('luv')
local os = os
local print = print
---timer----------
local function time_tick(ms, on_timer)
uv.timer_start(uv.new_timer(), ms, ms, on_timer)
end
----connect------------------------------------------
local function connect(host, port, connected_cb)
local client = uv.new_tcp()
uv.tcp_connect(client, host, port, connected_cb)
return client
end
----test policy support----
local callback = {}
local simple;
simple = {
msg = string.rep('*',8012),
on_timer = function(timer)
print('SEND',#simple.msg, 'bytes')
uv.write(simple.client,simple.msg)
end,
on_connected = function(self,err)
if err then
print(err)
self:close()
return
end
uv.read_start(self, function (self, err, chunk)
if err then
print(err)
end
if chunk then
print('RECV',#chunk,'bytes')
else
uv.close(self)
end
end)
end,
run = function(host,port)
simple.client = connect(host,port, simple.on_connected)
time_tick(1000,simple.on_timer)
end
}
callback.simple = simple
---speed policy-------
local speed
speed = {
msg = string.rep('*',8012),
on_connected = function(self,err)
if err then
print(err)
self:close()
return
end
uv.read_start(self, function (self, err, chunk)
if err then
print(err)
end
if chunk and #chunk>0 then
uv.write(self,chunk)
else
uv.close(self)
end
end)
uv.write(self,speed.msg)
end,
run = function(host,port)
local c
if not arg[4] then
print('input concurrent connect,default use 5')
c = tonumber(io.read())
c = c and c or 5
end
for i=1,c do
client = connect(host,port, speed.on_connected)
end
if speed.on_timer then
time_tick(1000,speed.on_timer)
end
end
}
callback.speed = speed
---------keep---
local keep
keep = {
msg = string.rep('*',8012),
on_timer = function(timer)
for i=1,keep.step do
client = connect(callback.host, callback.port, keep.on_connected)
end
end,
on_connected = function(self,err)
if err then
print(err)
self:close()
return
end
uv.read_start(self, function (self, err, chunk)
if err then
print(err)
end
if chunk and #chunk>0 then
uv.write(self,chunk)
else
uv.close(self)
end
end)
end,
run = function(host,port)
local c
if not arg[4] then
print('input connect step count,default use 500')
c = tonumber(io.read())
c = c and c or 500
end
for i=1,c do
client = connect(host,port, keep.on_connected)
end
keep.step = c
if keep.on_timer then
time_tick(1000,keep.on_timer)
end
end
}
callback.keep = keep
-----new connection
local newc
newc = {
msg = string.rep('*',8012),
on_timer = function(timer)
for i=1,newc.step do
client = connect(callback.host, callback.port, newc.on_connected)
end
end,
on_connected = function(self,err)
if err then
print(err)
self:close()
return
end
uv.read_start(self, function (self, err, chunk)
if err then
print(err)
end
if chunk and #chunk>0 then
uv.write(self,chunk)
else
uv.close(self)
end
self:close()
end)
end,
run = function(host,port)
local c
if not arg[4] then
print('input connect step count,default use 4000')
c = tonumber(io.read())
c = c and c or 4000
end
for i=1,c do
client = connect(host,port, newc.on_connected)
end
newc.step = c
if newc.on_timer then
time_tick(1000,newc.on_timer)
end
end
}
callback.newc = newc
---------------------------------------------------------------------
local host = arg[1] or '127.0.0.1'
local port = arg[2] and tonumber(arg[2]) or 8080
local mode = 'simple'
if not arg[1] then
print('input remote host,default to:'..host)
local i = io.read()
host = #i>0 and i or host
end
if not arg[2] then
print('input remote port,default to:'..port)
local i = tonumber(io.read())
port = i and i or port
end
if not arg[3] then
print('input test mode, default use "simple"')
print(' support mode: simple, keep, speed, newc')
mode = io.read()
mode = #mode==0 and 'simple' or mode
end
callback.host = host
callback.port = port
if callback[mode] then
print('CONNECT %s:%d to run %s',host,port,mode)
callback[mode].run(host,port)
else
print('NOT support mode:'..mode)
end
-- Start the main event loop
uv.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment