20261112 完成#187真实序列录制回放与七指标评估

This commit is contained in:
2026-11-12 15:14:06 +08:00
parent 05650b23bf
commit dcf3e5b6ae
49 changed files with 6943 additions and 111 deletions

View File

@@ -0,0 +1,106 @@
from pathlib import Path
import tempfile
import unittest
from common import atomic_json
from formal_metrics import collect, hit_ratios, interval_union
class FormalMetricsTests(unittest.TestCase):
def test_parallel_sync_union_and_reversed_bounds(self):
self.assertEqual(interval_union([(20, 40), (0, 10), (5, 25), (50, 55)]), 45)
self.assertEqual(interval_union([]), 0)
with self.assertRaises(ValueError):
interval_union([(5, 4)])
def test_hybrid_denominators_keep_layers_separate_and_do_not_double_count_context(self):
stage = {"analysis_counts": {"child_certificate_cache_hit": 7, "child_certificate_cache_lookup": 10,
"publication_point_cache_reuse_hits": 9, "publication_point_cache_lookup_total": 20},
"roa_validation_cache": {"hit_roas": 50, "miss_roas": 20, "blocked_roas": 30, "context_blocked_roas": 15}}
ratios = hit_ratios(stage, "hybrid")
self.assertEqual(ratios["roa"]["ratio"], 0.5)
self.assertEqual(ratios["child_certificate"]["ratio"], 0.7)
self.assertEqual(ratios["publication_point"]["ratio"], 0.45)
self.assertIsNone(ratios["signature"]["ratio"])
stage["analysis_counts"]["publication_point_cache_reuse_hits"] = 21
with self.assertRaises(ValueError):
hit_ratios(stage, "hybrid")
def test_seven_metrics_fail_closed_on_clock_coverage_and_background_boundary(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
run, benchmark = root / "run", root / "t2"
run.mkdir(); benchmark.mkdir()
direct = {"schema_version": 1, "clock": "Linux CLOCK_MONOTONIC nanoseconds",
"rp_start_ns": 20, "payload_ready_ns": 100, "sync_wall_union_ns": 40,
"sync_intervals": [{"protocol": "rrdp", "uri": "good", "start_ns": 30, "end_ns": 60, "success": True},
{"protocol": "rsync", "uri": "rsync://failed/repo/pp/", "start_ns": 50, "end_ns": 70, "success": False}],
"rrdp_modes": {"good": "delta"}, "sync_boundary": "native union", "payload_boundary": "atomic CCR"}
atomic_json(run / "evaluation.json", direct)
atomic_json(run / "contract.json", {"rsyncScopePolicy": "module-root"})
stage = {"analysis_counts": {}, "roa_validation_cache": {"hit_roas": 0, "miss_roas": 0, "blocked_roas": 0}}
atomic_json(run / "stage-timing.json", stage)
engine = {"boundary": "validation with background write drain", "wall_ns": 15, "user_cpu_secs": 0.1, "system_cpu_secs": 0.1}
atomic_json(benchmark / "engine-timing.json", engine)
(run / "process-time.txt").write_text("Maximum resident set size (kbytes): 1234\n")
process = {"controller_trigger_monotonic_ns": 10, "cpu_ns": 500, "memory_peak_bytes": 9999}
table = {"rrdp": {"good": "Success"}, "rsync": {"rsync://failed/repo/": {"Failure": "captured gap"}}}
result = collect(run, benchmark, process, {"required_serialized_bytes": 0}, "baseline", table)
self.assertEqual(result["update_to_payload_ns"], 90)
self.assertEqual(result["repository_sync_ns"], 40)
self.assertEqual(result["peak_rss_bytes"], 1234 * 1024)
self.assertTrue((run / "transport-measurement-comparison.json").exists())
with self.assertRaises(ValueError):
collect(run, benchmark, {**process, "controller_trigger_monotonic_ns": 21}, {"required_serialized_bytes": 0}, "baseline", table)
with self.assertRaises(ValueError):
collect(run, benchmark, process, {"required_serialized_bytes": 0}, "baseline", {**table, "rrdp": {}})
atomic_json(benchmark / "engine-timing.json", {**engine, "boundary": "missing drain"})
with self.assertRaises(ValueError):
collect(run, benchmark, process, {"required_serialized_bytes": 0}, "baseline", table)
def test_transport_scope_mapping_preserves_each_raw_success_and_failure(self):
from formal_metrics import compare_transport_measurements
rows = [{"protocol": "rsync", "uri": "rsync://host:873/repo/a/0/", "success": True},
{"protocol": "rsync", "uri": "rsync://host:873/repo/b/", "success": True},
{"protocol": "rsync", "uri": "rsync://gap/repo/pp/", "success": False},
{"protocol": "rrdp", "uri": "https://host/notification.xml", "success": True}]
table = {"rrdp": {"https://host/notification.xml": "Success"},
"rsync": {"rsync://host:873/repo/": "Success", "rsync://gap/repo/": {"Failure": "gap"}}}
proof = compare_transport_measurements(rows, table, "module-root")
self.assertTrue(proof["pass"])
self.assertEqual([r["uri"] for r in proof["intervals"]], [r["uri"] for r in rows])
self.assertEqual([r["scope_uri"] for r in proof["intervals"][:2]], ["rsync://host:873/repo/"] * 2)
self.assertEqual([r["success"] for r in proof["intervals"]], [True, True, False, True])
def test_transport_scope_mapping_rejects_missing_extra_wrong_or_conflicting_rows(self):
from formal_metrics import compare_transport_measurements
good = {"protocol": "rsync", "uri": "rsync://host/repo/pp/", "success": True}
table = {"rrdp": {}, "rsync": {"rsync://host/repo/": "Success"}}
cases = [[], [{**good, "uri": "rsync://other/repo/pp/"}],
[{**good, "uri": "rsync://host/repository/pp/"}],
[good, {**good, "uri": "rsync://host/other/pp/"}],
[good, {**good, "success": False}], [{**good, "success": 1}],
[{**good, "protocol": "unknown"}]]
for rows in cases:
with self.subTest(rows=rows), self.assertRaises(ValueError):
compare_transport_measurements(rows, table, "module-root")
for outcome in ["Skipped", {}, {"Failure": None}]:
with self.subTest(outcome=outcome), self.assertRaises(ValueError):
compare_transport_measurements([good], {"rrdp": {}, "rsync": {"rsync://host/repo/": outcome}}, "module-root")
def test_publication_and_host_scope_keep_fetch_uri_and_reject_unknown_policy(self):
from formal_metrics import compare_transport_measurements
row = {"protocol": "rsync", "uri": "rsync://host/repo/pp/", "success": True}
exact = {"rrdp": {}, "rsync": {row["uri"]: "Success"}}
module = {"rrdp": {}, "rsync": {"rsync://host/repo/": "Success"}}
for scope in ["publication-point", "host"]:
self.assertTrue(compare_transport_measurements([row], exact, scope)["pass"])
with self.assertRaises(ValueError):
compare_transport_measurements([row], module, scope)
for scope in [None, "", "unknown"]:
with self.assertRaises(ValueError):
compare_transport_measurements([row], exact, scope)
if __name__ == "__main__":
unittest.main()