Untangle.Time (Bonfire v0.9.12-social-beta.68)

View Source

Provides timing utilities for measuring function execution time.

This module offers decorators for measuring and logging the execution time of functions:

  • time(): Basic measurement of single function calls
  • time_process(): Tracks count and total time across multiple calls
  • time_tree(): Captures timing across process trees

Summary

Functions

Decorator that measures and logs execution time of functions.

Decorator that aggregates execution counts and times across multiple calls.

Decorator that measures execution time across a process tree.

Functions

disabled?()

time()

(macro)

time(var1)

(macro)

time(slow_ms \\ nil, fn_body, context)

Decorator that measures and logs execution time of functions.

When applied to a function, this decorator will:

  • Measure the function's execution time if the logger level is set to :debug
  • Log the execution time if it exceeds a configured threshold (default: 10,000 microseconds)
  • Return the function's result unchanged

Configuration

The minimum time threshold for logging can be configured in your application:

config :untangle, :time_slow_min, 10_000  # microseconds

Examples

defmodule Demo do
  use Untangle.Time

  @decorate time()
  def slow_function(ms) do
    Process.sleep(ms)
    :ok
  end
end

time_process()

(macro)

time_process(var1)

(macro)

time_process(slow_ms \\ nil, fn_body, context)

Decorator that aggregates execution counts and times across multiple calls.

When applied to a function, this decorator will:

  • Track each call in the process dictionary
  • Count the number of executions
  • Sum total execution time
  • Log detailed statistics when execution time exceeds threshold
  • Return the function's result unchanged

Examples

defmodule Demo do
  use Untangle.Time

  @decorate time_process()
  def repeated_function(ms) do
    Process.sleep(ms)
    :ok
  end
end

After multiple calls, logs will include call count and total time.

time_tree()

(macro)

time_tree(var1)

(macro)

time_tree(slow_ms \\ nil, fn_body, context)

Decorator that measures execution time across a process tree.

When applied to a function, this decorator will:

  • Track execution time of the function in the current process and all processes it spawns
  • Log detailed timing statistics when execution completes
  • Return the function's result unchanged

Requires the ProcessTree library to be added to your app's dependencies.

Examples

defmodule Demo do
  use Untangle.Time

  @decorate time_tree()
  def spawn_function(depth \ 1) do
    Process.sleep(50) 
    if depth < 3 do
      Task.async(fn -> 
        Demo.spawn_function(depth + 1)
      end) 
      |> Task.await()
    end
    :ok
  end
end