class SimpleSpeedTester
Usage Example: SimpleSpeedTester.measure
(“request to google.com”, times: 10) { HTTParty.get('www.google.com') }
Constants
- VERSION
Public Class Methods
clear!()
click to toggle source
# File lib/simple_speed_tester.rb, line 44 def clear! @profiler_data = {} end
measure(profile_id = "unspecified_task- { || ... }
click to toggle source
@param profile_id [String|Symbol] What key should we use to reference this speed test later? If not specified then we'll just create one. @option times [Integer] (optional) How many times should we run this test? You can run the class method multiple times but you can also set this number to easily run it multiple times in one line.
# File lib/simple_speed_tester.rb, line 11 def measure(profile_id = "unspecified_task-#{Time.now.to_i}", times: 1) times.times do start_time = Time.current result = yield time_elapsed = Time.current - start_time # Add to the profiler's data store add_result(profile_id, time_elapsed.to_f) result end print_summary end
print_profile(profile_id)
click to toggle source
# File lib/simple_speed_tester.rb, line 38 def print_profile(profile_id) return if @profiler_data.blank? puts @profiler_data[profile_id] end
print_summary()
click to toggle source
# File lib/simple_speed_tester.rb, line 30 def print_summary return if @profiler_data.blank? @profiler_data.keys.each do |profile_id| puts "#{profile_id} >>> #{@profiler_data[profile_id]}" end end
summary()
click to toggle source
# File lib/simple_speed_tester.rb, line 26 def summary @profiler_data end
Private Class Methods
add_result(profile_id, time_elapsed)
click to toggle source
# File lib/simple_speed_tester.rb, line 50 def add_result(profile_id, time_elapsed) @profiler_data ||= {} @profiler_data[profile_id] ||= {} @profiler_data[profile_id][:total_time] = @profiler_data[profile_id][:total_time].to_f + time_elapsed.to_f @profiler_data[profile_id][:times_called] = @profiler_data[profile_id][:times_called].to_i + 1 @profiler_data[profile_id][:avg_time] = @profiler_data[profile_id][:total_time] / @profiler_data[profile_id][:times_called] end