// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
// @category: Probability
// modeling.gk — Service modeling patterns
//
// Modules for simulating realistic service behavior with the model
// adapter. These compose latency distributions with probability
// primitives for bimodal and conditional models.
// Single-mode service latency with gaussian profile.
service_latency(input: u64, mean_ms: f64, stddev_ms: f64) -> (latency_ms: f64) := {
q := unit_interval(hash(input))
latency_ms := clamp_f64(icd_normal(q, mean_ms, stddev_ms), 0.1, 999999.0)
}
// Bimodal service latency: fast path (cache hit) and slow path (miss).
// slow_pct controls the fraction on the slow path (0.0-1.0).
bimodal_latency(input: u64, fast_ms: f64, fast_std: f64, slow_ms: f64, slow_std: f64, slow_pct: f64) -> (latency_ms: f64) := {
is_slow := unfair_coin(input, slow_pct)
fast := clamp_f64(icd_normal(unit_interval(hash(input)), fast_ms, fast_std), 0.1, 999999.0)
slow := clamp_f64(icd_normal(unit_interval(hash(hash(input))), slow_ms, slow_std), 0.1, 999999.0)
latency_ms := select(is_slow, slow, fast)
}
// Full service model: bimodal latency + path-dependent errors.
service_model(input: u64, fast_ms: f64, slow_ms: f64, slow_pct: f64, error_pct: f64) -> (latency_ms: f64, is_error: u64) := {
is_slow := unfair_coin(input, slow_pct)
fast := clamp_f64(icd_normal(unit_interval(hash(input)), fast_ms, 0.5), 0.1, 999999.0)
slow := clamp_f64(icd_normal(unit_interval(hash(hash(input))), slow_ms, 5.0), 0.1, 999999.0)
latency_ms := select(is_slow, slow, fast)
fast_err := unfair_coin(hash(input), error_pct)
slow_err := unfair_coin(hash(hash(input)), error_pct)
is_error := select(is_slow, slow_err, fast_err)
}