63 lines
2.9 KiB
Python
Executable File
63 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Deterministic oracle for HTTP Age / local freshness-cap algebra.
|
|
No binary execution, no claim of measured throughput. Run with Python 3 only.
|
|
"""
|
|
import json
|
|
import random
|
|
from pathlib import Path
|
|
|
|
|
|
def old_freshness(max_age, header_age, apparent_age, response_delay, local_limit):
|
|
age = max(header_age, apparent_age) + response_delay
|
|
return max(0, min(max_age, local_limit) - age)
|
|
|
|
|
|
def new_freshness(max_age, header_age, apparent_age, response_delay, local_limit):
|
|
# RFC 9111 corrected_initial_age = max(apparent_age, age_value + response_delay)
|
|
age = max(apparent_age, header_age + response_delay)
|
|
lifetime = min(max_age, age + local_limit)
|
|
return max(0, lifetime - age)
|
|
|
|
|
|
def run():
|
|
cases = [
|
|
('user_cdn_example', 2_592_000, 1_553_483, 0, 0, 86_400),
|
|
('user_cdn_example_7d', 2_592_000, 1_553_483, 0, 0, 604_800),
|
|
('origin_nearly_stale', 2_592_000, 2_591_990, 0, 0, 86_400),
|
|
('origin_stale', 2_592_000, 2_592_010, 0, 0, 86_400),
|
|
('normal_small', 3_600, 0, 0, 0, 86_400),
|
|
('date_is_older', 3_600, 10, 600, 2, 86_400),
|
|
('rfc_delay_case', 3_600, 3, 10, 2, 86_400),
|
|
]
|
|
report = {'reference': 'RFC 9111 4.2.3 (Age) and 4.2.1 (freshness lifetime)',
|
|
'kind': 'algebra simulation; not RustProxy RPS benchmark',
|
|
'scenarios': [], 'randomized_checks': 0, 'false_miss_cases_old': 0}
|
|
for name, max_age, age, apparent, delay, local in cases:
|
|
old = old_freshness(max_age, age, apparent, delay, local)
|
|
new = new_freshness(max_age, age, apparent, delay, local)
|
|
report['scenarios'].append({'scenario': name, 'origin_max_age_seconds': max_age,
|
|
'received_age_seconds': age, 'apparent_age_seconds': apparent,
|
|
'response_delay_seconds': delay, 'local_max_ttl_seconds': local,
|
|
'old_freshness_seconds': old, 'new_freshness_seconds': new})
|
|
rng = random.Random(0x3400)
|
|
for _ in range(100_000):
|
|
origin = rng.randrange(0, 1 << 24)
|
|
header_age = rng.randrange(0, 1 << 24)
|
|
apparent = rng.randrange(0, 1 << 24)
|
|
delay = rng.randrange(0, 300)
|
|
local = rng.randrange(0, 604_801)
|
|
corrected_age = max(apparent, header_age + delay)
|
|
new = new_freshness(origin, header_age, apparent, delay, local)
|
|
expected = min(max(0, origin - corrected_age), local)
|
|
assert new == expected, (origin, header_age, apparent, delay, local, new, expected)
|
|
assert new <= local and new <= max(0, origin - corrected_age)
|
|
old = old_freshness(origin, header_age, apparent, delay, local)
|
|
report['false_miss_cases_old'] += old == 0 < new
|
|
report['randomized_checks'] += 1
|
|
return report
|
|
|
|
|
|
if __name__ == '__main__':
|
|
report = run()
|
|
print(json.dumps(report, ensure_ascii=False, indent=2))
|