本文将继续对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中情况。

转自公众号【TimTest】,原文地址为:https://mp.weixin.qq.com/s?__biz=MzI2OTYwMDc0MQ==&mid=2247483754&idx=1&sn=aaf495b45c3f09978b1ef0c4686be757&chksm=eadc93e0ddab1af6c159ebb784261d0667f5eba551901516b68959e349245b0d5e62be3362fb&token=2021082705&lang=zh_CN#rd

最新文章

  1. Java常见问题
  2. 【001:go语言的一些语法基础】
  3. 非空二叉树的一个有趣的性质:n0 = n2 + 1
  4. location.hash属性介绍
  5. 有关对字符串的处理,需要用到List时的简化写法
  6. android Log 等级以及在Android Studio 的Logcat中过滤方法
  7. Linux Bash代码 利用for循环实现命令的多次执行
  8. UISegmentedControl 分段器加载不同的viewcontroller
  9. 学DSP(一):开始
  10. Android 内存管理 &Memory Leak & OOM 分析
  11. Tsinsen-A1490 osu! 【数学期望】
  12. Python自学笔记-面向对象相关(Mr seven)
  13. java配置环境及安装
  14. Design Mobile实现国际化
  15. linux下安装与配置Redis
  16. 关于mybatis缓存配置讲解
  17. [EXP]ThinkPHP 5.0.23/5.1.31 - Remote Code Execution
  18. ThreadLocal终极源码剖析-一篇足矣!
  19. Nginx软件优化【转】
  20. P3980 [NOI2008]志愿者招募

热门文章

  1. 第2章 执行SparkSQL查询
  2. powerdesigner16.5 生成sql语句
  3. 查询Linux CPU架构
  4. Spring Security 入门学习--数据库认证和授权
  5. android 数据绑定(6)自定义绑定方法、双向数据绑定
  6. 跨平台C# UI库
  7. c++ binding code generator based on clang
  8. HDU - 4548-美素数 (欧拉素数筛+打表)
  9. 记录call、apply、bind的源码
  10. 柱状图bar