Class: Urltest::Commands::Execute

Inherits:
Object
  • Object
show all
Defined in:
lib/urltest/commands.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Execute

Returns a new instance of Execute.



11
12
13
# File 'lib/urltest/commands.rb', line 11

def initialize(argv)
  @options = Options.new(argv).parse
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/urltest/commands.rb', line 9

def options
  @options
end

Instance Method Details

#benchmarkObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# 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