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: 14 additions & 0 deletions lib/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,20 @@ def to_a
[@label, @utime, @stime, @cutime, @cstime, @real]
end

#
# Returns a hash containing the same data as `to_a`.
#
def to_h
{
label: @label,
utime: @utime,
stime: @stime,
cutime: @cutime,
cstime: @cstime,
real: @real
}
end

protected

#
Expand Down
9 changes: 9 additions & 0 deletions test/benchmark/test_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,13 @@ def test_realtime_output
realtime = Benchmark.realtime { sleep sleeptime }
assert_operator sleeptime, :<, realtime
end

# Test that `to_h` returns a hash with the expected data.
def test_tms_to_h
tms = Benchmark::Tms.new(1.1, 2.2, 3.3, 4.4, 5.5, 'my label')
expected_hash = {
utime: 1.1, stime: 2.2, cutime: 3.3, cstime: 4.4, real: 5.5, label: 'my label'
}
assert_equal(expected_hash, tms.to_h)
end
end