Raxol.Core.Performance.JankDetector (Raxol v0.5.0)

View Source

Detects UI jank by analyzing frame timing.

A frame is considered janky if it takes longer than the jank threshold to render. The detector maintains a rolling window of frame times to identify patterns of jank.

Usage

# Create a new detector
detector = JankDetector.new(16, 60)  # 16ms threshold, 60 frame window

# Record a frame
detector = JankDetector.record_frame(detector, 20)

# Check for jank
if JankDetector.detect_jank?(detector) do
  Raxol.Core.Runtime.Log.warning_with_context("Jank detected", %{})
end

Summary

Functions

Checks if jank was detected in the last frame.

Gets the average frame time in the current window.

Gets the number of janky frames in the current window.

Gets the maximum frame time in the current window.

Creates a new jank detector.

Records a frame's timing.

Functions

detect_jank?(detector)

Checks if jank was detected in the last frame.

Parameters

  • detector - The jank detector

Returns

  • true if the last frame was janky
  • false otherwise

Examples

iex> detector = JankDetector.new(16, 60)
iex> detector = JankDetector.record_frame(detector, 20)
iex> JankDetector.detect_jank?(detector)
true

get_avg_frame_time(detector)

Gets the average frame time in the current window.

Parameters

  • detector - The jank detector

Returns

Average frame time in milliseconds.

Examples

iex> detector = JankDetector.new(16, 60)
iex> detector = JankDetector.record_frame(detector, 20)
iex> JankDetector.get_avg_frame_time(detector)
20.0

get_jank_count(detector)

Gets the number of janky frames in the current window.

Parameters

  • detector - The jank detector

Returns

Number of janky frames.

Examples

iex> detector = JankDetector.new(16, 60)
iex> detector = JankDetector.record_frame(detector, 20)
iex> JankDetector.get_jank_count(detector)
1

get_max_frame_time(detector)

Gets the maximum frame time in the current window.

Parameters

  • detector - The jank detector

Returns

Maximum frame time in milliseconds.

Examples

iex> detector = JankDetector.new(16, 60)
iex> detector = JankDetector.record_frame(detector, 20)
iex> JankDetector.get_max_frame_time(detector)
20

new(threshold, window_size)

Creates a new jank detector.

Parameters

  • threshold - Time in milliseconds above which a frame is considered janky
  • window_size - Number of frames to keep in the rolling window

Returns

A new jank detector struct.

Examples

iex> JankDetector.new(16, 60)
%JankDetector{
  threshold: 16,
  window_size: 60,
  frame_times: [],
  jank_count: 0
}

record_frame(detector, frame_time)

Records a frame's timing.

Parameters

  • detector - The jank detector
  • frame_time - Time taken to render the frame in milliseconds

Returns

Updated jank detector.

Examples

iex> detector = JankDetector.new(16, 60)
iex> detector = JankDetector.record_frame(detector, 20)
iex> detector.jank_count
1