Skip to content

Commit

Permalink
Some minor tweaks to the setup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kellyredding committed Nov 9, 2013
1 parent a4d5a7b commit 89f4605
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
24 changes: 12 additions & 12 deletions bench/results.txt
Expand Up @@ -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
-----------------
Expand All @@ -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
------------------
Expand All @@ -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
-------------------
Expand All @@ -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
---------------------------------------------
Expand All @@ -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
----------------------------------------------
Expand All @@ -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
-----------------------------------------------
Expand All @@ -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
------------------------------------------------
Expand All @@ -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

23 changes: 13 additions & 10 deletions lib/scmd/command.rb
Expand Up @@ -19,7 +19,7 @@ class Command

def initialize(cmd_str)
@cmd_str = cmd_str
setup
reset_attrs
end

def run(input = nil)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 89f4605

Please sign in to comment.