|
| 1 | +"""Benchmark scoring: compute precision and recall against expected findings.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +from dataclasses import dataclass |
| 6 | +from pathlib import Path |
| 7 | +from typing import List |
| 8 | + |
| 9 | +from .fixtures import BenchmarkScenario, ExpectedFinding |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class ScenarioResult: |
| 14 | + scenario_name: str |
| 15 | + true_positives: int |
| 16 | + false_positives: int |
| 17 | + false_negatives: int |
| 18 | + precision: float |
| 19 | + recall: float |
| 20 | + f1: float |
| 21 | + |
| 22 | + |
| 23 | +def score_scenario( |
| 24 | + scenario: BenchmarkScenario, |
| 25 | + actual_findings: List[dict], |
| 26 | +) -> ScenarioResult: |
| 27 | + matched_expected: set[int] = set() |
| 28 | + matched_actual: set[int] = set() |
| 29 | + |
| 30 | + for ei, expected in enumerate(scenario.expected_findings): |
| 31 | + for ai, actual in enumerate(actual_findings): |
| 32 | + if ai in matched_actual: |
| 33 | + continue |
| 34 | + if _matches(expected, actual): |
| 35 | + matched_expected.add(ei) |
| 36 | + matched_actual.add(ai) |
| 37 | + break |
| 38 | + |
| 39 | + tp = len(matched_expected) |
| 40 | + fp = len(actual_findings) - len(matched_actual) |
| 41 | + fn = len(scenario.expected_findings) - tp |
| 42 | + |
| 43 | + precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0 |
| 44 | + recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0 |
| 45 | + f1 = (2 * precision * recall / (precision + recall)) if (precision + recall) > 0 else 0.0 |
| 46 | + |
| 47 | + return ScenarioResult( |
| 48 | + scenario_name=scenario.name, |
| 49 | + true_positives=tp, |
| 50 | + false_positives=fp, |
| 51 | + false_negatives=fn, |
| 52 | + precision=round(precision, 4), |
| 53 | + recall=round(recall, 4), |
| 54 | + f1=round(f1, 4), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _matches(expected: ExpectedFinding, actual: dict) -> bool: |
| 59 | + vuln_class = actual.get("vulnerability_class", "") |
| 60 | + endpoint = actual.get("endpoint", "") |
| 61 | + confidence = actual.get("confidence", 0.0) |
| 62 | + |
| 63 | + if expected.vulnerability_class.lower() not in vuln_class.lower(): |
| 64 | + return False |
| 65 | + if expected.endpoint not in endpoint: |
| 66 | + return False |
| 67 | + if confidence < expected.min_confidence: |
| 68 | + return False |
| 69 | + return True |
| 70 | + |
| 71 | + |
| 72 | +def write_metrics(results: List[ScenarioResult], output_path: str = "benchmarks/metrics.json"): |
| 73 | + data = { |
| 74 | + "scenarios": [ |
| 75 | + { |
| 76 | + "name": r.scenario_name, |
| 77 | + "true_positives": r.true_positives, |
| 78 | + "false_positives": r.false_positives, |
| 79 | + "false_negatives": r.false_negatives, |
| 80 | + "precision": r.precision, |
| 81 | + "recall": r.recall, |
| 82 | + "f1": r.f1, |
| 83 | + } |
| 84 | + for r in results |
| 85 | + ], |
| 86 | + "aggregate": { |
| 87 | + "avg_precision": round( |
| 88 | + sum(r.precision for r in results) / len(results), 4 |
| 89 | + ) |
| 90 | + if results |
| 91 | + else 0.0, |
| 92 | + "avg_recall": round( |
| 93 | + sum(r.recall for r in results) / len(results), 4 |
| 94 | + ) |
| 95 | + if results |
| 96 | + else 0.0, |
| 97 | + }, |
| 98 | + } |
| 99 | + Path(output_path).write_text(json.dumps(data, indent=2), encoding="utf-8") |
| 100 | + return data |
0 commit comments