Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the label column width configurable #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/benchmark/compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion lib/benchmark/ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions lib/benchmark/ips/job/stream_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/benchmark/ips/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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])
Expand All @@ -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.
Expand Down