From 89f4605d9bc9fe60bdb3f444ffcecb226f65ff55 Mon Sep 17 00:00:00 2001 From: Kelly Redding Date: Fri, 8 Nov 2013 23:10:22 -0600 Subject: [PATCH] Some minor tweaks to the setup This breaks resetting the attributes into its own method and makes the setup/teardown more about setting up or tearing down cmd runs. This is a slight improvement in code organization with no behavior changes. --- bench/results.txt | 24 ++++++++++++------------ lib/scmd/command.rb | 23 +++++++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/bench/results.txt b/bench/results.txt index 923352b..4e38125 100644 --- a/bench/results.txt +++ b/bench/results.txt @@ -2,11 +2,11 @@ echo hi: 1 times ---------------- whysoslow? .. -mem @ start 38 MB ?? -mem @ finish 38 MB + 0 MB, 0% +mem @ start 29 MB ?? +mem @ finish 29 MB + 0 MB, 0% user system total real -time 0.0 ms 0.0 ms 0.0 ms 1.864 ms +time 0.0 ms 0.0 ms 0.0 ms 1.928 ms echo hi: 10 times ----------------- @@ -16,7 +16,7 @@ mem @ start 38 MB ?? mem @ finish 38 MB - 0 MB, 0% user system total real -time 0.0 ms 10.0 ms 20.0 ms 17.165 ms +time 0.0 ms 0.0 ms 10.0 ms 16.874 ms echo hi: 100 times ------------------ @@ -26,7 +26,7 @@ mem @ start 38 MB ?? mem @ finish 38 MB - 0 MB, 0% user system total real -time 20.0 ms 20.0 ms 130.0 ms 150.305 ms +time 20.0 ms 20.0 ms 130.0 ms 147.925 ms echo hi: 1000 times ------------------- @@ -36,7 +36,7 @@ mem @ start 38 MB ?? mem @ finish 41 MB + 3 MB, 7% user system total real -time 180.0 ms 190.0 ms 1360.0 ms 1415.856 ms +time 170.0 ms 180.0 ms 1320.0 ms 1390.406 ms cat test/support/bigger-than-64k.txt: 1 times --------------------------------------------- @@ -46,7 +46,7 @@ mem @ start 41 MB ?? mem @ finish 41 MB - 0 MB, 0% user system total real -time 0.0 ms 0.0 ms 0.0 ms 3.312 ms +time 0.0 ms 0.0 ms 0.0 ms 3.487 ms cat test/support/bigger-than-64k.txt: 10 times ---------------------------------------------- @@ -55,8 +55,8 @@ whysoslow? .. mem @ start 41 MB ?? mem @ finish 43 MB + 2 MB, 4% - user system total real -time 10.0 ms 10.0 ms 40.0 ms 29.29 ms + user system total real +time 0.0 ms 0.0 ms 20.0 ms 30.533 ms cat test/support/bigger-than-64k.txt: 100 times ----------------------------------------------- @@ -65,8 +65,8 @@ whysoslow? .. mem @ start 43 MB ?? mem @ finish 85 MB + 42 MB, 98% - user system total real -time 40.0 ms 40.0 ms 270.0 ms 278.486 ms + user system total real +time 40.0 ms 40.0 ms 250.0 ms 274.06 ms cat test/support/bigger-than-64k.txt: 1000 times ------------------------------------------------ @@ -76,5 +76,5 @@ mem @ start 85 MB ?? mem @ finish 506 MB + 421 MB, 497% user system total real -time 370.0 ms 420.0 ms 2650.0 ms 2710.427 ms +time 360.0 ms 400.0 ms 2540.0 ms 2649.686 ms diff --git a/lib/scmd/command.rb b/lib/scmd/command.rb index 5bf2ce4..e76e8b5 100644 --- a/lib/scmd/command.rb +++ b/lib/scmd/command.rb @@ -19,7 +19,7 @@ class Command def initialize(cmd_str) @cmd_str = cmd_str - setup + reset_attrs end def run(input = nil) @@ -42,11 +42,9 @@ def run!(input = nil) end def start(input = nil) - setup - @stop_r, @stop_w = IO.pipe - @child_process = ChildProcess.new(@cmd_str) - @pid = @child_process.pid.to_i + setup_run + @pid = @child_process.pid.to_i @child_process.write(input) @read_output_thread = Thread.new do while @child_process.check_for_exit @@ -73,7 +71,7 @@ def wait(timeout = nil) @stderr << @child_process.flush_stderr @exitstatus = @child_process.exitstatus - teardown + teardown_run end def stop(timeout = nil) @@ -124,13 +122,18 @@ def wait_for_exit(timeout) @stop_r.read_nonblock(1) if ios && ios.include?(@stop_r) end - def setup + def reset_attrs @stdout, @stderr, @pid, @exitstatus = '', '', nil, nil - @stop_r, @stop_w = nil, nil - @child_process, @read_output_thread = nil, nil end - def teardown + def setup_run + reset_attrs + @stop_r, @stop_w = IO.pipe + @read_output_thread = nil + @child_process = ChildProcess.new(@cmd_str) + end + + def teardown_run @child_process.teardown [@stop_r, @stop_w].each{ |fd| fd.close if fd && !fd.closed? } @stop_r, @stop_w = nil, nil