|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate whether an aiperf agentic replay produced benchmarkable results.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import math |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +from typing import Any |
| 12 | + |
| 13 | + |
| 14 | +def _resolve_aggregate_path(artifact_dir: Path) -> Path: |
| 15 | + """Find aiperf's aggregate JSON in the direct or per-run artifact layout.""" |
| 16 | + direct = artifact_dir / "profile_export_aiperf.json" |
| 17 | + if direct.is_file(): |
| 18 | + return direct |
| 19 | + |
| 20 | + if artifact_dir.is_dir(): |
| 21 | + for child in sorted(artifact_dir.iterdir()): |
| 22 | + candidate = child / "profile_export_aiperf.json" |
| 23 | + if child.is_dir() and candidate.is_file(): |
| 24 | + return candidate |
| 25 | + |
| 26 | + return direct |
| 27 | + |
| 28 | + |
| 29 | +def _metric_avg(aggregate: dict[str, Any], name: str) -> float | None: |
| 30 | + """Read an aggregate metric's numeric average, if present.""" |
| 31 | + metric = aggregate.get(name) |
| 32 | + if metric is None: |
| 33 | + return None |
| 34 | + if not isinstance(metric, dict): |
| 35 | + raise ValueError(f"{name} must be an object") |
| 36 | + |
| 37 | + value = metric.get("avg") |
| 38 | + if value is None: |
| 39 | + return None |
| 40 | + if not isinstance(value, int | float) or isinstance(value, bool): |
| 41 | + raise ValueError(f"{name}.avg must be numeric") |
| 42 | + |
| 43 | + value = float(value) |
| 44 | + if not math.isfinite(value) or value < 0: |
| 45 | + raise ValueError(f"{name}.avg must be a finite non-negative number") |
| 46 | + return value |
| 47 | + |
| 48 | + |
| 49 | +def validate_result(artifact_dir: Path, failed_request_threshold: float) -> list[str]: |
| 50 | + """Return validation errors for an aiperf artifact directory.""" |
| 51 | + aggregate_path = _resolve_aggregate_path(artifact_dir) |
| 52 | + if not aggregate_path.is_file(): |
| 53 | + return [f"{aggregate_path} not found"] |
| 54 | + |
| 55 | + try: |
| 56 | + with open(aggregate_path) as f: |
| 57 | + aggregate = json.load(f) |
| 58 | + if not isinstance(aggregate, dict): |
| 59 | + return [f"{aggregate_path} must contain a JSON object"] |
| 60 | + |
| 61 | + successes = _metric_avg(aggregate, "request_count") |
| 62 | + errors = _metric_avg(aggregate, "error_request_count") or 0.0 |
| 63 | + completed = _metric_avg(aggregate, "completed_request_count") |
| 64 | + except (OSError, json.JSONDecodeError, ValueError) as exc: |
| 65 | + return [f"failed to read {aggregate_path}: {exc}"] |
| 66 | + |
| 67 | + if successes is None: |
| 68 | + return ["request_count.avg is missing"] |
| 69 | + if completed is None: |
| 70 | + completed = successes + errors |
| 71 | + if completed <= 0: |
| 72 | + return ["aiperf completed zero requests"] |
| 73 | + |
| 74 | + error_rate = errors / completed |
| 75 | + if error_rate > failed_request_threshold: |
| 76 | + return [ |
| 77 | + "aiperf request error rate exceeded the benchmark limit: " |
| 78 | + f"{errors:g}/{completed:g} = {error_rate:.3%} > " |
| 79 | + f"{failed_request_threshold:.3%}" |
| 80 | + ] |
| 81 | + |
| 82 | + print( |
| 83 | + "Validated aiperf request error rate: " |
| 84 | + f"{errors:g}/{completed:g} = {error_rate:.3%} <= " |
| 85 | + f"{failed_request_threshold:.3%}" |
| 86 | + ) |
| 87 | + return [] |
| 88 | + |
| 89 | + |
| 90 | +def main() -> int: |
| 91 | + parser = argparse.ArgumentParser() |
| 92 | + parser.add_argument("artifact_dir", type=Path) |
| 93 | + parser.add_argument( |
| 94 | + "--failed-request-threshold", |
| 95 | + type=float, |
| 96 | + required=True, |
| 97 | + help="Maximum accepted error fraction, inclusive", |
| 98 | + ) |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + if not 0 <= args.failed_request_threshold <= 1: |
| 102 | + parser.error("--failed-request-threshold must be between 0 and 1") |
| 103 | + |
| 104 | + errors = validate_result(args.artifact_dir, args.failed_request_threshold) |
| 105 | + for error in errors: |
| 106 | + print(f"ERROR: {error}", file=sys.stderr) |
| 107 | + return 1 if errors else 0 |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + sys.exit(main()) |
0 commit comments