Skip to content

Commit

Permalink
handle non existant memory values even in memory output
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob committed May 1, 2018
1 parent 48fdd74 commit 887e7d1
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
43 changes: 38 additions & 5 deletions lib/benchee/formatters/console/memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Benchee.Formatters.Console.Memory do
Conversion,
Conversion.Unit,
Conversion.Count,
Conversion.Memory,
Formatters.Console.Helpers
}

Expand Down Expand Up @@ -114,8 +115,29 @@ defmodule Benchee.Formatters.Console.Memory do
end)
end

@na "N/A"

@spec format_scenario(Scenario.t(), unit_per_statistic, integer, boolean) :: String.t()
defp format_scenario(scenario, units, label_width, hide_statistics)

defp format_scenario(
scenario = %Scenario{memory_usage_statistics: %{sample_size: 0}},
_,
label_width,
_) do

"~*ts~*ts\n"
|> :io_lib.format([
-label_width,
scenario.name,
@average_width,
@na
])
|> to_string
end

defp format_scenario(scenario, %{memory: memory_unit}, label_width, false) do
# IO.inspect(scenario)
%Scenario{
name: name,
memory_usage_statistics: %Statistics{
Expand Down Expand Up @@ -205,13 +227,18 @@ defmodule Benchee.Formatters.Console.Memory do
end)
end

defp calculate_slower_value(0, _), do: "N/A"
defp calculate_slower_value(_, 0), do: "N/A"
defp calculate_slower_value(job_median, reference_median), do: job_median / reference_median
defp calculate_slower_value(job_median, reference_median)
when job_median == 0 or is_nil(job_median) or reference_median == 0 or
is_nil(reference_median) do
@na
end
defp calculate_slower_value(job_median, reference_median) do
job_median / reference_median
end

defp format_comparison(scenario, %{memory: memory_unit}, label_width, "N/A") do
defp format_comparison(scenario, %{memory: memory_unit}, label_width, @na) do
%Scenario{name: name, memory_usage_statistics: %Statistics{median: median}} = scenario
median_format = Helpers.duration_output(median, memory_unit)
median_format = memory_output(median, memory_unit)

"~*s~*s\n"
|> :io_lib.format([-label_width, name, @median_width, median_format])
Expand All @@ -227,6 +254,12 @@ defmodule Benchee.Formatters.Console.Memory do
|> to_string
end

defp memory_output(nil, _unit), do: "N/A"

defp memory_output(memory, unit) do
Memory.format({Memory.scale(memory, unit), unit})
end

defp extended_statistics_report(_, _, _, %{extended_statistics: false}, _), do: []
defp extended_statistics_report(_, _, _, _, true), do: []

Expand Down
77 changes: 77 additions & 0 deletions test/benchee/formatters/console/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,83 @@ defmodule Benchee.Formatters.Console.MemoryTest do

assert [] = Memory.format_scenarios(scenarios, %{})
end

test "it doesn't blow up if some of the values have no statistics (yes that happened)" do
scenarios = [
%Scenario{
name: "First",
memory_usage_statistics: %Statistics{
average: 100.0,
std_dev: 0.0,
std_dev_ratio: 0.0,
median: 100.0,
percentiles: %{99 => 100.0},
sample_size: 10
},
run_time_statistics: %Statistics{}
},
%Scenario{
name: "Second",
memory_usage_statistics: %Statistics{},
run_time_statistics: %Statistics{}
}
]

output =
Enum.join(
Memory.format_scenarios(scenarios, %{
comparison: true,
unit_scaling: :best,
extended_statistics: false
})
)

assert output =~ "First"
assert output =~ ~r/Second.+N\/A/i
assert output =~ "N/A"
end

test "it doesn't blow up if some come back with a median of 0.0" do
scenarios = [
%Scenario{
name: "First",
memory_usage_statistics: %Statistics{
average: 0.0,
median: 0.0,
sample_size: 10,
percentiles: %{99 => 0.0},
std_dev: 0.0,
std_dev_ratio: 0.0
},
run_time_statistics: %Statistics{}
},
%Scenario{
name: "Second",
memory_usage_statistics: %Statistics{
average: 100.0,
median: 100.0,
sample_size: 5,
percentiles: %{99 => 100.0},
std_dev: 5.0,
std_dev_ratio: 0.10
},
run_time_statistics: %Statistics{}
}
]

output =
Enum.join(
Memory.format_scenarios(scenarios, %{
comparison: true,
unit_scaling: :best,
extended_statistics: false
})
)

assert output =~ "First"
assert output =~ "Second"
refute output =~ "x memory usage"
end
end

defp assert_column_width(name, string, expected_width) do
Expand Down

0 comments on commit 887e7d1

Please sign in to comment.