nmbrs-workload 0.7.3

Uniform workload specification parsing and processing for nmbrs
Documentation
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Synthesis of host designations and of a plain phase's own columns
//! (SRD-118 ยง10.1).

use super::*;

fn wl(yaml: &str) -> Workload {
    crate::parse::parse_workload(yaml, &std::collections::HashMap::new()).expect("workload parses")
}

fn parse_error(key_metrics: &str) -> String {
    let yaml = format!(
        "phases:\n  load:\n    key_metrics:\n      c: {key_metrics}\n    ops: {{ t: {{ stmt: \"X\" }} }}\n"
    );
    crate::parse::parse_workload(&yaml, &std::collections::HashMap::new())
        .err()
        .unwrap_or_else(|| panic!("`{key_metrics}` parsed"))
}

const PHASES: &str = r#"
params: { adapter: testkit }
scenarios:
  default: [load, settle, idle]
phases:
  load:
    key_metrics:
      rows: last(result_success)
      cpu: avg(sysmon_cpu_util)
      node: avg(sysmon_cgroup_cpu_cores{cgroup="node"})
      count: max(result_success_p99)
    ops: { t: { stmt: "X" } }
  settle:
    key_metrics:
      cpu: avg(sysmon_cpu_util)
      rows: max(sysmon_ram_util, closing)
    ops: { t: { stmt: "Y" } }
  idle:
    ops: { t: { stmt: "Z" } }
"#;

fn spine() -> String {
    let v = synthesize(&wl(PHASES))
        .expect("well-formed")
        .expect("synthesized");
    crate::report::parse_report(&v).expect("synthesized section parses");
    v["phases"].as_str().unwrap().to_string()
}

/// A plain phase's designations are columns of the phases table, after
/// the contract's. Phases that designate one measure under one name
/// share its column.
#[test]
fn a_plain_phases_designations_are_columns_of_the_phases_table() {
    let spine = spine();
    assert!(
        spine.contains(
            r#"query: cpu: avg(avg_over_time(sysmon_cpu_util{phase=~"load|settle",phase_samples="overlapping"}[30d])) by (phase)"#
        ),
        "{spine}"
    );
    assert!(
        spine.contains(
            r#"query: rows: max(last_over_time(result_success{phase=~"load"}[30d])) by (phase)"#
        ),
        "{spine}"
    );
    // The contract's columns still cover every phase, designated or not.
    assert!(
        spine.contains(r#"result_failure{phase=~"idle|load|settle"}"#),
        "{spine}"
    );
}

/// A sampler family is read over the phase with `phase_samples`; its
/// matchers are in the selector, and its header says what it is.
#[test]
fn a_sampler_designation_carries_its_matchers_and_mode() {
    let spine = spine();
    assert!(
        spine.contains(
            r#"sysmon_cgroup_cpu_cores{phase=~"load",cgroup="node",phase_samples="overlapping"}"#
        ),
        "{spine}"
    );
    assert!(
        spine.contains(r#"header node: avg(sysmon_cgroup_cpu_cores{cgroup="node"})"#),
        "{spine}"
    );
    assert!(
        spine.contains(r#"sysmon_ram_util{phase=~"settle",phase_samples="closing"}"#),
        "{spine}"
    );
    assert!(
        spine.contains("header settle_rows: max(sysmon_ram_util, closing)"),
        "{spine}"
    );
}

/// A column name another measure already has, or the contract has, is
/// given its phase's name in front.
#[test]
fn a_taken_column_name_is_qualified_by_its_phase() {
    let spine = spine();
    assert!(spine.contains("query: settle_rows:"), "{spine}");
    assert!(spine.contains("query: load_count:"), "{spine}");
}

/// A sampler designation under an anchored sweep is read over each
/// iteration's activation, grouped by the sweep's label.
#[test]
fn a_sampler_designation_under_a_sweep_is_grouped_by_the_sweep() {
    let yaml = r#"
params: { adapter: testkit }
scenarios:
  default:
    - for: "k in 1,2"
      anchor: sweep
      phases: [tick]
phases:
  tick:
    key_metrics:
      cpu: max(sysmon_cpu_util, within)
    ops: { t: { stmt: "X" } }
"#;
    let v = synthesize(&wl(yaml)).unwrap().unwrap();
    let view = v["view_sweep"].as_str().unwrap();
    assert!(
        view.contains(
            r#"query: cpu: max(max_over_time(sysmon_cpu_util{phase="tick",phase_samples="within"}[30d])) by (k)"#
        ),
        "{view}"
    );
}

#[test]
fn a_mode_on_a_phases_own_family_and_a_rate_of_a_sampler_family_are_refused() {
    let err = parse_error("avg(result_success, within)");
    assert!(
        err.contains("a sample mode is for a family of the session sampler"),
        "{err}"
    );
    let err = parse_error("rate(sysmon_cpu_util)");
    assert!(
        err.contains("rate() is not for a family of the session sampler"),
        "{err}"
    );
    let err = parse_error("avg(sysmon_cpu_util, inside)");
    assert!(
        err.contains("within, overlapping, nearest, closing"),
        "{err}"
    );
}