Module: Perfer::Formatter
- Extended by:
- Formatter
- Included in:
- Formatter, MeasurementsFormatter, ResultsFormatter, SessionFormatter
- Defined in:
- lib/perfer/formatter.rb
Constant Summary collapse
- TIME_UNITS =
{ 0 => "s ", -3 => "ms", -6 => "µs", -9 => "ns" }
Instance Method Summary collapse
- #float_scale(time) ⇒ Object
-
#format_duration(time, scale = float_scale(time)) ⇒ Object
formats a duration with an 8-chars width.
- #format_duration_and_error(time, error, after_unit = "") ⇒ Object
- #format_error(error, base, scale) ⇒ Object
- #format_float(f) ⇒ Object
- #format_ips(ips) ⇒ Object
- #format_n(n, maxlen) ⇒ Object
- #format_time(time) ⇒ Object
- #max_length_of(enum) ⇒ Object
- #ruby_version(desc) ⇒ Object
- #short_ruby_description(desc) ⇒ Object
Instance Method Details
#float_scale(time) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/perfer/formatter.rb', line 47 def float_scale(time) if time == 0 or time > 1.0 0 elsif time > 0.001 -3 elsif time > 0.000001 -6 else -9 end end |
#format_duration(time, scale = float_scale(time)) ⇒ Object
formats a duration with an 8-chars width
60 61 62 63 64 65 66 |
# File 'lib/perfer/formatter.rb', line 60 def format_duration(time, scale = float_scale(time)) if time == 0 " 0 " else "#{format_float(time*10**-scale)} #{TIME_UNITS[scale]}" end end |
#format_duration_and_error(time, error, after_unit = "") ⇒ Object
68 69 70 71 |
# File 'lib/perfer/formatter.rb', line 68 def format_duration_and_error(time, error, after_unit = "") scale = float_scale(time) "#{format_duration(time, scale)}#{after_unit} ± #{format_error(error, time, scale)}" end |
#format_error(error, base, scale) ⇒ Object
39 40 41 |
# File 'lib/perfer/formatter.rb', line 39 def format_error(error, base, scale) "#{format_float(error*10**-scale)} (#{'%4.1f' % (error / base * 100.0)}%)" end |
#format_float(f) ⇒ Object
35 36 37 |
# File 'lib/perfer/formatter.rb', line 35 def format_float(f) ('%5.3f' % f)[0...5] end |
#format_ips(ips) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/perfer/formatter.rb', line 23 def format_ips(ips) if ips > 100 ips.round else "%.3g" % ips end end |
#format_n(n, maxlen) ⇒ Object
31 32 33 |
# File 'lib/perfer/formatter.rb', line 31 def format_n(n, maxlen) n.to_s.rjust(maxlen) end |
#format_time(time) ⇒ Object
43 44 45 |
# File 'lib/perfer/formatter.rb', line 43 def format_time(time) time.strftime("%F %T") end |
#max_length_of(enum) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/perfer/formatter.rb', line 14 def max_length_of(enum) max = 0 enum.each { |e| alt = yield(e).to_s.length max = alt if alt > max } max end |
#ruby_version(desc) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/perfer/formatter.rb', line 73 def ruby_version(desc) # # ruby 2.0.0dev (2012-08-25 trunk 36824) [x86_64-darwin10.8.0] case desc when /^ruby (\d\.\d\.\d) .+ patchlevel (\d+)/ "#{$1}p#{$2}" when /^ruby (\d\.\d\.\d(?:p\d+|\w+)) .+ (?:trunk|revision) (\d+)/ "#{$1} r#{$2}" when /^rubinius .+? \((\d\.\d\.\d) / $1 when /^jruby .+? \(ruby-(\d\.\d\.\d)-p(\d+)\)/, /^jruby .+? \((\d\.\d\.\d)p(\d+)\)/ "#{$1}p#{$2}" when /^MacRuby .+? \(ruby (\d\.\d\.\d)\)/ $1 else raise "Unknown ruby version: #{desc}" end end |
#short_ruby_description(desc) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/perfer/formatter.rb', line 92 def short_ruby_description(desc) impl, version = nil, nil case desc when /Ruby Enterprise Edition (\d{4}\.\d{2})$/ impl, version = "ree", $1 when /^MacRuby (\S+)/ impl, version = "macruby", $1 when /^rubinius (\S+) \(\d\.\d\.\d release (\d{4}-\d{2}-\d{2})/, /^rubinius (\S+) \(\d\.\d\.\d ([0-9a-f]+) / impl, version = "rbx", "#{$1} #{$2}" when /^jruby (\S+) \(.+?\) \(\d{4}-\d{2}-\d{2} ([0-9a-f]+)\)/, /^jruby (\S+) \(.+?\) (\d{4}-\d{2}-\d{2}) f+/, /^jruby (\S+) \(.+?\) \d{4}-\d{2}-\d{2} ([0-9a-f]+)/ impl, version = "jruby", "#{$1} #{$2}" when /^ruby / impl = "mri" else raise "Unknown ruby interpreter: #{desc}" end ruby_version = ruby_version(desc) if version "#{impl} #{version} (#{ruby_version})" else "#{impl} #{ruby_version}" end end |