Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created October 25, 2011 14:31
Show Gist options
  • Save thinkerbot/1312929 to your computer and use it in GitHub Desktop.
Save thinkerbot/1312929 to your computer and use it in GitHub Desktop.
Shell timeout test for PTY exits
require 'pty'
#
# To add to results:
#
# echo "# OS" >> results.txt
# ruby example.rb | tee -a results.txt
#
shells = ARGV
shells = %w{/bin/bash /bin/csh /bin/ksh /bin/zsh} if shells.empty?
timeout = 3
$stderr.puts "0. shell"
$stderr.puts "1. PTY - select on slave"
$stderr.puts "2. PTY - select on slave, clearing the slave after kill"
$stderr.puts "3. PTY - select on slave, closing streams after kill"
$stderr.puts "* timeout waiting on slave"
puts "# #{`ruby --version`}"
shells.each do |shell|
printf "#{shell}\t"
`which '#{shell}'`
unless $?.exitstatus == 0
printf "%8s\t%8s\t%8s\n" % %w{- - -}
next
end
# 1. PTY - select on slave
PTY.spawn(shell) do |slave, master, pid|
unless IO.select([slave],nil,nil,timeout)
printf "* "
end
start = Time.now
Process.kill(9, pid)
Process.wait(pid) rescue PTY::ChildExited
printf "%.6f\t", Time.now - start
end
# 2. PTY - select on slave, clearing the slave after kill
PTY.spawn(shell) do |slave, master, pid|
unless IO.select([slave],nil,nil,timeout)
printf "* "
end
start = Time.now
Process.kill(9, pid)
slave.read rescue Errno::EIO
Process.wait(pid) rescue PTY::ChildExited
printf "%.6f\t", Time.now - start
end
# 3. PTY - select on slave, closing streams after kill
PTY.spawn(shell) do |slave, master, pid|
unless IO.select([slave],nil,nil,timeout)
printf "* "
end
start = Time.now
Process.kill(9, pid)
master.close
slave.close
Process.wait(pid) rescue PTY::ChildExited
printf "%.6f\n", Time.now - start
end
end
# OS X 10.5.8
# ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin9.8.0]
/bin/bash 0.600294 0.000258 0.057034
/bin/csh 0.600335 0.000334 0.055263
/bin/ksh 0.600467 0.000260 0.060063
/bin/zsh 0.600302 0.000279 0.053442
# OS X 10.6.8
# ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin10.4.0]
/bin/bash 0.601312 0.000303 0.056619
/bin/csh 0.601657 0.000363 0.050967
/bin/ksh 0.602031 0.000352 0.057968
/bin/zsh 0.601496 0.000373 0.051002
# Ubuntu 11.04
# ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
/bin/bash 0.001165 0.001289 0.001075
/bin/csh 0.100429 0.100702 0.000523
/bin/ksh 0.001285 0.001559 0.001251
/bin/zsh 0.001535 0.001573 0.001559
# Ubuntu 11.10
# ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
/bin/bash 0.012751 0.030525 0.036334
/bin/csh 0.055288 0.057541 0.047337
/bin/ksh 0.059810 0.060154 0.042831
/bin/zsh 0.037786 0.001283 0.049365
# SLES 10
# ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-linux]
/bin/bash 0.039185 0.001369 0.026253
/bin/csh 0.060401 0.038210 0.013586
/bin/ksh 0.032744 0.001268 0.061503
/bin/zsh 0.043043 0.009338 0.021305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment