Class: Gruf::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/timer.rb

Overview

Times a given block and returns the result and the elapsed time as a Float

result = Timer.time { do_my_thing }

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.timeTimer::Result

Times a given block by recording start and end times

result = Timer.time { do_my_thing }

Returns:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gruf/timer.rb', line 71

def self.time
  start_time = Time.now
  begin
    result = yield
  rescue GRPC::BadStatus, StandardError, GRPC::Core::CallError => e
    result = e
  end
  end_time = Time.now
  elapsed = (end_time - start_time) * 1000.0
  Result.new(result, elapsed)
end