[Proposal] Make function return values accessible in Mix.Tasks.Profile.#{Profiler}.profile

26 views
Skip to first unread message

Tobias Pfeiffer

unread,
Feb 20, 2022, 6:17:00 AM2/20/22
to elixir-lang-core
Hey everyone,

I'm finally working on a new Benchee release that will include profiling after benchmark runs. I did notice one problem, using the programmatic interface of the tasks (like: https://hexdocs.pm/mix/main/Mix.Tasks.Profile.Eprof.html#profile/2) it only prints the report but doesn't give me access to the return value of the function that was profiled.

That's a problem because Benchee needs the return value for after_each hooks (docs: https://github.com/bencheeorg/benchee#after_each)

The return value of the Profiler.profile functions seems to be undocumented, can we change it to be the return value of the function / {:ok, value} & {:error, reason} / some structure that includes the return value?

I checked and it seems that all of eprof/fprof/cprof return the value so it should be relatively easy to implement. They do return the value in slightly different ways though:

iex(1)> :eprof.start()
{:ok, #PID<0.109.0>}
iex(2)> {:ok, val} = :eprof.profile([], fn -> 42 end)
{:ok, 42}
iex(3)> val
42
iex(4)> :fprof.start()
{:ok, #PID<0.114.0>}
iex(5)> :fprof.apply(fn -> 43 end, [])
43

Which raises the question that if we did this, do we want to pass on the value as they return it or normalize it.

Input welcome, and as always thanks for all you do!

Tobi

José Valim

unread,
Feb 20, 2022, 8:54:00 AM2/20/22
to elixir-lang-core
Good call, I think it should return the result directly. A PR is welcome!

Tobias Pfeiffer

unread,
Feb 20, 2022, 3:31:06 PM2/20/22
to elixir-lang-core

In the spirit of me not promising OSS work and then not doing it (yes I still have mox back of my mind) I did it straight away, as it was also rather easy and I had looked at it today: https://github.com/elixir-lang/elixir/pull/11657

Thanks for the quick answer!
Reply all
Reply to author
Forward
0 new messages