Last active
September 6, 2017 22:36
-
-
Save hdenman/b714d42367d1e29f8567e15af0a1bb87 to your computer and use it in GitHub Desktop.
Quick-n-dirty Ruby popen3 ssh benchmark
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
bytes_read = 0 | |
cmd = "ssh -i #{key_file} #{user}@#{ip} 'base64 /dev/urandom'" | |
Open3.popen3(cmd) do |i, o, e, t| # (in, out, error, monitor thread) | |
begin | |
Timeout.timeout(120) do | |
while readables = IO.select([o, e])[0] | |
readables.each{|r| | |
if r == o | |
dump = "" | |
begin | |
o.read_nonblock(4096, dump) | |
rescue IO::WaitReadable | |
ensure | |
bytes_read += dump.length | |
end | |
end | |
if r == e | |
dump = "" | |
begin | |
e.read_nonblock(4096, dump) | |
rescue IO::WaitReadable | |
ensure | |
puts(dump) | |
end | |
end | |
} | |
end | |
Process.wait t.pid | |
end | |
rescue Timeout::Error => ex | |
Process.kill 9, t.pid | |
end | |
end | |
puts "run_ssh: " + bytes_read.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment