EZProfiler.CodeProfiler (ezprofiler v1.3.0)

This module handles code profiling. The user hits c or c label or EZProfiler.Manager.enable_profiling/1 and any process whose code calls one of the profiling functions is profiled. Only a single process at a time can be profiled, although label transition can assist in that.

The module is loaded from the escript, replacing the one in the application, the reverse happens when the escript terminates.

The module in the release has functions like:

def start_profiling() do

end

So they are mostly no-ops, with no run-time cost.

There is a minimal run-time cost when the module is loaded, as much as a message to an Agent.

block-profiling

Block Profiling

Profile a block of code. If the function EZCodeProfiler.start_profiling is called, any code between that and EZCodeProfiler.stop_profiling is profiled.

function-profiling

Function Profiling

Profiles a specific function using EZProfiler.CodeProfiler.function_profiling.

pipe-profiling

Pipe Profiling

Profiles a function within an Elixir pipe using EZProfiler.CodeProfiler.pipe_profiling.

NOTE: The function we want to profile for function profiling and pipe function is specified as a function capture. It is recommended to include the module name (&MyModule.foo/1 not &foo/1)

labels

Labels

When using either the CLI c labels or EZProfiler.Manager.enable_profiling/1 either a single label or a list of labels can be specified. In the case of a list there are two modes of operation, label transition (labeltran) true or label transition false (the default). The behavior is as follows:

Label Transition false

This effectively a request to profile one-of those labels. The first matching label is selected for profiling and the rest of the labels are ignored.

Label Transition true

In this case all specified labels shall be profiled sequentially (order doesn't matter), effectively the profiler automatically re-enables profiling after a label match. A label that matches and is profiled, will removed from the list of labels to be profiled next and profiling is re-enabled for the remaining labels. This allows profiling to follow the flow of code through your application, even if processes are switched.

It is important to note that the rule of only one process at a time can be profiled still exists, so ideally profiled code calls should be sequential. However, if there are sections of code that need to be profiled that overlap in time ezprofiler performs pseudo profiling. This is where ezprofiler will calculate and display how long the profiled code took to execute.

Link to this section Summary

Functions

Profiles a specific function without any arguments or labels.

Profiles a specific function with arguments and control labels/functions.

Profiles a specific function with arguments and control labels/functions.

Profiles a function in a pipe without any extra arguments.

Profiles a specific function in a pipe with arguments and/or control labels/functions.

Profiles a specific function in a pipe with extra arguments and control labels/functions.

Starts profiling a block of code, must call EZProfiler.CodeProfiler.stop_code_profiling() to stop and present the results.

Starts profiling a block of code using a label (atom or string) or an anonymous function to target the results.

Stops profiling a block oof code started with start_code_profiling(..)

Link to this section Functions

Link to this function

function_profiling(fun)

Profiles a specific function without any arguments or labels.

options

Options

  • fun: The function (capture) to profile

example

Example

def foo() do
  x = function1()
  y = function2()
  EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/0)
end
Link to this function

function_profiling(fun, options)

Profiles a specific function with arguments and control labels/functions.

options

Options

  • fun: The function (capture) to profile
  • options: Can be either a list of arguments, a label (atom or string) or anonymous function returning a label

Starts profiling a function with arguments only:

example

Example

 def foo() do
   x = function1()
   y = function2()
   EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/1, [x])
 end

Starts profiling a function using label:

example-1

Example

 def foo() do
   x = function1()
   y = function2()
   EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/0, :my_label)
 end

Starts profiling with an anonymous function:

example-2

Example

def foo() do
  x = function1()
  y = function2()
  EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/0, fn -> if should_i_profile?(y), do: :my_label, else: :nok end)
end

Then in the ezprofiler console:

waiting..(1)> c
waiting..(2)>
Code profiling enabled

Or with a label:

 waiting..(4)> c :my_label, bob@foo.com
 waiting..(5)>
 Code profiling enabled with a label of :my_label, bob@foo.com

 waiting..(5)>
 Got a start profiling from source code with label of :my_label

Or using EZProfiler.Manager:

 # Will profile :my_label and/or "bob@foo.com", see main help on label transition
 EZProfiler.Manager.enable_profiling([:my_label, "bob@foo.com"])

NOTE: If anonymous function is used it must return a label to allow profiling or the atom :nok to not profile.

Link to this function

function_profiling(fun, args, options)

Profiles a specific function with arguments and control labels/functions.

options

Options

  • fun: The function (capture) to profile
  • args: The list of arguments to pass to the function
  • options: Can be either a label (atom or string) or anonymous function returning a label

Starts profiling a function using arguments and a label:

example

Example

