Skip to content

Commit 0d6ae23

Browse files
committed
Add sort :per_call option to tprof (#13611)
1 parent cf84b7b commit 0d6ae23

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/mix/lib/mix/tasks/profile.tprof.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ defmodule Mix.Tasks.Profile.Tprof do
4545
* `--calls` - filters out any results with a call count lower than this
4646
* `--time` - filters out any results that took lower than specified (in µs), the `type` needs to be `time`
4747
* `--memory` - filters out any results that used less memory than specified (in words), the `type` needs to be `memory`
48-
* `--sort` - sorts the results by `calls` or by the value of `type` (default: the value of `type`)
48+
* `--sort` - sorts the results by `calls`, `per_call` or by the value of `type` (default: the value of `type`)
4949
* `--eval`, `-e` - evaluates the given code
5050
* `--require`, `-r` - requires pattern before running the command
5151
* `--parallel`, `-p` - makes all requires parallel
@@ -214,6 +214,7 @@ defmodule Mix.Tasks.Profile.Tprof do
214214
defp parse_opt({:sort, "time"}), do: {:sort, :time}
215215
defp parse_opt({:sort, "calls"}), do: {:sort, :calls}
216216
defp parse_opt({:sort, "memory"}), do: {:sort, :memory}
217+
defp parse_opt({:sort, "per_call"}), do: {:sort, :per_call}
217218
defp parse_opt({:sort, other}), do: Mix.raise("Invalid sort option: #{other}")
218219
defp parse_opt(other), do: other
219220

@@ -235,7 +236,7 @@ defmodule Mix.Tasks.Profile.Tprof do
235236
`type` needs to be `:time`
236237
* `:memory` - filters out any results that used less memory than specified (in words),
237238
`type` needs to be `:memory`
238-
* `:sort` - sort the results by `:calls` or by the value of `type`
239+
* `:sort` - sort the results by `:calls`, `:per_call` or by the value of `type`
239240
(default: the value of `type`)
240241
* `:warmup` - if the code should be warmed up before profiling (default: `true`)
241242
* `:set_on_spawn` - if newly spawned processes should be measured (default: `true`)
@@ -267,6 +268,9 @@ defmodule Mix.Tasks.Profile.Tprof do
267268
:calls ->
268269
:calls
269270

271+
:per_call ->
272+
:measurement_per_call
273+
270274
^type ->
271275
:measurement
272276

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,23 @@ defmodule Mix.Tasks.Profile.TprofTest do
7777
in_tmp(context.test, fn ->
7878
assert capture_io(fn ->
7979
Tprof.run(["--sort", "calls", "-e", @expr])
80-
end) =~ ~r/Enum\.each\/2.*String\.Chars\.Integer\.to_string\/1/s
80+
end) =~ ~r/\nEnum\.each\/2.*\nString\.Chars\.Integer\.to_string\/1/s
8181
end)
8282
end
8383

8484
test "sorts based on memory usage", context do
8585
in_tmp(context.test, fn ->
8686
assert capture_io(fn ->
8787
Tprof.run(["--type", "memory", "--sort", "calls", "-e", @expr])
88-
end) =~ ~r/Enum\.each\/2.*:erlang\.integer_to_binary\/1/s
88+
end) =~ ~r/\nEnum\.each\/2.*\n:erlang\.integer_to_binary\/1/s
89+
end)
90+
end
91+
92+
test "sorts based on memory per call", context do
93+
in_tmp(context.test, fn ->
94+
assert capture_io(fn ->
95+
Tprof.run(["--type", "memory", "--sort", "per_call", "-e", @expr])
96+
end) =~ ~r/\n:erlang\.integer_to_binary\/1.*\nEnum\.each\/2/s
8997
end)
9098
end
9199

0 commit comments

Comments
 (0)