There are multiple Users in a locustfile
from locust import task, HttpUser
class User01(HttpUser):
weight = 3 # 权重
host = 'https://www.baidu.com'
@task
def user_01_task(self):
self.client.get(url='/', name='user_01_task')
class User02(HttpUser):
weight = 1 # 权重
host = 'https://www.qq.com'
@task
def user_02_task(self):
self.client.get(url='/', name='user_02_task')
- Two users are defined
host
, and the host can be omitted at runtime - Defined
weight
, the same as the weight set to the task in the previous section, the user allocation ratio is executed according to 3:1 at runtime
- Results of the
Use --class-picker to specify execution
- The command to run locust uses the --class-picker parameter, and you can manually select the UserClasses to execute when starting the web-ui
$ locust -f locustfiles/multiple_user.py --class-picker
- If it is in no-ui mode, you can also directly specify the executed UserClasses in the command, for example
$ locust -f locustfiles/multiple_user.py --class-picker User02 User01 --headless -u 20 -r 20 -t 5s
[2023-07-28 12:17:00,956] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 20 users at a rate of 20.00 per second
[2023-07-28 12:17:00,957] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"User01": 15, "User02": 5} (20 total users)
- Explain the command parameters
- --headless no-ui mode execution, and start testing immediately
- -u specifies the maximum number of concurrent users to run
- -r number of users started per second
- -t time to execute the test
summary
weight
Multiple test User classes can be defined in one locustfile, and the default execution ratio is 1:1, and their execution ratio can be modified by using attributes- When there are multiple User classes in a locustfile, you can use the --class-picker command parameter to select which User classes to execute to execute the test
- The no-ui execution is mentioned here, because in general, our stress test is executed on the cloud server in actual work, and locust also provides the --headless command to execute through no-ui
- After assigning a value to the host in the User class, we don't need to set the host for locust at runtime
- Locust uses more command parameters, it is recommended to read it carefully, and
locust -h
check it by
Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.