本文将继续对Locust性能测试进行持续讲解,主要是讲解虚拟用户数分配和权重的关系。在locust file中进行多用户类的实现和操作。我们这次先上完整的代码:

from locust import User,between,task
import time

class WebUser(User):
    wait_time = between(5,9)
    weight = 3
    @task
    def task_1(self):
        nowTime = time.strftime("%Y-%m-%d %H:%M:%S")
        print("This is a web user, test time is: "+nowTime)

class MobileUser(User):
    wait_time = between(5,9)
    weight = 1
    @task
    def task_2(self):
        nowTime = time.strftime("%Y-%m-%d %H:%M:%S")
        print("This is a mobile user, test time is: "+nowTime)

代码分析。先从全局看,line 4和line 12是分别定义了2个用户类,这两个类都是继承User类。也都定义了wait_time属性。
       在用户 WebUser中定义了一个任务方法task_1,在用户MobileUser中定义了一个任务方法 task_2。

其次是两个用户类中都定义了weight权重属性设置,其中WebUser为3(line 6), MobileUser为1(line 14)。

测试:

1)当模拟用户数为1时,测试log如下,可以发现这1个虚拟用户是作用在WebUser上的,而MobileUser上作用的虚拟用户数为0.所以,下面的log也没有MobileUser相关的任务被执行。

[2020-09-22 20:37:17,384] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1
[2020-09-22 20:37:23,794] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-09-22 20:37:23,794] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 1, MobileUser: 0 (0 already running)
This is a web user, test time is: 2020-09-22 20:37:23
This is a web user, test time is: 2020-09-22 20:37:29
This is a web user, test time is: 2020-09-22 20:37:35
This is a web user, test time is: 2020-09-22 20:37:41
This is a web user, test time is: 2020-09-22 20:37:48
This is a web user, test time is: 2020-09-22 20:37:57
This is a web user, test time is: 2020-09-22 20:38:04
This is a web user, test time is: 2020-09-22 20:38:12

     2)当模拟用户数为2时,测试log如下,发现这2个用户都是作用在WebUser上的。

[2020-09-22 21:11:42,893] jasondeMacBook-Pro.local/INFO/locust.main: Starting web interface at http://:8089
[2020-09-22 21:11:42,899] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1
[2020-09-22 21:11:49,142] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 2 users at the rate 1 users/s (0 users already running)...
This is a web user, test time is: 2020-09-22 21:11:49
[2020-09-22 21:11:50,147] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 2, MobileUser: 0 (0 already running)
This is a web user, test time is: 2020-09-22 21:11:50
This is a web user, test time is: 2020-09-22 21:11:57
This is a web user, test time is: 2020-09-22 21:11:57

  3)当模拟用户数为4时,测试log如下,发现有3个用户作用在WebUser,1个用户作用在MobileUser上。

[2020-09-22 21:13:24,883] jasondeMacBook-Pro.local/INFO/locust.main: Starting web interface at http://:8089
[2020-09-22 21:13:24,892] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1
[2020-09-22 21:13:29,896] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 4 users at the rate 1 users/s (0 users already running)...
This is a web user, test time is: 2020-09-22 21:13:29
This is a web user, test time is: 2020-09-22 21:13:30
This is a web user, test time is: 2020-09-22 21:13:31
[2020-09-22 21:13:32,899] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 3, MobileUser: 1 (0 already running)
This is a mobile user, test time is: 2020-09-22 21:13:32
This is a web user, test time is: 2020-09-22 21:13:37
This is a web user, test time is: 2020-09-22 21:13:37
This is a web user, test time is: 2020-09-22 21:13:38
This is a mobile user, test time is: 2020-09-22 21:13:41

总结:

1)一个场景文件中可以定义多个用户类,方便对业务逻辑进行分类处理。并且可以为多个用户类设置权重。

2)如果用户类权重不足1人,则会被忽略,如测试第1,2中情况。

Locust性能-零基础入门系列(3)_Locust