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

Automatically compute the max label width to always properly align the output #142

Merged
merged 2 commits into from
Jan 31, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2, head, jruby, truffleruby]
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2, 3.3, 3.4, head, jruby, truffleruby]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 4 additions & 2 deletions lib/benchmark/compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ module Compare
def compare(*entries, order: :fastest)
return if entries.size < 2

max_width = entries.map { |e| e.label.to_s.size }.max

case order
when :baseline
baseline = entries.shift
Expand All @@ -69,12 +71,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 "%#{max_width}s: %10.1f i/s\n", baseline.label.to_s, baseline.stats.central_tendency

sorted.each do |report|
name = report.label.to_s

$stdout.printf "%20s: %10.1f i/s - ", name, report.stats.central_tendency
$stdout.printf "%#{max_width}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
11 changes: 9 additions & 2 deletions lib/benchmark/ips/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Job
# @return [Integer]
attr_accessor :confidence

# The maximum label width
# @return [Integer]
attr_reader :max_width

# Silence output
# @return [Boolean]
def quiet
Expand All @@ -72,6 +76,7 @@ def initialize opts={}
@compare_order = :fastest
@held_path = nil
@held_results = nil
@max_width = 20 # automatically computed as entries are added

@timing = Hash.new 1 # default to 1 in case warmup isn't run
@full_report = Report.new
Expand All @@ -85,7 +90,7 @@ def initialize opts={}
@stats = :sd
@confidence = 95

@out = MultiReport.new(StreamReport.new)
@out = MultiReport.new(StreamReport.new(self))
end

# Job configuration options, set +@warmup+ and +@time+.
Expand All @@ -106,7 +111,7 @@ def quiet=(val)
if val # remove instances of StreamReport
@out.quiet!
else # ensure there is an instance of StreamReport
@out << StreamReport.new if @out.quiet?
@out << StreamReport.new(self) if @out.quiet?
end
end

Expand Down Expand Up @@ -180,6 +185,8 @@ def item(label="", str=nil, &blk) # :yield:
action = str || blk
raise ArgumentError, "no block or string" unless action

@max_width = label.size if label.size > @max_width

@list.push Entry.new(label, action)
self
end
Expand Down
20 changes: 4 additions & 16 deletions lib/benchmark/ips/job/stream_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module Benchmark
module IPS
class Job
class StreamReport
def initialize(stream = $stdout)
def initialize(job, stream = $stdout)
@last_item = nil
@out = stream
@job = job
end

def start_warming
Expand All @@ -17,8 +18,9 @@ def start_running
end

def warming(label, _warmup)
@out.print rjust(label)
@out.print label.to_s.rjust(@job.max_width)
end
alias_method :running, :warming

def warmup_stats(_warmup_time_us, timing)
case format
Expand All @@ -29,8 +31,6 @@ def warmup_stats(_warmup_time_us, timing)
end
end

alias_method :running, :warming

def add_report(item, caller)
@out.puts " #{item.body}"
@last_item = item
Expand All @@ -48,18 +48,6 @@ def footer
def format
Benchmark::IPS.options[:format]
end

# Add padding to label's right if label's length < 20,
# Otherwise add a new line and 20 whitespaces.
# @return [String] Right justified label.
def rjust(label)
label = label.to_s
if label.size > 20
"#{label}\n#{' ' * 20}"
else
label.rjust(20)
end
end
end
end
end
Expand Down