Skip to content

Commit

Permalink
Improve report to show duration/i (e.g: 12.24 ns/i).
Browse files Browse the repository at this point in the history
```
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
```
  • Loading branch information
huacnlee committed Dec 20, 2023
1 parent 0f68e60 commit 81e8c7d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/benchmark/ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions lib/benchmark/ips/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/test_benchmark_ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 81e8c7d

Please sign in to comment.