def foo() do
  x = function1()
  y = function2()
  EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/1, [x], "Profile 52")
end

Starts profiling a function using arguments and an anonymous function:

example-1

Example

def foo() do
  x = function1()
  y = function2()
  EZProfiler.CodeProfiler.function_profiling(&MyModule.bar/1, [x], fn -> if should_i_profile?(y), do: "Profile 52", else: :nok end)
end

Then in the ezprofiler console:

 waiting..(4)> c "Profile 52"
 waiting..(5)>
 Code profiling enabled with a label of :my_label

 waiting..(5)>
 Got a start profiling from source code with label of "Profile 52"

Or using EZProfiler.Manager:

 EZProfiler.Manager.enable_profiling("Profile 52")

NOTE: If anonymous function is used it must return a label to allow profiling or the atom :nok to not profile.

Link to this function

pipe_profiling(arg, fun)

Profiles a function in a pipe without any extra arguments.

options

Options

  • arg: The argument passed in from the previous function/term in the sequence
  • fun: The function (capture) to profile

example

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1)
   |> function2()
end
Link to this function

pipe_profiling(arg, fun, options)

Profiles a specific function in a pipe with arguments and/or control labels/functions.

options

Options

  • arg: The argument passed in from the previous function/term in the sequence
  • fun: The function (capture) to profile
  • options: Can be either extra arguments, a label (atom or string) or anonymous function returning a label

Starts profiling a function in a pipe using extra arguments:

example

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1, [x])
   |> function2()
end

Starts profiling a function in a pipe using a label without extra arguments:

example-1

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1, :my_label)
   |> function2()
end

Starts profiling without extra arguments and an anonymous function:

example-2

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1, fn -> if should_i_profile?(x), do: :my_label, else: :nok end)
   |> function2()
end
Link to this function

pipe_profiling(arg, fun, args, options)

Profiles a specific function in a pipe with extra arguments and control labels/functions.

options

Options

  • arg: The argument passed in from the previous function/term in the sequence
  • fun: The function (capture) to profile
  • args: The list of extra arguments to pass to the function
  • options: Either a label (atom or string) or anonymous function returning a label

Starts profiling using arguments and a label:

example

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1, [x], :my_label)
   |> function2()
end

Starts profiling using arguments and an anonymous function:

example-1

Example

def foo(data) do
   x = function1()
   data
   |> bar()
   |> EZProfiler.CodeProfiler.pipe_profiling(&MyModule.baz/1, [x], fn -> if should_i_profile?(x), do: :my_label, else: :nok end)
   |> function2()
end

Then in the ezprofiler console:

waiting..(4)> c :my_label
waiting..(5)>
Code profiling enabled with a label of :my_label

waiting..(5)>
Got a start profiling from source code with label of :my_label

Or using EZProfiler.Manager:

EZProfiler.Manager.enable_profiling([:my_label, "bob@foo.com"])

NOTE: If anonymous function is used it must return a label to allow profiling or the atom :nok to not profile.

Link to this function

start_code_profiling()

Starts profiling a block of code, must call EZProfiler.CodeProfiler.stop_code_profiling() to stop and present the results.

example

Example

def foo() do
   x = function1()
   y = function2()
   EZProfiler.CodeProfiler.start_code_profiling()
   bar(x)
   baz(y)
   EZProfiler.CodeProfiler.stop_code_profiling()
end
Link to this function

start_code_profiling(options)

Starts profiling a block of code using a label (atom or string) or an anonymous function to target the results.

options

Options

  • options: Can be either a label (atom or string) or anonymous function returning a label

Starts profiling a function using label:

example

Example

def foo() do
  x = function1()
  y = function2()
  EZProfiler.CodeProfiler.start_code_profiling(:my_label)
  bar(x)
  baz(y)
  EZProfiler.CodeProfiler.stop_code_profiling()
end

Starts profiling a function using an anonymous function:

example-1

Example

EZProfiler.CodeProfiler.start_code_profiling(fn -> if should_i_profile?(foo), do: :my_label, else: :nok end)

case do_send_email(email, private_key) do
  :ok ->
    EZProfiler.CodeProfiler.stop_code_profiling()

Then in the ezprofiler console:

waiting..(1)> c
waiting..(2)>
Code profiling enabled

Or with a label:

waiting..(4)> c :my_label
waiting..(5)>
Code profiling enabled with a label of :my_label

waiting..(5)>
Got a start profiling from source code with label of :my_label

NOTE: If anonymous function is used it must return a label to allow profiling or the atom :nok to not profile.

Link to this function

stop_code_profiling()

Stops profiling a block oof code started with start_code_profiling(..)