From 9e836ea152e745cda6269983815e36464b03ce72 Mon Sep 17 00:00:00 2001 From: retoor Date: Fri, 5 Jun 2026 19:32:46 +0200 Subject: [PATCH] Update --- locustfile.py | 98 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/locustfile.py b/locustfile.py index 1e59938..e5ec3e5 100644 --- a/locustfile.py +++ b/locustfile.py @@ -135,19 +135,20 @@ def seed_data(environment, **kwargs): logger.info(f"Created {len(POST_UIDS)} seed posts") - for seed in SEED_USERS[:2]: - try: - opener = logged_in_opener(seed["email"]) - body = urllib.parse.urlencode({ - "content": f"Seed comment by {seed['username']}", - "post_uid": POST_UIDS[0], - }).encode() - opener.open( - urllib.request.Request(f"{host}/comments/create", data=body), - timeout=10, - ) - except Exception as e: - logger.warning(f"Create seed comment failed: {e}") + for post_uid in POST_UIDS: + for seed in SEED_USERS[:2]: + try: + opener = logged_in_opener(seed["email"]) + body = urllib.parse.urlencode({ + "content": f"Seed comment by {seed['username']}", + "post_uid": post_uid, + }).encode() + opener.open( + urllib.request.Request(f"{host}/comments/create", data=body), + timeout=10, + ) + except Exception as e: + logger.warning(f"Create seed comment failed: {e}") for seed in SEED_USERS[:2]: try: @@ -306,11 +307,11 @@ def seed_data(environment, **kwargs): except Exception as e: logger.warning(f"Harvest from {seed['username']} failed: {e}") - if POST_UIDS: + for post_uid in POST_UIDS: try: opener = logged_in_opener(SEED_USERS[0]["email"]) resp = opener.open( - urllib.request.Request(f"{host}/posts/{POST_UIDS[0]}"), + urllib.request.Request(f"{host}/posts/{post_uid}"), timeout=10, ) html = resp.read().decode("utf-8", errors="replace") @@ -1021,3 +1022,70 @@ class AdminUser(HttpUser): self.client.post( f"/admin/users/{uid}/toggle", name="admin/users/toggle" ) + + +class AnonymousUser(HttpUser): + weight = 3 + wait_time = between(1, 5) + + @task(5) + def browse_feed(self): + self.client.get("/feed", name="feed") + + @task(3) + def browse_post(self): + if POST_SLUGS: + self.client.get( + f"/posts/{random.choice(POST_SLUGS)}", name="posts/[uid]" + ) + + @task(2) + def browse_public(self): + path = random.choice([ + "/", "/news", "/gists", "/projects", "/leaderboard", + ]) + self.client.get(path, name="public/[page]") + + @task(2) + def browse_detail(self): + if NEWS_SLUGS: + self.client.get( + f"/news/{random.choice(NEWS_SLUGS)}", name="news/[slug]" + ) + if GIST_SLUGS: + self.client.get( + f"/gists/{random.choice(GIST_SLUGS)}", name="gists/[slug]" + ) + if PROJECT_SLUGS: + self.client.get( + f"/projects/{random.choice(PROJECT_SLUGS)}", name="projects/[slug]" + ) + + @task(2) + def guest_vote_redirects_to_login(self): + if not POST_UIDS: + return + uid = random.choice(POST_UIDS) + with self.client.post( + f"/votes/post/{uid}", data={"value": 1}, + headers={"x-requested-with": "fetch"}, + catch_response=True, name="votes/post/guest-redirect", + ) as resp: + if "/auth/login" in resp.url: + resp.success() + else: + resp.failure(f"guest vote not redirected to login: {resp.url}") + + @task(1) + def guest_guarded_get_redirects_to_login(self): + with self.client.get( + "/messages", catch_response=True, name="guarded/guest-redirect", + ) as resp: + if "/auth/login" in resp.url: + resp.success() + else: + resp.failure(f"guarded GET not redirected to login: {resp.url}") + + @task(1) + def login_with_next(self): + self.client.get("/auth/login", params={"next": "/feed"}, name="auth/login?next")