# misc/frameprobe (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/frameprobe/

Detects slow devices. (FrameProbe of vanilla.js misc/frameprobe.js)
- Measures the frame intervals of the first few seconds, used to automatically turn on a light path that disables heavy effects.
- After the wait seconds from the start of measuring have passed, looks at each frame's interval during the measure seconds. (the first few seconds stutter while models and images load)
- If the share of frames exceeding the slow factor times the target interval (one tick of the maximum frame rate, or one tick of 60 frames if unlimited) is at least the slow share, it is a slow device.
  - Why 1.4 times instead of one tick: a 90 Hz screen at a maximum of 60 has the engine drawing every other frame, always 22 ms apart, and that is not the device being slow.
  - On 60 Hz and 120 Hz screens, frames that missed one refresh (33 ms, 25 ms) are counted as slow. One in four means below 48 fps.
- Once decided as a slow device, it does not measure again. If the window is hidden before measuring is done, restart measures again from the beginning.
  If not slow, measure_again can measure once more in a heavy scene.
- Does not import the engine. The caller passes the frame interval and the maximum frame rate.

## FrameProbe (struct) (Default)

Detects slow devices. (decides whether to turn off heavy effects from the frame intervals of the first few seconds)
- Initial wait 2 seconds, measure 4 seconds, slow factor 1.4, slow share 0.25, target 60 frames when unlimited.

Example: A device that stutters once every three frames is decided as slow

```rust
use vanilla::FrameProbe;

let mut frame_probe = FrameProbe::new();
let mut frame_index = 0;
let mut is_done = false;
while !is_done {
	let frame_seconds = if frame_index % 3 == 0 {
		1.0 / 20.0
	}
	else {
		1.0 / 60.0
	};
	is_done = frame_probe.measure_frame(frame_seconds, 60.0);
	frame_index += 1;
}
assert!(frame_probe.is_finished());
assert!(frame_probe.is_slow());
assert!(frame_probe.read_slow_frame_share() >= 0.25);
```

Example: Turn on the light path once in the scene on the frame measuring finishes

```rust
use vanilla::{Engine, FrameProbe};

fn tick_frame_probe(frame_probe: &mut FrameProbe, engine: &Engine, is_light_mode: &mut bool) {
	let frame_seconds = engine.get_time_manager().get_unscale_delta_time();
	let maximum_frame_per_second = engine.get_maximum_frame_per_second();
	if frame_probe.measure_frame(frame_seconds, maximum_frame_per_second) && frame_probe.is_slow() {
		*is_light_mode = true;
	}
}
```

- `pub fn new() -> FrameProbe` (constructor, since 0.4.0): Constructor.
  - returns FrameProbe: A new probe that has not measured yet. (wait 2 seconds, measure 4 seconds, slow factor 1.4, slow share 0.25, unlimited target 60)
- `pub fn measure_frame(&mut self, frame_seconds: f64, maximum_frame_per_second: f64) -> bool` (method, since 0.4.0): Measures one frame. (returns true on the frame measuring finishes)
  - Frames whose elapsed time is within the wait seconds are not counted. The slow interval is slow factor / target frame rate (seconds), and frames longer than that are counted as slow.
  - `frame_seconds` f64: Time elapsed since the last frame. (seconds, ignored if 0 or less or NaN)
  - `maximum_frame_per_second` f64: The engine's maximum frame rate. (0 or less is treated as unlimited and the target from set_unlimited_target_frame_per_second is used)
  - returns bool: true if measuring finished with this frame. false if not yet or already finished.

  Example: A steady 60 frames is not slow

  ```rust
  use vanilla::FrameProbe;
  
  let mut frame_probe = FrameProbe::new();
  frame_probe.set_seconds(0.5, 1.0);
  let mut frame_count = 0;
  while !frame_probe.measure_frame(1.0 / 60.0, 60.0) {
  	frame_count += 1;
  }
  assert!(frame_count >= 89);
  assert!(!frame_probe.is_slow());
  assert!(!frame_probe.measure_frame(1.0 / 60.0, 60.0));
  ```

- `pub fn restart(&mut self)` (method, since 0.4.0): Measures again from the start. (when the window was hidden before measuring finished, left as is if already finished)
- `pub fn measure_again(&mut self)` (method, since 0.4.0): Reopens a finished probe for one more measurement. (only if not slow, from the wait seconds as at the start)
- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): Returns whether measuring is finished.
  - returns bool: true if measuring has finished. (false after reopening with measure_again)
- `pub fn is_slow(&self) -> bool` (method, since 0.4.0): Returns whether it is a slow device. (false before measuring finishes)
  - returns bool: true if the measured share of slow frames was at least the slow share.
- `pub fn read_slow_frame_share(&self) -> f64` (method, since 0.4.0): Returns the slow frame fraction so far. (0 if no frames were measured)
  - returns f64: Slow frame count / measured frame count. (0 ~ 1)
- `pub fn set_seconds(&mut self, warmup_seconds: f64, probe_seconds: f64)` (method, since 0.4.0): Sets the wait seconds and measure seconds. (set before measuring)
  - `warmup_seconds` f64: Time to wait before measuring. (seconds, 0 if less than 0, initially 2)
  - `probe_seconds` f64: Time to measure after waiting. (seconds, 0 if less than 0, initially 4)
- `pub fn get_warmup_seconds(&self) -> f64` (method, since 0.4.0): Returns the wait seconds.
  - returns f64: Time to wait before measuring. (seconds)
- `pub fn get_probe_seconds(&self) -> f64` (method, since 0.4.0): Returns the measure seconds.
  - returns f64: Time to measure after waiting. (seconds)
- `pub fn set_slow_frame_threshold(&mut self, slow_frame_ratio: f64, slow_frame_share: f64)` (method, since 0.4.0): Sets the slow frame criteria. (multiple of the target interval, fraction to count as a slow device)
  - `slow_frame_ratio` f64: How many times the target interval a frame must exceed to count as slow. (initially 1.4)
  - `slow_frame_share` f64: If slow frames are at least this share of measured frames, it is a slow device. (0 ~ 1, initially 0.25)
- `pub fn get_slow_frame_ratio(&self) -> f64` (method, since 0.4.0): Returns the interval multiple counted as a slow frame.
  - returns f64: Factor multiplied by the target interval. (initially 1.4)
- `pub fn get_slow_frame_share(&self) -> f64` (method, since 0.4.0): Returns the fraction of slow frames to count as a slow device.
  - returns f64: Slow share. (0 ~ 1, initially 0.25)
- `pub fn set_unlimited_target_frame_per_second(&mut self, unlimited_target_frame_per_second: f64)` (method, since 0.4.0): Sets the target when the max frame rate is unlimited.
  - `unlimited_target_frame_per_second` f64: Target frame rate used when measure_frame gets a maximum frame rate of 0 or less. (frames per second, initially 60)
- `pub fn get_unlimited_target_frame_per_second(&self) -> f64` (method, since 0.4.0): Returns the target when the max frame rate is unlimited.
  - returns f64: Target frame rate. (frames per second)
