class Urltest::Commands::Execute

Attributes

options[R]

Public Class Methods

new(argv) click to toggle source
# File lib/urltest/commands.rb, line 11
def initialize(argv)
  @options = Options.new(argv).parse
end

Public Instance Methods

benchmark() click to toggle source
# File lib/urltest/commands.rb, line 27
def benchmark
  return unless @options.link

  min_value = 0
  max_value = 0
  average_value = 0

  puts '*' * 50
  @options.times.times do |i|
    s = Time.now
    begin
      r = RestClient.get @options.link
      e = Time.now
      diff = e - s

      if i == 0
        min_value = diff
        max_value = diff
      end

      average_value += diff

      min_value = diff if min_value > diff
      max_value = diff if max_value < diff

      puts "#{i + 1}. Cost #{colorful_text(diff)}s <status #{r.code}, size: #{r.to_s.bytesize} bytes>"
    end
  end

  average_value = average_value.to_f / @options.times
  puts '*' * 50
  puts "Total:\t\t#{@options.times}"
  puts "Min time:\t#{colorful_text(min_value)}s"
  puts "Max time:\t#{colorful_text(max_value)}s"
  puts "Average time:\t#{colorful_text(average_value)}s"
end
colorful_text(value) click to toggle source
# File lib/urltest/commands.rb, line 15
def colorful_text(value)
  color = if value < 0.3
    "green"
  elsif value > 0.8
    "yellow"
  else
    "red"
  end

  Rainbow(value).send(color)
end