perf-sentinel-core 0.7.8

Core library for perf-sentinel: polyglot performance anti-pattern detector
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Integration tests for the Redfish scraper.

use std::collections::HashMap;

use super::apply::{apply_chassis_scrape, build_chassis_services};
use super::config::RedfishSchema;
use super::parser::{ParseOutcome, parse_redfish_power};
use super::scraper::{ScraperError, scraper_error_reason};
use super::state::ServiceEnergy;

// --- attribution tests -----------------------------------------------

fn services(names: &[&str]) -> Vec<String> {
    names.iter().map(|s| (*s).to_string()).collect()
}

fn ops(entries: &[(&str, u64)]) -> HashMap<String, u64> {
    entries
        .iter()
        .map(|(svc, ops)| ((*svc).to_string(), *ops))
        .collect()
}

fn mappings(entries: &[(&str, &str)]) -> HashMap<String, String> {
    entries
        .iter()
        .map(|(svc, chassis)| ((*svc).to_string(), (*chassis).to_string()))
        .collect()
}

#[test]
fn single_chassis_single_service_publishes_coefficient() {
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_services = services(&["order-svc"]);
    let deltas = ops(&[("order-svc", 100)]);
    // 300 W × 60 s = 18000 J = 5e-3 kWh / 100 ops = 5e-5 kWh per op.
    let changed = apply_chassis_scrape(&mut next, &chassis_services, 300.0, 60.0, &deltas, 1000);
    assert!(changed);
    assert_eq!(next.len(), 1);
    assert!((next["order-svc"].energy_per_op_kwh - 5e-5).abs() < 1e-12);
}

#[test]
fn two_services_on_same_chassis_share_coefficient() {
    // Total ops 300 across two services → both get the same per-op value.
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_services = services(&["svc-a", "svc-b"]);
    let deltas = ops(&[("svc-a", 100), ("svc-b", 200)]);
    apply_chassis_scrape(&mut next, &chassis_services, 300.0, 60.0, &deltas, 1000);
    assert_eq!(next.len(), 2);
    // 18000 J / 3.6e6 = 5e-3 kWh, divided by 300 ops = 1.6666e-5.
    let expected = (300.0 * 60.0 / 3_600_000.0) / 300.0;
    assert!((next["svc-a"].energy_per_op_kwh - expected).abs() < 1e-15);
    assert!((next["svc-b"].energy_per_op_kwh - expected).abs() < 1e-15);
}

#[test]
fn service_on_other_chassis_unaffected() {
    // Only chassis-1's services are passed to apply_chassis_scrape, so
    // chassis-2's service stays out of `next`.
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_services = services(&["svc-a"]); // svc-b lives elsewhere
    let deltas = ops(&[("svc-a", 100), ("svc-b", 50)]);
    apply_chassis_scrape(&mut next, &chassis_services, 300.0, 60.0, &deltas, 1000);
    assert_eq!(next.len(), 1);
    assert!(next.contains_key("svc-a"));
    assert!(!next.contains_key("svc-b"));
}

#[test]
fn zero_ops_keeps_previous_entry() {
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    next.insert(
        "svc-a".to_string(),
        ServiceEnergy {
            energy_per_op_kwh: 7e-7,
            last_update_ms: 100,
        },
    );
    let chassis_services = services(&["svc-a"]);
    let deltas = ops(&[]);
    let changed = apply_chassis_scrape(&mut next, &chassis_services, 300.0, 60.0, &deltas, 200);
    assert!(!changed);
    assert!((next["svc-a"].energy_per_op_kwh - 7e-7).abs() < f64::EPSILON);
    assert_eq!(next["svc-a"].last_update_ms, 100);
}

#[test]
fn negative_watts_ignored() {
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    next.insert(
        "svc-a".to_string(),
        ServiceEnergy {
            energy_per_op_kwh: 7e-7,
            last_update_ms: 100,
        },
    );
    let chassis_services = services(&["svc-a"]);
    let deltas = ops(&[("svc-a", 100)]);
    let changed = apply_chassis_scrape(&mut next, &chassis_services, -1.0, 60.0, &deltas, 200);
    assert!(!changed);
    assert!((next["svc-a"].energy_per_op_kwh - 7e-7).abs() < f64::EPSILON);
}

#[test]
fn nan_watts_ignored() {
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_services = services(&["svc-a"]);
    let deltas = ops(&[("svc-a", 100)]);
    let changed = apply_chassis_scrape(&mut next, &chassis_services, f64::NAN, 60.0, &deltas, 200);
    assert!(!changed);
    assert!(next.is_empty());
}

#[test]
fn non_finite_scrape_interval_rejected() {
    // Defense in depth, config clamps to [15, 3600] but the function
    // is `pub` and could be called with non-finite f64.
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_services = services(&["svc-a"]);
    let deltas = ops(&[("svc-a", 100)]);
    assert!(!apply_chassis_scrape(
        &mut next,
        &chassis_services,
        300.0,
        f64::NAN,
        &deltas,
        200,
    ));
    assert!(!apply_chassis_scrape(
        &mut next,
        &chassis_services,
        300.0,
        f64::INFINITY,
        &deltas,
        200,
    ));
    assert!(next.is_empty());
}

