forked from mtusi2026/mtusi2026lab4
grade4
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import base64, concurrent.futures, json, pathlib, time, urllib.request, uuid
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
|
||||
def _req(method, path, body=None, headers=None, token=None):
|
||||
h = {"Accept": "application/json"}
|
||||
if token: h["Authorization"] = f"Bearer {token}"
|
||||
h.update(headers or {})
|
||||
req = urllib.request.Request(BASE + path, data=body, headers=h, method=method)
|
||||
with urllib.request.urlopen(req) as r:
|
||||
return r.status, r.read()
|
||||
|
||||
def register_and_login():
|
||||
u = f"u_{uuid.uuid4().hex[:10]}"
|
||||
p = "T3st-" + uuid.uuid4().hex
|
||||
_req("POST", "/api/v1/users",
|
||||
json.dumps({"username": u, "display_name": "Load", "password": p}).encode(),
|
||||
{"Content-Type": "application/json"})
|
||||
basic = base64.b64encode(f"{u}:{p}".encode()).decode()
|
||||
_, body = _req("POST", "/api/v1/auth/sessions",
|
||||
headers={"Authorization": f"Basic {basic}"})
|
||||
return json.loads(body)["token"]
|
||||
|
||||
def make_chat(token):
|
||||
_, me = _req("GET", "/api/v1/users/me", token=token)
|
||||
uid = json.loads(me)["id"]
|
||||
_, chat = _req("POST", "/api/v1/chats",
|
||||
json.dumps({"title": "load", "member_ids": [uid]}).encode(),
|
||||
{"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex},
|
||||
token=token)
|
||||
return json.loads(chat)["id"]
|
||||
|
||||
def main():
|
||||
token = register_and_login()
|
||||
chat = make_chat(token)
|
||||
png = pathlib.Path("tests/fixtures/sample.png").read_bytes()
|
||||
|
||||
def one(_):
|
||||
try:
|
||||
status, _ = _req("POST",
|
||||
f"/api/v1/chats/{chat}/attachments?filename=f.png",
|
||||
png, {"Content-Type": "image/png"}, token)
|
||||
return status
|
||||
except Exception as e:
|
||||
return repr(e)
|
||||
|
||||
t0 = time.monotonic()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex:
|
||||
results = list(ex.map(one, range(10)))
|
||||
print(json.dumps({"results": results,
|
||||
"elapsed_s": round(time.monotonic() - t0, 3)}))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user