Class: YourKarma::Benchmarker

Inherits:
Object
  • Object
show all
Defined in:
lib/yourkarma/benchmarker.rb

Defined Under Namespace

Classes: ConnectionError

Constant Summary collapse

HTTP_ERRORS =
[Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
Errno::ENETUNREACH, Errno::ETIMEDOUT, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
Net::ProtocolError, SocketError]
DEFAULT_OPTIONS =
{
  timeout:   10,
  benchmark: 'http://yourkarma.com/dashboard'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Benchmarker

Returns a new instance of Benchmarker.



22
23
24
# File 'lib/yourkarma/benchmarker.rb', line 22

def initialize(options = {})
  self.options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/yourkarma/benchmarker.rb', line 20

def options
  @options
end

Instance Method Details

#benchmarkObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yourkarma/benchmarker.rb', line 26

def benchmark
  uri = URI(options[:benchmark])
  Timeout::timeout(options[:timeout]) do
    duration = Benchmark.realtime do
      Net::HTTP.get_response(uri)
    end
    return duration / options[:timeout].to_f
  end
rescue *HTTP_ERRORS => e
  raise ConnectionError
end