Skip to content

Instantly share code, notes, and snippets.

@funny-falcon
Created June 1, 2012 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funny-falcon/2850641 to your computer and use it in GitHub Desktop.
Save funny-falcon/2850641 to your computer and use it in GitHub Desktop.
Script for testing kgio_*writev
require 'kgio'
ip = '127.0.0.1' # better write here real IP address of your network card
prc = fork do
srv = Kgio::TCPServer.new(ip, 8000)
sock = srv.kgio_accept(Kgio::TCPSocket)
count = 0
b = ''
while s = sock.kgio_read(1024*1024, b)
count += s.size
end
print "COUNT #{count} #{count.class}\n"
end
require 'benchmark'
writev = (ARGV[0] || 's') == 'v'
num = (ARGV[1] || 100).to_i
sz_a = 10000000 / num
sz_b = 3000000 / num
puts "WRITEV #{writev} NUM #{num}"
sock = Kgio::TCPSocket.new(ip, 8000)
a = Array.new(sz_a){ '1'*num }
aa = Array.new(sz_b){ '1'*num }
_n = writev ? [] : ''
realtime = Benchmark.realtime {
b = writev ? a : a.join
10000.times{|i|
case c = (writev ? sock.kgio_trywritev(b) : sock.kgio_trywrite(b))
when Array, String
b = c
when Symbol
sock.kgio_wait_writable
when nil
b = _n.dup
end
if writev
b.concat aa
else
b = [b, *aa].join
end
}
while c = (writev ? sock.kgio_trywritev(b) : sock.kgio_trywrite(b))
case c
when Array, String
b = c
when Symbol
sock.kgio_wait_writable
end
end
sock.close
}
print "Realtime #{realtime}\n"
Process.wait prc
for i in 100 200 400 600 800 1000 4000 16000 64000 128000 256000 512000 1024000; do ruby -Iext/kgio -Ilib tst.rb v $i ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment