Skip to content

Commit

Permalink
Mix.Tasks.Profile.Eprof: Protect against division by zero (elixir-lan…
Browse files Browse the repository at this point in the history
…g#6614)

Tests for this Mix task failed on Windows with `(ArithmeticError) bad argument in arithmetic expression`.
  • Loading branch information
robi-wan authored and josevalim committed Sep 29, 2017
1 parent 9191e15 commit 825fb73
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/mix/lib/mix/tasks/profile.eprof.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,23 @@ defmodule Mix.Tasks.Profile.Eprof do

defp format_row({{module, function, arity}, {count, time}}, total_time) do
mfa = Exception.format_mfa(module, function, arity)
time_percentage = :erlang.float_to_binary(100 * time / total_time, [{:decimals, 2}])
time_per_call = :erlang.float_to_binary(time / count, [{:decimals, 2}])
time_percentage = :erlang.float_to_binary(100 * divide(time, total_time), [{:decimals, 2}])
time_per_call = :erlang.float_to_binary(divide(time, count), [{:decimals, 2}])
count = Integer.to_string(count)
time = Integer.to_string(time)

[mfa, count, time_percentage, time, time_per_call]
end

defp format_total(total_time, total_count) do
time_per_call = :erlang.float_to_binary(total_time / total_count, [{:decimals, 2}])
time_per_call = :erlang.float_to_binary(divide(total_time, total_count), [{:decimals, 2}])

["Total", Integer.to_string(total_count), "100.00", Integer.to_string(total_time), time_per_call]
end

defp divide(_, 0), do: 0.0
defp divide(t, n), do: t / n

defp column_lengths(header, rows) do
max_lengths = Enum.map(header, &String.length/1)

Expand Down

0 comments on commit 825fb73

Please sign in to comment.