Skip to content

Commit e17b247

Browse files
costaraphaelJosé Valim
authored andcommitted
Fixed eprof to work with multiple processes (#7317)
1 parent 8a1c5c3 commit e17b247

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

lib/mix/lib/mix/tasks/profile.eprof.ex

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ defmodule Mix.Tasks.Profile.Eprof do
152152
def profile(fun, opts) do
153153
fun
154154
|> profile_and_analyse(opts)
155-
|> print_output
155+
|> print_output()
156156
end
157157

158158
defp profile_and_analyse(fun, opts) do
@@ -165,11 +165,15 @@ defmodule Mix.Tasks.Profile.Eprof do
165165
:eprof.profile([], fun, matching_pattern(opts))
166166

167167
results =
168-
:eprof.dump()
169-
|> extract_results
170-
|> filter_results(opts)
171-
|> sort_results(opts)
172-
|> add_totals
168+
Enum.map(:eprof.dump(), fn {pid, call_results} ->
169+
parsed_calls =
170+
call_results
171+
|> filter_results(opts)
172+
|> sort_results(opts)
173+
|> add_totals()
174+
175+
{pid, parsed_calls}
176+
end)
173177

174178
:eprof.stop()
175179

@@ -191,9 +195,6 @@ defmodule Mix.Tasks.Profile.Eprof do
191195
end
192196
end
193197

194-
defp extract_results([]), do: []
195-
defp extract_results([{_pid, call_results}]), do: call_results
196-
197198
defp filter_results(call_results, opts) do
198199
calls_opt = Keyword.get(opts, :calls, 0)
199200
time_opt = Keyword.get(opts, :time, 0)
@@ -227,14 +228,23 @@ defmodule Mix.Tasks.Profile.Eprof do
227228

228229
@header ["#", "CALLS", "%", "TIME", "µS/CALL"]
229230

230-
defp print_output({0, _, _, _}), do: print_function_count(0)
231+
defp print_output([]) do
232+
print_function_count(0)
233+
end
234+
235+
defp print_output(results) do
236+
Enum.each(results, &print_result/1)
237+
end
231238

232-
defp print_output({function_count, call_results, call_count, total_time}) do
239+
defp print_result({pid, {function_count, call_results, call_count, total_time}}) do
233240
formatted_rows = Enum.map(call_results, &format_row(&1, total_time))
234241
formatted_total = format_total(total_time, call_count)
235242

236243
column_lengths = column_lengths(@header, formatted_rows)
237244

245+
IO.puts("")
246+
247+
print_pid_row(pid)
238248
print_row(@header, column_lengths)
239249
print_row(formatted_total, column_lengths)
240250
Enum.each(formatted_rows, &print_row(&1, column_lengths))
@@ -244,6 +254,10 @@ defmodule Mix.Tasks.Profile.Eprof do
244254
print_function_count(function_count)
245255
end
246256

257+
defp print_pid_row(pid) do
258+
IO.puts("Profile results of #{inspect(pid)}")
259+
end
260+
247261
defp format_row({{module, function, arity}, {count, time}}, total_time) do
248262
mfa = Exception.format_mfa(module, function, arity)
249263
time_percentage = :erlang.float_to_binary(100 * divide(time, total_time), [{:decimals, 2}])

lib/mix/test/mix/tasks/profile.eprof_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ defmodule Mix.Tasks.Profile.EprofTest do
1818
end
1919
end
2020

21+
test "profiles evaluated expression in multiple processes", context do
22+
in_tmp context.test, fn ->
23+
assert capture_io(fn ->
24+
Eprof.run(["-e", "spawn(fn -> #{@expr} end)"])
25+
end) =~ ~r(String\.Chars\.Integer\.to_string\/1\s+\d)
26+
end
27+
end
28+
2129
test "profiles the script", context do
2230
in_tmp context.test, fn ->
2331
profile_script_name = "profile_script.ex"

0 commit comments

Comments
 (0)