Skip to content

Commit

Permalink
find and fix particularly nasty bug that lead to real bad output alwa…
Browse files Browse the repository at this point in the history
…ys appending 0 ns
  • Loading branch information
PragTob committed Dec 22, 2023
1 parent 3af6a7b commit 79dd7e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/benchee/conversion/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ defmodule Benchee.Conversion.Duration do
iex> format_human(0)
"0 ns"
iex> format_human(2 * 1000 * 1000 * 1000)
"2 s"
"""
def format_human(duration) do
Format.format_human(duration, __MODULE__)
Expand Down
5 changes: 5 additions & 0 deletions lib/benchee/conversion/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ defmodule Benchee.Conversion.Format do
format(0, module)
end

def format_human(+0.0, module) do
format(0, module)
end

def format_human(number, module) when is_number(number) do
number
|> split_into_place_values(module)
Expand All @@ -108,6 +112,7 @@ defmodule Benchee.Conversion.Format do

@spec place_values(number, [Unit.t()]) :: [{number, Unit.t()}]
defp place_values(0, _units), do: []
defp place_values(+0.0, _units), do: []

# smalles unit, carries the decimal
defp place_values(number, [base_unit = %Unit{magnitude: 1}]), do: [{number, base_unit}]
Expand Down
5 changes: 5 additions & 0 deletions test/benchee/conversion/duration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ defmodule Benchee.Conversion.DurationTest do
assert format_human(9_008) == "9 μs 8 ns"
end

# particularly nasty bug
test ".format_human()" do
assert format_human(2_000_000_000.0) == "2 s"
end

test ".format_human(9_876.54321)" do
assert format_human(9_876.54321) == "9 μs 876.54 ns"
end
Expand Down

0 comments on commit 79dd7e3

Please sign in to comment.