From 81e8c7d9cd3fd957d3988f4c99be2d394dc3cc39 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 20 Dec 2023 14:36:03 +0800 Subject: [PATCH] Improve report to show duration/i (e.g: `12.24 ns/i`). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` Warming up -------------------------------------- string.new 1.406M i/100ms string.concat 1.091M i/100ms Calculating ------------------------------------- string.new 45.48 ns/i 21.990M (± 4.0%) i/s - 44.997M in 2.049861s string.concat 64.32 ns/i 15.546M (± 5.9%) i/s - 31.644M in 2.045207s ``` --- lib/benchmark/ips.rb | 13 +++++++++++++ lib/benchmark/ips/report.rb | 10 ++++++---- test/test_benchmark_ips.rb | 8 ++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/benchmark/ips.rb b/lib/benchmark/ips.rb index 5d2492a..130a158 100644 --- a/lib/benchmark/ips.rb +++ b/lib/benchmark/ips.rb @@ -93,6 +93,19 @@ def scale(value) "%10.3f#{suffix}" % scaled_value end module_function :scale + + def humanize_duration(duration_ns) + if duration_ns < 1000 + "%4.2f ns" % duration_ns + elsif duration_ns < 1_000_000 + "%4.2f μs" % (duration_ns / 1000) + elsif duration_ns < 1_000_000_000 + "%4.2f ms" % (duration_ns / 1_000_000) + else + "%4.2f s" % (duration_ns / 1_000_000_000) + end + end + module_function :humanize_duration end end diff --git a/lib/benchmark/ips/report.rb b/lib/benchmark/ips/report.rb index b5102a9..73a12d7 100644 --- a/lib/benchmark/ips/report.rb +++ b/lib/benchmark/ips/report.rb @@ -86,23 +86,25 @@ def error_percentage # percentage of standard deviation, iterations in runtime. # @return [String] Left justified body. def body + iter_duration = " %s/i" % [Helpers.humanize_duration(1_000_000_000 / @stats.central_tendency)] + case Benchmark::IPS.options[:format] when :human left = "%s (±%4.1f%%) i/s" % [Helpers.scale(@stats.central_tendency), @stats.error_percentage] iters = Helpers.scale(@iterations) if @show_total_time - left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime]) + iter_duration + left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime]) else - left.ljust(20) + (" - %s" % iters) + iter_duration + left.ljust(20) + (" - %s" % iters) end else left = "%10.1f (±%.1f%%) i/s" % [@stats.central_tendency, @stats.error_percentage] if @show_total_time - left.ljust(20) + (" - %10d in %10.6fs" % [@iterations, runtime]) + iter_duration + left.ljust(20) + (" - %10d in %10.6fs" % [@iterations, runtime]) else - left.ljust(20) + (" - %10d" % @iterations) + iter_duration + left.ljust(20) + (" - %10d" % @iterations) end end end diff --git a/test/test_benchmark_ips.rb b/test/test_benchmark_ips.rb index 4679da6..20d57ef 100644 --- a/test/test_benchmark_ips.rb +++ b/test/test_benchmark_ips.rb @@ -268,4 +268,12 @@ def test_small_warmup_and_time end assert_equal 1, report.entries[0].iterations end + + def test_humanize_duration + assert_equal Benchmark::IPS::Helpers.humanize_duration(0.000000001), "0.00 ns" + assert_equal Benchmark::IPS::Helpers.humanize_duration(123.456789), "123.46 ns" + assert_equal Benchmark::IPS::Helpers.humanize_duration(12345.67890123), "12.35 μs" + assert_equal Benchmark::IPS::Helpers.humanize_duration(123456789.0123456789), "123.46 ms" + assert_equal Benchmark::IPS::Helpers.humanize_duration(123456789012.3456789012), "123.46 s" + end end