dream_test/timing

Human-readable duration formatting for test timing.

Provides utilities for measuring and displaying test execution times in a human-friendly format that scales appropriately.

Duration Scaling

DurationDisplay Format
< 1ms0.42ms
1ms - 999ms42ms
1s - 59s1.2s
1m - 59m2m 30s
>= 1h1h 15m

Example

timing.format_duration_ms(1500)
|> should
|> be_equal("1.5s")
|> or_fail_with("expected 1.5s")

Values

pub fn format_duration_ms(duration_ms duration_ms: Int) -> String

Format a duration in milliseconds as a human-readable string.

Automatically scales to the most appropriate unit:

  • Milliseconds for durations under 1 second
  • Seconds (with decimal) for durations under 1 minute
  • Minutes and seconds for durations under 1 hour
  • Hours and minutes for longer durations

Example

// Arrange & Act
let ms = timing.format_duration_ms(42)

// Assert
ms
|> should
|> be_equal("42ms")
|> or_fail_with("expected 42ms")

Parameters

  • duration_ms: Duration in milliseconds

Returns

A human-readable duration string (for example "42ms" or "1.5s").

pub fn format_duration_us(duration_us duration_us: Int) -> String

Format a duration in microseconds as a human-readable string.

Similar to format_duration_ms but accepts microseconds. Useful when working with high-precision timing.

Examples

timing.format_duration_us(500)
|> should
|> be_equal("0.5ms")
|> or_fail_with("expected 0.5ms")

Parameters

  • duration_us: Duration in microseconds

Returns

A human-readable duration string (for example "0.5ms" or "42ms").

pub fn now_ms() -> Int

Get the current monotonic time in milliseconds.

Use this to measure elapsed time between two points. Monotonic time is not affected by system clock changes.

Example

let t1 = timing.now_ms()
let t2 = timing.now_ms()
let ok = t2 >= t1

ok
|> should
|> be_equal(True)
|> or_fail_with("expected now_ms to be monotonic")

Returns

A monotonic timestamp in milliseconds.

pub fn now_us() -> Int

Get the current monotonic time in microseconds.

Higher precision version of now_ms() for sub-millisecond timing.

Example

let t1 = timing.now_us()
let t2 = timing.now_us()
let ok = t2 >= t1

ok
|> should
|> be_equal(True)
|> or_fail_with("expected now_us to be monotonic")

Returns

A monotonic timestamp in microseconds.

Search Document