#!/usr/bin/env python3 from locust_plugins import run_single_user import os from locust_plugins.mongoreader import MongoReader from locust import HttpUser, task reader = MongoReader( filters=[{"tb": 0}, {"lb": 1}], id_column="ssn", uri=os.environ["LOCUST_MONGO"], database=os.environ["LOCUST_MONGO_DATABASE"], collection=os.environ["LOCUST_MONGO_COLLECTION"], ) class MyUser(HttpUser): @task def my_task(self): with reader.user() as user: self.client.get(f"/?ssn={user['ssn']}") host = "http://example.com" # allow running as executable, to support attaching the debugger if __name__ == "__main__": run_single_user(MyUser)
ssn_input.click() ssn_input.send_keys("199901010109") ssn_input.send_keys(Keys.RETURN) self.client.implicitly_wait(10) self.client.find_element( By.XPATH, '//*[@id="last-login-time"]/div/div[4]/a/span', name="logged in").click() self.client.find_element( By.CSS_SELECTOR, "body > div.fixed-top-content.js-top-content-wrapper.balance-bar-ao-brand-small > div.balance-bar-account > span.balance-bar-account-item.balance-bar-left-border.pointer.js-balance-toggle.balance-bar-toggle > span", name= "balance clickable", # this is just client side so it will be really fast ).click() self.environment.events.request_success.fire( request_type="flow", name="log in flow", response_time=(time.monotonic() - scenario_start_time) * 1000, response_length=0, ) @events.init.add_listener def on_locust_init(environment, **_kwargs): RescheduleTaskOnFail(environment) if __name__ == "__main__": run_single_user(MyUser, init_listener=on_locust_init)
# a more in-depth example of how to use run_single_user in combination with init method from locust import task, HttpUser, events from locust_plugins import run_single_user from locust_plugins.listeners import TimescaleListener class MyUser(HttpUser): @task def t(self): self.client.post("/") @events.init.add_listener def on_locust_init(environment, **_kwargs): TimescaleListener(environment, "debug") if __name__ == "__main__": MyUser.host = "http://example.com" run_single_user(MyUser, include_time=True, include_length=True, init_listener=on_locust_init)
self.image_id = random.choice(self.image_ids) # get image information response = self.client.get( f'/api/v1/images/images/{self.image_id}/slide_information/', headers={'X-CSRFToken': self.csrftoken}) self.slide_information = json.loads(response.text) #@task def index(self): result = self.client.get(f"/annotations/{self.image_id}/", headers={'X-CSRFToken': self.csrftoken}) @task def get_tile(self): level = random.randint( 0, min(self.max_level, len(self.slide_information["level_tiles"]) - 1)) num_x_tiles, num_y_tiles = self.slide_information["level_tiles"][level] x_tile = random.randint(0, num_x_tiles - 1) y_tile = random.randint(0, num_y_tiles - 1) result = self.client.get( f"/images/image/{self.image_id}/1/1/tile_files/{level}/{x_tile}_{y_tile}.jpeg", headers={'X-CSRFToken': self.csrftoken}) if __name__ == "__main__": ApiUser.host = "http://127.0.0.1:8000" #"https://exact.cs.fau.de" run_single_user(ApiUser, include_time=True, include_length=True)
) time.sleep(short_sleep) ssn_input.send_keys(Keys.RETURN) time.sleep(short_sleep) try: self.client.find_element_by_xpath('//*[@id="last-login-time"]/div/div[4]/a/span').click() except StaleElementReferenceException: # retry... self.client.find_element_by_xpath('//*[@id="last-login-time"]/div/div[4]/a/span').click() # typically you would release the customer only after its task has finished, # but in this case I dont care, and dont want to block another test run from using it # (particularly if this test crashes) self.user.environment.events.request_success.fire( request_type="Selenium", name="Log in", response_time=(time.time() - start_at) * 1000, response_length=0 ) time.sleep(short_sleep * 2) class MyWebdriverUser(WebdriverUser): task_set = UserBehaviour min_wait = 0 max_wait = 0 host = f"https://spela.{os.environ['LOCUST_TEST_ENV']}.svenskaspel.se/" def __init__(self): super().__init__(parent=None, headless=(__name__ != "__main__")) if __name__ == "__main__": run_single_user(MyWebdriverUser)
# How to use VS Code debugger with Locust from locust_plugins import run_single_user import locust_plugins.listeners from locust import task, HttpUser, events, env class MyUser(HttpUser): host = "http://example.com" @task def my_task(self): self.client.get("/fail") print("this will never be run") @events.init.add_listener def on_locust_init(environment, **_kwargs): locust_plugins.listeners.RescheduleTaskOnFailListener(environment) if __name__ == "__main__": env = env.Environment() on_locust_init(env) run_single_user(MyUser, env)
# http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html # to see how to restore the debug tracing back correctly. # # (if you know why this happens, please let me know :) from locust import task, HttpUser from locust.exception import StopUser from locust_plugins import run_single_user class MyUser(HttpUser): @task def t(self): self.client.get("/", context={"foo": 1}) self.client.get("/", context={"bar": 2}) raise StopUser() # when executed as a script, run a single locust in a way suitable for the vs code debugger if __name__ == "__main__": MyUser.host = "http://example.edu" run_single_user(MyUser, include_length=True, include_time=True, include_context=True) # You should get output similar to this: # # time type name resp_ms length exception context # 2021-06-18 01:37:23.029486 GET / 358 1256 {'foo': 1} # 2021-06-18 01:37:23.188831 GET / 159 1256 {'bar': 2}