locust: Use random user for each session.
Allow accounts to (optionally) be used simultaneously.
from html.parser import HTMLParser
import json
from os import environ
+import random
import re
from urllib.parse import urlparse
from locust import HttpUser, task
@@ -100,15 +101,24 @@ class MyAppsWithdrawParser(HTMLParser):
class LoggedInUser(HttpUser):
def on_start(self):
- self.user = {}
+ """
+ Login a random user from wpUsers.
+ """
+ random.shuffle(wpUsers)
+ # reuse = true if the user may have multiple simultaneous sessions.
+ if "reuse" in wpUsers[0] and wpUsers[0]["reuse"] == True:
+ self.user = wpUsers[0]
+ else:
+ self.user = wpUsers.pop(0)
+ if self.user and "wpName" in self.user and "wpPassword" in self.user:
self.login()
+ else:
+ raise Exception("login failed: credentials required.")
def on_stop(self):
self.logout()
def login(self):
- self.user = wpUsers.pop(0)
- if self.user and self.user["wpName"] and self.user["wpPassword"]:
name = "/oauth/login/?next=/users/my_library/"
with self.client.get(
name,
@@ -145,17 +155,25 @@ class LoggedInUser(HttpUser):
post_login.failure("login failed: No sessionid")
if post_login.status_code != 200:
post_login.failure(
- "post_login status code: "
- + str(post_login.status_code)
+ "post_login status code: " + str(post_login.status_code)
)
- else:
- raise Exception("login failed: credentials required.")
def logout(self):
self.client.get(
"/accounts/logout/?next=/",
)
- if self.user["wpName"] and self.user["wpPassword"]:
+ # if the user wasn't reusable, return them to the pool now that we're done.
+ if not (
+ all(
+ key in self.user
+ for key in (
+ "reuse",
+ "wpName",
+ "wpPassword",
+ )
+ )
+ and self.user["reuse"] == True
+ ):
wpUsers.append(self.user)
self.user = {}
