Class StreamingPhysicsExpander

java.lang.Object
org.mockserver.llm.StreamingPhysicsExpander

public class StreamingPhysicsExpander extends Object
Applies streaming physics (timing delays) to a list of SSE events.

Algorithm (per spec section 2.3.2):

  • If physics is null or all fields are null, return the events unchanged.
  • Event 0 delay = timeToFirstToken (default 0 ms).
  • Events 1..n delay = baseDelay * (1 + uniform(-jitter, +jitter)) where baseDelay = 1000 / tokensPerSecond ms.
  • Uses java.util.Random seeded with physics.seed (default System.nanoTime()) for reproducible jitter.

The base delay is computed in floating point (1000.0 / tokensPerSecond) so fast streams above 1000 tokens/sec — whose per-token delay is below one millisecond — are not silently flattened to zero by integer division. Because the apply path (see Delay.applyDelay() and the SSE scheduler) only supports whole-millisecond granularity, the sub-millisecond remainder is carried forward across tokens so the cumulative stream timing stays accurate (e.g. 2000 tokens/sec yields a 1 ms delay on every other token, averaging 0.5 ms per token).

  • Method Details

    • applyPhysics

      public static List<SseEvent> applyPhysics(List<SseEvent> rawEvents, StreamingPhysics physics)
      Apply streaming physics delays to SSE events.
      Parameters:
      rawEvents - the raw events from the codec (not mutated)
      physics - streaming physics parameters (may be null)
      Returns:
      a new list of events with per-event delays set