#[test]
fn empty_chassis_services_is_noop() {
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let deltas = ops(&[("svc-a", 100)]);
    let changed = apply_chassis_scrape(&mut next, &[], 300.0, 60.0, &deltas, 200);
    assert!(!changed);
    assert!(next.is_empty());
}

#[test]
fn build_chassis_services_groups_by_chassis() {
    let m = mappings(&[
        ("svc-a", "chassis-1"),
        ("svc-b", "chassis-1"),
        ("svc-c", "chassis-2"),
    ]);
    let by_chassis = build_chassis_services(&m);
    assert_eq!(by_chassis.len(), 2);
    let mut chassis1 = by_chassis.get("chassis-1").cloned().unwrap();
    chassis1.sort();
    assert_eq!(chassis1, vec!["svc-a".to_string(), "svc-b".to_string()]);
    assert_eq!(
        by_chassis.get("chassis-2").unwrap(),
        &vec!["svc-c".to_string()]
    );
}

// --- scraper error mapping ------------------------------------------

#[test]
fn scraper_error_reason_maps_fetch_errors() {
    use crate::http_client::FetchError;
    use crate::report::metrics::RedfishScrapeReason;
    let utf8_err = ScraperError::Utf8(String::from_utf8(vec![0xff, 0xfe]).unwrap_err());
    assert_eq!(
        scraper_error_reason(&utf8_err),
        RedfishScrapeReason::InvalidUtf8
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::InvalidJson),
        RedfishScrapeReason::InvalidJson
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::PathMissing),
        RedfishScrapeReason::PathMissing
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::InvalidValue),
        RedfishScrapeReason::InvalidValue
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::Fetch(FetchError::Timeout)),
        RedfishScrapeReason::Timeout
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::Fetch(FetchError::HttpStatus(500))),
        RedfishScrapeReason::HttpError
    );
    assert_eq!(
        scraper_error_reason(&ScraperError::Fetch(FetchError::BodyRead("eof".into()))),
        RedfishScrapeReason::BodyReadError
    );
}

// --- parser tests (cross-vendor fixtures) ----------------------------

#[test]
fn parses_dell_idrac_response() {
    let body = r#"{
        "@odata.id": "/redfish/v1/Chassis/System.Embedded.1/Power",
        "Id": "Power",
        "Name": "Power",
        "PowerControl": [
            {
                "@odata.id": "/redfish/v1/Chassis/System.Embedded.1/Power#/PowerControl/0",
                "MemberId": "0",
                "Name": "System Power Control",
                "PowerConsumedWatts": 287.0,
                "PowerCapacityWatts": 750.0
            }
        ]
    }"#;
    assert_eq!(
        parse_redfish_power(body, RedfishSchema::LegacyPower),
        ParseOutcome::Ok(287.0)
    );
}

#[test]
fn parses_hpe_ilo_response() {
    // HPE iLO uses the same Redfish standard path for PowerConsumedWatts.
    let body = r#"{
        "@odata.id": "/redfish/v1/Chassis/1/Power/",
        "Id": "Power",
        "Name": "PowerMetrics",
        "PowerControl": [
            {
                "@odata.id": "/redfish/v1/Chassis/1/Power/#PowerControl/0",
                "MemberId": "0",
                "PowerConsumedWatts": 412.5,
                "PowerCapacityWatts": 1000
            }
        ],
        "Oem": {
            "Hpe": {
                "PowerRegulationEnabled": false
            }
        }
    }"#;
    assert_eq!(
        parse_redfish_power(body, RedfishSchema::LegacyPower),
        ParseOutcome::Ok(412.5)
    );
}

#[test]
fn parses_openbmc_reference_response() {
    let body = r#"{
        "@odata.id": "/redfish/v1/Chassis/chassis/Power",
        "Id": "Power",
        "Name": "Power",
        "PowerControl": [
            {
                "@odata.id": "/redfish/v1/Chassis/chassis/Power#/PowerControl/0",
                "MemberId": "0",
                "Name": "Chassis Power Control",
                "PowerConsumedWatts": 198.4
            }
        ]
    }"#;
    assert_eq!(
        parse_redfish_power(body, RedfishSchema::LegacyPower),
        ParseOutcome::Ok(198.4)
    );
}

#[test]
fn rejects_dell_response_in_transition_state() {
    // Some Dell iDRACs return null while the BMC reinitializes.
    let body = r#"{
        "PowerControl": [
            {
                "MemberId": "0",
                "PowerConsumedWatts": null
            }
        ]
    }"#;
    assert_eq!(
        parse_redfish_power(body, RedfishSchema::LegacyPower),
        ParseOutcome::InvalidValue
    );
}

#[test]
fn rejects_empty_power_control_array() {
    let body = r#"{"PowerControl": []}"#;
    assert_eq!(
        parse_redfish_power(body, RedfishSchema::LegacyPower),
        ParseOutcome::PathMissing
    );
}

