diff --git a/lib/benchmark/compare.rb b/lib/benchmark/compare.rb index 51f9952..911506b 100644 --- a/lib/benchmark/compare.rb +++ b/lib/benchmark/compare.rb @@ -69,12 +69,12 @@ def compare(*entries, order: :fastest) $stdout.puts "\nComparison:" - $stdout.printf "%20s: %10.1f i/s\n", baseline.label.to_s, baseline.stats.central_tendency + $stdout.printf "%s: %10.1f i/s\n", baseline.label.to_s.rjust(IPS.width), baseline.stats.central_tendency sorted.each do |report| - name = report.label.to_s + name = report.label.to_s.rjust(IPS.width) - $stdout.printf "%20s: %10.1f i/s - ", name, report.stats.central_tendency + $stdout.printf "%s: %10.1f i/s - ", name, report.stats.central_tendency if report.stats.overlaps?(baseline.stats) $stdout.print "same-ish: difference falls within error" diff --git a/lib/benchmark/ips.rb b/lib/benchmark/ips.rb index fd038e4..20b6c9c 100644 --- a/lib/benchmark/ips.rb +++ b/lib/benchmark/ips.rb @@ -78,7 +78,15 @@ def ips(*args) # :human format narrows precision and scales results for readability # :raw format displays 6 places of precision and exact iteration counts def self.options - @options ||= {:format => :human} + @options ||= { + :format => :human, + :width => 20 + } + end + + # How wide of a column (in characters) to display the labels in (by default 20) + def self.width + options[:width] || 20 end module Helpers @@ -175,3 +183,11 @@ def humanize_duration(duration_ns) # # See also Benchmark::IPS end + +if width = ENV['IPS_WIDTH'] + w = width.to_i + + if w > 0 + Benchmark::IPS.options[:width] = w + end +end diff --git a/lib/benchmark/ips/job/stream_report.rb b/lib/benchmark/ips/job/stream_report.rb index 0258ded..68f35ce 100644 --- a/lib/benchmark/ips/job/stream_report.rb +++ b/lib/benchmark/ips/job/stream_report.rb @@ -54,10 +54,11 @@ def format # @return [String] Right justified label. def rjust(label) label = label.to_s - if label.size > 20 - "#{label}\n#{' ' * 20}" + width = IPS.width + if label.size > width + "#{label}\n#{' ' * width}" else - label.rjust(20) + label.rjust(width) end end end diff --git a/lib/benchmark/ips/report.rb b/lib/benchmark/ips/report.rb index 53cde85..8c309f1 100644 --- a/lib/benchmark/ips/report.rb +++ b/lib/benchmark/ips/report.rb @@ -86,6 +86,7 @@ def error_percentage # percentage of standard deviation, iterations in runtime. # @return [String] Left justified body. def body + width = IPS.width per_iter = (" (%s/i)" % Helpers.humanize_duration(1_000_000_000 / @stats.central_tendency)).rjust(15) case Benchmark::IPS.options[:format] @@ -99,7 +100,7 @@ def body left + per_iter + (" - %s" % iters) end else - left = ("%10.1f (±%.1f%%) i/s" % [@stats.central_tendency, @stats.error_percentage]).ljust(20) + left = ("%10.1f (±%.1f%%) i/s" % [@stats.central_tendency, @stats.error_percentage]).ljust(width) if @show_total_time left + per_iter + (" - %10d in %10.6fs" % [@iterations, runtime]) @@ -109,10 +110,10 @@ def body end end - # Return header with padding if +@label+ is < length of 20. + # Return header with padding if +@label+ is < length of the configured width (by default 20). # @return [String] Right justified header (+@label+). def header - @label.to_s.rjust(20) + @label.to_s.rjust(IPS.width) end # Return string repesentation of Entry object.