class MicroBench::Configurations
Attributes
formatter[R]
Public Class Methods
new()
click to toggle source
Initialize with a default formatters that changes nothing.
# File lib/micro_bench/configurations.rb, line 7 def initialize @formatter = method(:default_formatter) end
Public Instance Methods
formatter=(value)
click to toggle source
Set formatter to change {MicroBench.duration} results.
Parameters:¶ ↑
- value
-
It can be a proc that receives a duration float in seconds and returns any formatted value. Or it can be one of the default values, +“simple”+, +“mmss”+ or +“human”+. If you set it to
nil
or +“default”+, the formatter will be ignored, and the result will be the raw float.
# File lib/micro_bench/configurations.rb, line 19 def formatter=(value) # This ensures that both Proc and Method can be used there. Or even a user # defined class that responds to call. if value.respond_to?(:call) @formatter = value return end formatter_method = case value.to_s when "", "default" :default_formatter when "simple" :simple_formatter when "mmss" :mmss_formatter when "human" :human_formatter else raise ArgumentError, "formatter must be callable or a default string" end @formatter = method(formatter_method) end
Private Instance Methods
default_formatter(duration)
click to toggle source
# File lib/micro_bench/configurations.rb, line 46 def default_formatter(duration) duration end
human_formatter(duration)
click to toggle source
# File lib/micro_bench/configurations.rb, line 67 def human_formatter(duration) ss = duration.round mm, ss = ss.divmod(60) hh, mm = mm.divmod(60) human_unit = ->(value, name) { "#{value} #{name}#{"s" unless value == 1}" } hours = human_unit[hh, "hour"] minutes = human_unit[mm, "minute"] seconds = human_unit[ss, "second"] human_join = ->(*array) { array[0..-2].join(", ") + " and #{array.last}" } return human_join[hours, minutes, seconds] if hh > 0 return human_join[minutes, seconds] if mm > 0 return seconds end
mmss_formatter(duration)
click to toggle source
# File lib/micro_bench/configurations.rb, line 54 def mmss_formatter(duration) ss, ms = duration.divmod(1) mm, ss = ss.divmod(60) hh, mm = mm.divmod(60) # Format ms to have only 3 digits. ms = (ms * 1_000).round return format("%02d:%02d:%02d.%03d", hh, mm, ss, ms) if hh > 0 return format("%02d:%02d.%03d", mm, ss, ms) if mm > 0 return format("%d.%03d", ss, ms) end
simple_formatter(duration)
click to toggle source
# File lib/micro_bench/configurations.rb, line 50 def simple_formatter(duration) duration.round(2) end