blairanderson
1/27/2017 - 5:36 PM

benchmarking methods

benchmarking methods

new_result = old_result = new_ms = old_ms = nil
old_ms = Benchmark.ms do
  old_result = Conversation.slow_method_thing(person_id) 
end

    # 5% of requests
if rand(20) == 1
  # [safely](https://github.com/ankane/safely) is shorthand for begin/rescue
  safely do
    new_ms = Benchmark.ms do
      new_result = Conversation.fast_method_thing(person_id)
    end
    # you can inline this, expanded here for visual reference
    benchdata = {
      new: new_result, 
      old: old_result, 
      new_ms: new_ms, 
      old_ms: old_ms, 
      match: (new_result == old_result), # should always be true
      perf: (new_ms < old_ms ? 'faster' : 'slower') # look for faster/slower
     }
     Rollbar.debug("Benchmark|Conversation.method_thing", benchdata)
  end
end
old_result # return the original result