locustio / locust

Write scalable load tests in plain Python 🚗💨
MIT License
24.58k stars 2.96k forks source link

Want iteration control and worker shutdown #2883

Open aazammazaa opened 2 weeks ago

aazammazaa commented 2 weeks ago

Prerequisites

Description

class UserBehavior1(SequentialTaskSet): max_iterations = 2

def on_start(self):
    self.iterations = 0    # Initialize the counter in the on_start method

@task
def my_task(self):
    if self.iterations >= self.max_iterations:
        self.user.environment.runner.stop()
    try:
        logging.info("hello2")
        self.client.get("/time")
        self.iterations += 1
    except Exception as e:
        logging.info(f"Error during request: {e}")
        raise RescheduleTaskImmediately

@task
def my_taskA(self):     
    logging.info("hello1A")
    self.user.environment.runner.stop()
`class WebsiteUser(HttpUser):
  def on_start(self):
      logging.info("thread started--------------------------------------------------------------")

  def on_stop(self):
      logging.info("thread stoped+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

  wait_time = between(1, 5)
  tasks = [UserBehavior1]`

' I want to run this in worker node is this possible have ieration control and shutdown of task after 10 iteration. As defining a variable for iteration doesn't got the iteration counter correctly and also many time worker struck to stopped event

cyberw commented 2 weeks ago

Hi! locust-plugins offers something like that: https://github.com/SvenskaSpel/locust-plugins?tab=readme-ov-file#command-line-options