forked from mtusi2026/mtusi2026lab4
160 lines
5.6 KiB
Python
160 lines
5.6 KiB
Python
"""Controlled inventory experiment on the teaching stand, with own fixtures."""
|
|
|
|
import argparse
|
|
import json
|
|
import statistics
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import httpx
|
|
|
|
from scripts.stand import BASE, actor, auth, command, compose, ready, sql
|
|
from scripts.verify_storage import PNG, PREFIX, inside
|
|
|
|
|
|
def probe():
|
|
# Run the actual code with the reconciler service's .25 CPU /256 MiB.
|
|
code = (
|
|
PREFIX
|
|
+ """
|
|
import pathlib,resource
|
|
started=time.process_time()
|
|
result=reconcile(d,s,apply=False,grace=3600)
|
|
result['cpu_seconds']=time.process_time()-started
|
|
result['rss_peak_bytes']=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024
|
|
result['cgroup_memory_peak_bytes']=int(pathlib.Path('/sys/fs/cgroup/memory.peak').read_text())
|
|
print(json.dumps(result)); s.close(); d.close()
|
|
"""
|
|
)
|
|
return json.loads(
|
|
command(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-T",
|
|
"reconciler",
|
|
"python",
|
|
"-",
|
|
],
|
|
input=code,
|
|
)
|
|
)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--output", default="evidence/cost-repeat.json")
|
|
args = parser.parse_args()
|
|
output = Path(args.output)
|
|
assert not output.exists()
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
prefix = "objects/cost-" + uuid4().hex + "/"
|
|
result = {
|
|
"date": datetime.now(UTC).isoformat(),
|
|
"source_commit": command(["git", "rev-parse", "HEAD"]),
|
|
"complete": False,
|
|
"cells": [],
|
|
"warmups": [],
|
|
"method": "0/100/1100 extra fresh one-byte objects; one warmup then 3 repeats per dataset; real reconciler service .25 CPU/256MiB; grace3600; 8 PNG fixtures with deliberately aged PG metadata (2days); other runtime objects remain real",
|
|
"fixture_rows": 8,
|
|
"grace_seconds": 3600,
|
|
}
|
|
chat = None
|
|
ready()
|
|
compose("stop", "reconciler")
|
|
try:
|
|
with httpx.Client(
|
|
base_url=BASE, trust_env=False, timeout=20
|
|
) as client:
|
|
user, _, session = actor(client)
|
|
r = client.post(
|
|
"/api/v1/chats",
|
|
headers=auth(session),
|
|
json={
|
|
"title": "Controlled inventory",
|
|
"member_ids": [user["id"]],
|
|
},
|
|
)
|
|
assert r.status_code == 201
|
|
chat = r.json()["id"]
|
|
seed = f"""
|
|
from uuid import uuid4
|
|
data=bytes.fromhex({PNG.hex()!r})
|
|
for i in range(8):
|
|
identity=uuid4(); key={prefix!r}+'ready-'+str(i)
|
|
s.put(key=key,stream=io.BytesIO(data),size=len(data),content_type='image/png',sha256=hashlib.sha256(data).hexdigest())
|
|
with d.connection() as c:
|
|
c.execute("INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,state,uploader_instance,expires_at,created_at) VALUES(%s,%s,%s,'fixture.png','image/png',%s,%s,%s,'ready','cost-fixture',clock_timestamp()-interval '1 day',clock_timestamp()-interval '2 days')",(identity,{chat!r},{user["id"]!r},len(data),hashlib.sha256(data).hexdigest(),key))
|
|
print('true')
|
|
"""
|
|
inside(seed)
|
|
for count in [0, 100, 1100]:
|
|
if count:
|
|
start = 0 if count == 100 else 100
|
|
inside(
|
|
f"\nfor i in range({start},{count}):\n s.put(key={prefix!r}+'extra-'+str(i),stream=io.BytesIO(b'x'),size=1,content_type='application/octet-stream',sha256=hashlib.sha256(b'x').hexdigest())\nprint('true')"
|
|
)
|
|
result["warmups"].append({"extra_objects": count, **probe()})
|
|
for repeat in range(3):
|
|
cell = {
|
|
"extra_objects": count,
|
|
"repetition": repeat + 1,
|
|
**probe(),
|
|
}
|
|
result["cells"].append(cell)
|
|
assert cell["complete"] and not cell["applied"]
|
|
assert cell["ready_protected"] >= 8
|
|
controls = {
|
|
(v["rows_scanned"], v["head_requests"], v["bytes_read"])
|
|
for v in result["cells"]
|
|
}
|
|
assert len(controls) == 1, (
|
|
"Age boundary changed work; choose a fresh stand"
|
|
)
|
|
for count in [0, 100, 1100]:
|
|
assert (
|
|
len(
|
|
{
|
|
v["objects_scanned"]
|
|
for v in result["cells"]
|
|
if v["extra_objects"] == count
|
|
}
|
|
)
|
|
== 1
|
|
)
|
|
result["stable_rows_head_bytes"] = list(next(iter(controls)))
|
|
result["median_seconds"] = {
|
|
str(n): statistics.median(
|
|
v["duration_seconds"]
|
|
for v in result["cells"]
|
|
if v["extra_objects"] == n
|
|
)
|
|
for n in [0, 100, 1100]
|
|
}
|
|
result["complete"] = True
|
|
finally:
|
|
if chat:
|
|
sql(
|
|
"DELETE FROM attachments WHERE chat_id='"
|
|
+ chat
|
|
+ "' AND uploader_instance='cost-fixture'"
|
|
)
|
|
inside(
|
|
f"\nfor o in s.objects():\n if o.object_name.startswith({prefix!r}): s.delete(key=o.object_name)\nprint('true')"
|
|
)
|
|
compose("up", "-d", "--no-build", "--no-recreate", "reconciler")
|
|
output.write_text(
|
|
json.dumps(result, ensure_ascii=False, indent=2) + "\n"
|
|
)
|
|
print(
|
|
"PASS: controlled inventory cost, 9 measurements and constant metadata/HEAD work"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|