Locust.io 当我 start/stop 换一个新的 运行 时,我如何在 locust.io 上使用不同的用户而不会出现系统故障

Locust.io how could i use different users on locust.io without the system bugging when i start/stop for a new run

我的代码在 locust.io 部署的服务器上对第一个 运行 工作正常,但是当我 start/stop 创建一个新的 运行 我得到多个错误,关于列表。你能帮我循环一下吗?

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)

错误是:

[2020-08-18 23:06:50,899] PC/ERROR/locust.user.task: 'somethingclass' 对象没有属性 'id' 追溯(最近一次通话): 文件“c:\windows\system32\src\locust\locust\user\task.py”,第 284 行,在 运行 中 self.execute_next_task() 文件“c:\windows\system32\src\locust\locust\user\task.py”,第 309 行,在 execute_next_task self.execute_task(self._task_queue.pop(0)) 文件“c:\windows\system32\src\locust\locust\user\task.py”,第 321 行,在 execute_task 中 任务(自己) 文件“C:\Users\user\Desktop\Work\Chipico Chip Transfer\chiptransfer_load.py”,第 44 行,在某个地方 "uuid": '%s' % self.id, AttributeError: 'somethingclass' 对象没有属性 'id'

您可以使用 test_start and test_stop events 重置您的 user 数据。

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]

这给了我以下多次停止和开始测试的输出。

[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')]

from locust import 用户、任务、常量、任务集、事件 从日志记录导入 getLogger 导入 uuid

logger = getLogger("测试") 用户 = []

@events.test_start.add_listener def start_something(**kwargs): logger.info("开始测试...") user.append(uuid.uuid4())

class 某事class(任务集): @任务 def do_something(自我): logger.info(f"用户:{user}")

class 主要(用户): wait_time = 常量(2) 任务 = [某事class]

是正确答案