Skip to content
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
14 changes: 10 additions & 4 deletions lib/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ def benchmark(caption = "", label_width = nil, format = nil, *labels) # :yield:
label_width ||= 0
label_width += 1
format ||= FORMAT
print ' '*label_width + caption unless caption.empty?
report = Report.new(label_width, format)
results = yield(report)

print " " * report.width + caption unless caption.empty?
report.list.each { |i|
print i.label.to_s.ljust(report.width)
print i.format(report.format, *format)
}

Array === results and results.grep(Tms).each {|t|
print((labels.shift || t.label || "").ljust(label_width), t.format(format))
}
Expand Down Expand Up @@ -374,16 +380,16 @@ def initialize(width = 0, format = nil)
# formatting rules.
#
def item(label = "", *format, &blk) # :yield:
print label.to_s.ljust(@width)
w = label.to_s.length
@width = w if @width < w
@list << res = Benchmark.measure(label, &blk)
print res.format(@format, *format)
res
end

alias report item

# An array of Benchmark::Tms objects representing each item.
attr_reader :list
attr_reader :width, :format, :list
end


Expand Down
4 changes: 2 additions & 2 deletions test/benchmark/test_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def test_tms_wont_modify_the_format_String_given

def test_benchmark_does_not_print_any_space_if_the_given_caption_is_empty
assert_equal(<<-BENCH, capture_bench_output(:benchmark))
first --time-- --time-- --time-- ( --time--)
first --time-- --time-- --time-- ( --time--)
second --time-- --time-- --time-- ( --time--)
third --time-- --time-- --time-- ( --time--)
third --time-- --time-- --time-- ( --time--)
BENCH
end

Expand Down