Created
August 27, 2014 10:06
-
-
Save ogibayashi/4c77b4b74286fd75ce0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'parallel' | |
require 'socket' | |
require 'optparse' | |
opts = {} | |
ARGV.options {|opt| | |
opt.on('-h Host', 'host to connect'){|v| opts[:host] = v } | |
opt.on('-p Port', 'port to connect'){|v| opts[:port] = v.to_i } | |
opt.on('-n Num processes', 'Number of client processes'){|v| opts[:procs] = v.to_i } | |
opt.on('-b send bytes', 'Number of bytes to send'){|v| opts[:bytes] = v.to_i } | |
opt.on('--nodelay', 'Set TCP_NODELAY to the socket'){|v| opts[:nodelay] = true } | |
opt.on('-u send_unit', 'Send unit in bytes'){|v| opts[:sendunit] = v.to_i } | |
opt.parse! | |
} | |
opts = { | |
:host => "localhost", | |
:port => 11000, | |
:procs => 10, | |
:bytes => 0, | |
:sendunit => 100 | |
}.merge(opts) | |
$stdout.sync = true | |
puts "Started #{Time.now.to_s}" | |
Parallel.each(opts[:procs].times.to_a,:in_processes => opts[:procs]) do |i| | |
require 'sigdump/setup' | |
puts "connecting #{i}" | |
begin | |
s = TCPSocket.open(opts[:host],opts[:port]) | |
if opts[:nodelay] | |
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) | |
end | |
puts "connected #{i}" | |
if opts[:bytes] > 0 | |
time_before_send = Time.now | |
bytes_to_send = opts[:bytes] | |
send_unit = "a" * opts[:sendunit] | |
while bytes_to_send > 0 | |
if bytes_to_send > send_unit.size | |
s.puts send_unit | |
bytes_to_send -= send_unit.size | |
else | |
s.puts "a" * bytes_to_send | |
bytes_to_send -= bytes_to_send | |
end | |
end | |
s.puts "fin" | |
time_after_send = Time.now | |
puts "Time to send #{opts[:bytes]} bytes: #{time_after_send - time_before_send}, #{opts[:bytes]/ (time_after_send - time_before_send) } Bytes per sec" | |
else | |
sleep 60 | |
end | |
s.close | |
rescue => e | |
puts "#{Time.now.to_s} Exception in connecting: #{i}" | |
puts e | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'socket' | |
require 'optparse' | |
opts = {} | |
ARGV.options {|opt| | |
opt.on('-p Port', 'Port to listen'){|v| opts[:port] = v } | |
opt.on('-b Backlog', 'Backlog size'){|v| opts[:backlog] = v.to_i } | |
opt.on('-a', 'Accept connection'){|v| opts[:accept] = true } | |
opt.on('-s sleep', 'Sleep between reading data'){|v| opts[:sleep] = v.to_f} | |
opt.parse! | |
} | |
opts = { | |
:port => 11000, | |
:backlog => 10, | |
:accept => false, | |
:sleep => 0 | |
}.merge(opts) | |
server = TCPServer.open(opts[:port]) | |
server.listen(opts[:backlog]) | |
while true | |
if opts[:accept] | |
Thread.start(server.accept) do |client| | |
puts "Accepted connection" | |
received_data_size = 0 | |
while buf = client.gets | |
# p buf | |
# received_data_size += buf.size | |
# if /fin/ =~ buf | |
# puts "Received fin" | |
# end | |
if opts[:sleep] > 0 | |
sleep opts[:sleep] | |
end | |
end | |
client.close | |
# puts "Received #{received_data_size} bytes" | |
puts "Closed connection" | |
end | |
else | |
sleep 10 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment