fluxbench_logic/
synthetic.rs1use crate::context::{ContextError, MetricContext};
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone)]
10pub struct SyntheticDef {
11 pub id: &'static str,
13 pub formula: &'static str,
15 pub unit: Option<&'static str>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct SyntheticResult {
22 pub id: String,
24 pub value: f64,
26 pub formula: String,
28 pub unit: Option<String>,
30}
31
32pub fn compute_synthetics(
34 synthetics: &[SyntheticDef],
35 context: &MetricContext,
36) -> Vec<Result<SyntheticResult, ContextError>> {
37 synthetics
38 .iter()
39 .map(|s| {
40 context.evaluate(s.formula).map(|value| SyntheticResult {
41 id: s.id.to_string(),
42 value,
43 formula: s.formula.to_string(),
44 unit: s.unit.map(|u| u.to_string()),
45 })
46 })
47 .collect()
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_synthetic_computation() {
56 let mut ctx = MetricContext::new();
57 ctx.set("raw", 100.0);
58 ctx.set("overhead", 20.0);
59
60 let synthetics = vec![SyntheticDef {
61 id: "net_time",
62 formula: "raw - overhead",
63 unit: Some("ns"),
64 }];
65
66 let results = compute_synthetics(&synthetics, &ctx);
67 assert_eq!(results.len(), 1);
68
69 let result = results[0].as_ref().unwrap();
70 assert_eq!(result.id, "net_time");
71 assert!((result.value - 80.0).abs() < f64::EPSILON);
72 }
73}