Skip to content

Commit

Permalink
Improve report to show duration/i (e.g: 12.34 ns/i).
Browse files Browse the repository at this point in the history
```
Warming up --------------------------------------
          string.new     1.485M i/100ms
       string.concat     1.109M i/100ms
        string.split   568.355k i/100ms
Calculating -------------------------------------
          string.new    43.90 ns/i    22.780M (± 4.9%) i/s -     46.044M in   2.027883s
       string.concat    63.84 ns/i    15.664M (± 6.5%) i/s -     32.168M in   2.065552s
        string.split   148.87 ns/i     6.717M (± 5.1%) i/s -     13.641M in   2.036661s
```
  • Loading branch information
huacnlee committed Dec 20, 2023
1 parent 0f68e60 commit 58450fb
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
"%.2f ns" % duration_ns
elsif duration_ns < 1_000_000
"%.2f μs" % (duration_ns / 1000)
elsif duration_ns < 1_000_000_000
"%.2f ms" % (duration_ns / 1_000_000)
else
"%.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).rjust(10)]

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 58450fb

Please sign in to comment.