summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Bennett <keithrbennett@gmail.com>2021-02-07 23:02:52 -0500
committerMarc-Andre Lafortune <github@marc-andre.ca>2021-02-07 23:06:17 -0500
commit3a7ff66abc0c389851ab3c5cd0ac53ebe0647eec (patch)
treeef87c3b97cde2796211d4c385faafd67d14ecf58
parent9328112b9d6622e57eb348a0241d925271486e29 (diff)
[ruby/benchmark] Adds `Tms#to_h`
[Feature #17601]
-rw-r--r--lib/benchmark.rb14
-rw-r--r--test/benchmark/test_benchmark.rb9
2 files changed, 23 insertions, 0 deletions
diff --git a/lib/benchmark.rb b/lib/benchmark.rb
index 5ce9710586..4cfc0d8a5b 100644
--- a/lib/benchmark.rb
+++ b/lib/benchmark.rb
@@ -527,6 +527,20 @@ module Benchmark
[@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
#
diff --git a/test/benchmark/test_benchmark.rb b/test/benchmark/test_benchmark.rb
index 0eb331a1cd..3030bc5dec 100644
--- a/test/benchmark/test_benchmark.rb
+++ b/test/benchmark/test_benchmark.rb
@@ -155,4 +155,13 @@ BENCH
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