// `custom_power_path_resolves_for_oem_vendors` removed in v0.7.6:
// arbitrary JSON pointers are no longer configurable. An OEM that
// exposes wattage under a non-standard path is expected to either
// surface a Redfish-compliant `/Power` or `/EnvironmentMetrics` on
// its own URL, or be fronted by a reverse proxy that reshapes the
// payload. See docs/LIMITATIONS.md for the rationale.

// --- multi-chassis attribution --------------------------------------

#[test]
fn multi_chassis_each_gets_independent_coefficient() {
    // Two chassis, each with its own service set and its own wattage.
    // The scraper hoists state.current_owned()/publish() out of the
    // loop, so the test mimics that by sharing a single `next` buffer
    // across two apply_chassis_scrape calls.
    let mut next: HashMap<String, ServiceEnergy> = HashMap::new();
    let chassis_1 = services(&["svc-a"]);
    let chassis_2 = services(&["svc-b"]);
    let deltas = ops(&[("svc-a", 100), ("svc-b", 200)]);
    assert!(apply_chassis_scrape(
        &mut next, &chassis_1, 360.0, 10.0, &deltas, 1000,
    ));
    assert!(apply_chassis_scrape(
        &mut next, &chassis_2, 720.0, 10.0, &deltas, 1000,
    ));
    assert_eq!(next.len(), 2);
    let a = (360.0 * 10.0 / 3_600_000.0) / 100.0;
    let b = (720.0 * 10.0 / 3_600_000.0) / 200.0;
    assert!((next["svc-a"].energy_per_op_kwh - a).abs() < 1e-15);
    assert!((next["svc-b"].energy_per_op_kwh - b).abs() < 1e-15);
}

// --- ca_bundle_path fail-loud regression ----------------------------

#[tokio::test]
async fn spawn_scraper_with_ca_bundle_path_aborts_immediately() {
    use super::config::{RedfishConfig, RedfishEndpoint};
    use super::scraper::spawn_scraper;
    use crate::report::metrics::MetricsState;
    use crate::score::redfish::RedfishState;
    use std::sync::Arc;
    use std::time::Duration;

    let mut endpoints = HashMap::new();
    endpoints.insert(
        "chassis-1".to_string(),
        RedfishEndpoint {
            url: "https://127.0.0.1:12345/redfish/v1/Chassis/1/Power".to_string(),
            schema: RedfishSchema::LegacyPower,
        },
    );
    let mut mappings = HashMap::new();
    mappings.insert("svc-a".to_string(), "chassis-1".to_string());
    let cfg = RedfishConfig {
        endpoints,
        scrape_interval: Duration::from_secs(15),
        service_mappings: mappings,
        ca_bundle_path: Some("/tmp/perf-sentinel-fake-ca-bundle.pem".to_string()),
        auth_header: None,
    };
    let state = RedfishState::new();
    let metrics = Arc::new(MetricsState::new());
    let handle = spawn_scraper(cfg, state, metrics);
    // The scraper must exit cleanly before the first scrape tick.
    tokio::time::timeout(Duration::from_millis(500), handle)
        .await
        .expect("scraper should exit fast on ca_bundle_path")
        .expect("scraper task should complete without panic");
}

#[tokio::test]
async fn spawn_scraper_staleness_gauge_climbs_when_every_chassis_fails() {
    // Regression guard: the gauge must climb from boot when every
    // chassis is unreachable. Before the fix, last_success_ms was
    // None at boot and the gauge stayed at 0.0 indefinitely.
    use super::config::{RedfishConfig, RedfishEndpoint};
    use super::scraper::spawn_scraper;
    use crate::report::metrics::MetricsState;
    use crate::score::redfish::RedfishState;
    use std::sync::Arc;
    use std::time::Duration;

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    drop(listener);

    let mut endpoints = HashMap::new();
    endpoints.insert(
        "chassis-1".to_string(),
        RedfishEndpoint {
            url: format!("http://{addr}/Power"),
            schema: RedfishSchema::LegacyPower,
        },
    );
    let mut mappings = HashMap::new();
    mappings.insert("svc-a".to_string(), "chassis-1".to_string());
    // Sub-second interval lets the test fail several ticks within a
    // reasonable window. Config-load validation would reject this
    // value (clamp is [15, 3600] s) but the typed struct is built
    // directly here, bypassing that gate.
    let cfg = RedfishConfig {
        endpoints,
        scrape_interval: Duration::from_millis(50),
        service_mappings: mappings,
        ca_bundle_path: None,
        auth_header: None,
    };
    let state = RedfishState::new();
    let metrics = Arc::new(MetricsState::new());
    let handle = spawn_scraper(cfg, state, metrics.clone());

    tokio::time::sleep(Duration::from_millis(300)).await;

    let age = metrics.redfish_last_scrape_age_seconds.get();
    handle.abort();
    assert!(
        age > 0.0,
        "staleness gauge should climb on never-succeeded scraper, got {age}"
    );
}