~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
74 lines · 2.7 KB Raw History
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = []
# ///
import http.client
import json
import os
from pathlib import Path
import subprocess
import sys
from urllib.parse import urlencode, urlsplit


def resident_bytes(pid):
    statm = Path(f"/proc/{pid}/statm")
    if statm.exists():
        return int(statm.read_text().split()[1]) * os.sysconf("SC_PAGE_SIZE")
    return int(subprocess.check_output(["ps", "-o", "rss=", "-p", str(pid)])) * 1024


def main():
    base, pid, repo, collection, rkey, cid = sys.argv[1:]
    url = urlsplit(base)
    conn = http.client.HTTPConnection(url.hostname, url.port, timeout=10)
    params = {"repo": repo, "collection": collection, "rkey": rkey}
    paths = {
        "describeRepo": [("repo", repo)],
        "getRecord": list(params.items()),
        "listRecords": [("repo", repo), ("collection", collection)],
    }
    try:
        for method, query in paths.items():
            path = "/xrpc/com.atproto.repo." + method + "?" + urlencode(query)

            def read(index):
                target = path
                expected_status = 200
                if method == "getRecord" and index % 2:
                    target += "&cid=missing"
                    expected_status = 400
                if method == "listRecords" and index % 2:
                    target = "/xrpc/com.atproto.repo.listRecords?" + urlencode(
                        {"repo": repo, "collection": "dev.zds.memory.empty"}
                    )
                conn.request("GET", target)
                response = conn.getresponse()
                body = json.loads(response.read())
                assert response.status == expected_status, (method, response.status, body)
                if expected_status == 400:
                    assert body["error"] == "RecordNotFound", body
                elif method == "describeRepo":
                    assert body["did"] == repo, body
                elif method == "getRecord":
                    assert body["cid"] == cid and body["value"]["$type"] == collection, body
                elif index % 2:
                    assert body["records"] == [], body
                else:
                    assert any(record["cid"] == cid for record in body["records"]), body

            for index in range(64):
                read(index)
            before = resident_bytes(pid)
            for index in range(1024):
                read(index)
            growth = resident_bytes(pid) - before
            print(f"repo memory: {method}, 1024 reads, RSS growth {growth / 1048576:.2f} MiB")
            assert growth < 8 * 1024 * 1024, f"{method} retained {growth} bytes"
    finally:
        conn.close()


if __name__ == "__main__":
    main()