Skip to content

Commit b5704cc

Browse files
committed
Add MIX_PROFILE
1 parent 8923da0 commit b5704cc

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

lib/mix/lib/mix.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ defmodule Mix do
356356
357357
* `MIX_PATH` - appends extra code paths
358358
359+
* `MIX_PROFILE` - a list of comma-separated Mix tasks to profile the time spent on
360+
functions by the process running the task
361+
359362
* `MIX_QUIET` - does not print information messages to the terminal
360363
361364
* `MIX_REBAR3` - path to rebar3 command that overrides the one Mix installs

lib/mix/lib/mix/cli.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ defmodule Mix.CLI do
1010
if env_variable_activated?("MIX_QUIET"), do: Mix.shell(Mix.Shell.Quiet)
1111
if env_variable_activated?("MIX_DEBUG"), do: Mix.debug(true)
1212

13+
if profile = System.get_env("MIX_PROFILE", "") do
14+
Mix.State.put(:profile, String.split(profile, ","))
15+
end
16+
1317
case check_for_shortcuts(args) do
1418
:help ->
1519
Mix.shell().info("Mix is a build tool for Elixir")

lib/mix/lib/mix/task.ex

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,21 @@ defmodule Mix.Task do
492492
end
493493

494494
defp with_debug(task, args, proj, fun) do
495-
if Mix.debug?() do
496-
shell = Mix.shell()
497-
shell.info(["-> Running mix ", task_to_string(task, args), project_to_string(proj)])
498-
{time, res} = :timer.tc(fun)
499-
shell.info(["<- Ran mix ", task, " in ", Integer.to_string(div(time, 1000)), "ms"])
500-
res
501-
else
502-
fun.()
495+
cond do
496+
Mix.debug?() ->
497+
shell = Mix.shell()
498+
shell.info(["-> Running mix ", task_to_string(task, args), project_to_string(proj)])
499+
{time, res} = :timer.tc(fun)
500+
shell.info(["<- Ran mix ", task, " in ", Integer.to_string(div(time, 1000)), "ms"])
501+
res
502+
503+
task in Mix.State.get(:profile, []) ->
504+
shell = Mix.shell()
505+
shell.info(["-> Profiling mix ", task_to_string(task, args), project_to_string(proj)])
506+
Mix.Tasks.Profile.Eprof.profile(fun, warmup: false, set_on_spawn: false)
507+
508+
true ->
509+
fun.()
503510
end
504511
end
505512

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ defmodule Mix.Tasks.Profile.Eprof do
181181
* `:calls` - filters out any results with a call count lower than this
182182
* `:time` - filters out any results that took lower than specified (in µs)
183183
* `:sort` - sort the results by `:time` or `:calls` (default: `:time`)
184+
* `:warmup` - if the code should be warmed up before profiling (default: `true`)
185+
* `:set_on_spawn` - if newly spawned processes should be measured (default: `true`)
184186
185187
"""
186188
@spec profile((-> result), keyword()) :: result when result: any()
@@ -198,7 +200,8 @@ defmodule Mix.Tasks.Profile.Eprof do
198200
end
199201

200202
:eprof.start()
201-
{:ok, return_value} = :eprof.profile([], fun, Keyword.get(opts, :matching, {:_, :_, :_}))
203+
matching = Keyword.get(opts, :matching, {:_, :_, :_})
204+
{:ok, return_value} = :eprof.profile([], fun, matching, Keyword.take(opts, [:set_on_spawn]))
202205

203206
results =
204207
Enum.map(:eprof.dump(), fn {pid, call_results} ->

0 commit comments

Comments
 (0)