-1

I have this code that works fine for the first run on a locust.io deployed server, but when i start/stop to make a new run i get multiple errors, in regard with the list. Could you guyz help me to be able to cycle it?

from locust import HttpUser, task, between, SequentialTaskSet, events
import uuid
import json
import csv
import logging, sys, random, os

with open('somecsv.csv', 'r') as f:
    reader = csv.reader(f)
    user= list(reader)
    #print(user)

class somethingclass(SequentialTaskSet):

    def on_start(self):
        if len(user) > 0:
            self.id= str(user.pop()).strip("][''")

    @task
    def someting(self):
        do something with self.id request
  
    @task
    def someting2(self):
        do something with self.id request
                        


class Main(HttpUser):
    
    wait_time = between(5, 10)
    tasks = [somethingclass]
    
    def _init_(self, *args, **kwargs):
        super(Main, self)._init_(*args, **kwargs)

errors are:

[2020-08-18 23:06:50,899] PC/ERROR/locust.user.task: 'somethingclass' object has no attribute 'id' Traceback (most recent call last): File "c:\windows\system32\src\locust\locust\user\task.py", line 284, in run self.execute_next_task() File "c:\windows\system32\src\locust\locust\user\task.py", line 309, in execute_next_task self.execute_task(self._task_queue.pop(0)) File "c:\windows\system32\src\locust\locust\user\task.py", line 321, in execute_task task(self) File "C:\Users\user\Desktop\Work\Chipico Chip Transfer\chiptransfer_load.py", line 44, in someting "uuid": '%s' % self.id, AttributeError: 'somethingclass' object has no attribute 'id'

  • Hello, Razvan! Can you share the errors? – Mihai Aug 18 '20 at 20:03
  • Hi Mihai, i have added the errors – Razvan Ristea Aug 18 '20 at 20:10
  • Ok, the problem with the code is that you are popping users from the list, the second time you try to run the test, locust does not try to load the users anymore, probably from the way locust runs the script. Is it possible to randomly choose users from the list and not mutate it - like using random.choice to get a user? – Mihai Aug 18 '20 at 20:26
  • no, it needs to be sequential – Razvan Ristea Aug 18 '20 at 20:27
  • I think a version of your question is answered here https://stackoverflow.com/questions/23016278/way-to-use-locust-io-by-supplying-user-list . The comments describe multiple ways of ensuring that the list is refreshed. Only one note, `hatch_complete` event was renamed to spawning_complete. Check this example for more details: https://github.com/locustio/locust/blob/10f88e2107977164a1d2e1390cdde24751973b70/examples/semaphore_wait.py – Mihai Aug 18 '20 at 20:45
  • I have read the question, and tried everything there and still to no avail for me, and regarding Semaphore i do not know how to implement it. – Razvan Ristea Aug 18 '20 at 21:21

2 Answers2

1

You could use test_start and test_stop events to reset your user data.

from locust import User, task, constant, TaskSet, events
from logging import getLogger
import uuid

logger = getLogger("test")
user = []

@events.test_start.add_listener
def start_something(**kwargs):
    logger.info("Starting test...")
    user.append(uuid.uuid4())

class somethingclass(TaskSet):
    @task
    def do_something(self):
        logger.info(f"User: {user}")

class Main(User):
    wait_time = constant(2)
    tasks = [somethingclass]

This gives me the following output with multiple stopping and starting of tests.

[2020-08-18 16:14:12,549] INFO/locust.main: Starting web interface at http://:8089
[2020-08-18 16:14:12,557] INFO/locust.main: Starting Locust 1.1.1
[2020-08-18 16:14:15,759] INFO/test: Starting test...
[2020-08-18 16:14:15,759] INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-08-18 16:14:15,759] INFO/locust.runners: All users hatched: Main: 1 (0 already running)
[2020-08-18 16:14:15,760] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd')]
[2020-08-18 16:14:17,764] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd')]
[2020-08-18 16:14:19,765] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd')]
[2020-08-18 16:14:21,767] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd')]
[2020-08-18 16:14:28,674] INFO/test: Starting test...
[2020-08-18 16:14:28,675] INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-08-18 16:14:28,675] INFO/locust.runners: All users hatched: Main: 1 (0 already running)
[2020-08-18 16:14:28,675] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb')]
[2020-08-18 16:14:30,679] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb')]
[2020-08-18 16:14:32,682] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb')]
[2020-08-18 16:14:34,688] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb')]
[2020-08-18 16:14:38,426] INFO/test: Starting test...
[2020-08-18 16:14:38,427] INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-08-18 16:14:38,427] INFO/locust.runners: All users hatched: Main: 1 (0 already running)
[2020-08-18 16:14:38,428] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb'), UUID('cfd2a456-87d4-4cb2-b7d2-939bb1dfc560')]
[2020-08-18 16:14:40,433] INFO/test: User: [UUID('f4e9d580-af4a-4fb7-804a-372faa8fcacd'), UUID('c061117e-f585-4da6-8188-59a0e1ff8acb'), UUID('cfd2a456-87d4-4cb2-b7d2-939bb1dfc560')]
Solowalker
  • 2,431
  • 8
  • 13
  • This did not seem to work for me, still "c:\windows\system32\src\locust\locust\user\task.py", line 321, in execute_task task(self) File "C:\Users\user\Desktop\Work\Chipico Chip Transfer\chiptransfer_load.py", line 44, in someting "uuid": '%s' % self.id, AttributeError: 'somethingclass' object has no attribute 'id' – Razvan Ristea Aug 18 '20 at 21:21
  • It's a bit suspicious that your line number is the same… but did you verify the function is being called? Is it re-reading the csv properly each time it is? – Solowalker Aug 18 '20 at 21:28
  • @events.test_start.add_listener only start to run after the first initial run from what i can tell – Razvan Ristea Aug 18 '20 at 21:32
  • Edited my answer for clarity and to show that it should work. – Solowalker Aug 18 '20 at 22:23
  • it's stil not working for me as i need to read a csv document x times, because i am using more than one hatch. and afterwards i need to reinitialize the file to it's original state so that the next hatch can use it on start/stop – Razvan Ristea Aug 18 '20 at 23:03
  • Yes, it works; i made a mistake the first time i have implemented – Razvan Ristea Aug 19 '20 at 07:55
-1

from locust import User, task, constant, TaskSet, events from logging import getLogger import uuid

logger = getLogger("test") user = []

@events.test_start.add_listener def start_something(**kwargs): logger.info("Starting test...") user.append(uuid.uuid4())

class somethingclass(TaskSet): @task def do_something(self): logger.info(f"User: {user}")

class Main(User): wait_time = constant(2) tasks = [somethingclass]

is the correct answer