Sha256: 279c9e49ec054b800879a5a8352775a0f5994c763e42398c6a0a0d162afbaaf6

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# TestDiff module
module TestDiff
  # estimates and prints how long it will take to empty a queue
  class TimingTracker
    def self.run(queue, &block)
      new(queue).run(&block)
    end

    def initialize(queue)
      @queue = queue
      @original_size = queue_size
    end

    def run
      @start_time = Time.now
      thread = start_timing_thread
      yield
      thread.kill
    end

    private

    def queue_size
      @queue.size
    end

    def seconds_elapsed
      (Time.now - @start_time).to_f
    end

    def start_timing_thread
      Thread.new do
        begin
          do_timing
        rescue => e
          puts "----- Timing failed: #{e.message} -----"
        end
      end
    end

    def do_timing
      puts "Timing #{@original_size} specs"
      sleep_time = 30.0
      until queue_empty?
        last_current_size = queue_size
        sleep(sleep_time)
        current_size = queue_size
        current_completed = last_current_size - current_size
        if current_completed > 0
          est_time_left = (sleep_time / current_completed.to_f) * current_size
          puts "specs left #{current_size}, est time_left: #{est_time_left.to_i}"
        else
          puts "specs left #{current_size}, est time_left: N/A"
        end
      end
    end

    def queue_empty?
      @queue.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
test_diff-0.4.2 lib/test_diff/timing_tracker.rb