HttpLocust类

可定义多个HttpLocust类,即多个用户可执行不同的任务或者相同的任务,但是执行频率不一样,用weight进行约定。

# coding:utf-8
from locust import HttpLocust, TaskSet, task
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) class UserTask1(TaskSet):
@task
def task1(self):
self.client.get("/", headers=header, verify=False) class UserTask2(TaskSet):
@task
def task2(self):
self.client.get("/belle-ls/", verify=False) class UserOne(HttpLocust):
weight = 1
task_set = UserTask1 class UserTwo(HttpLocust):
weight = 2
task_set = UserTask2

终端命令:

 $ locust -f locustDemo2.py UserOne UserTwo --host=https://www.cnblogs.com
[2019-03-07 14:55:49,299] LiuShuangdeiMac.local/INFO/locust.main: Starting web monitor at *:8089
[2019-03-07 14:55:49,300] LiuShuangdeiMac.local/INFO/locust.main: Starting Locust 0.9.0
[2019-03-07 14:55:58,657] LiuShuangdeiMac.local/INFO/locust.runners: Hatching and swarming 10 clients at the rate 10 clients/s...
[2019-03-07 14:55:59,678] LiuShuangdeiMac.local/INFO/locust.runners: All locusts hatched: UserTwo: 7, UserOne: 3

运行结果如下,UserTwo 的执行频率是UserOne的两倍多

TaskSet类:执行频率的约定及嵌套

1. task修饰符 @task or @task(1)  1为权重,权重越高,执行比例越大

2. taskSet嵌套,需要用 interrupt()跳出

 写法1:

# coding:utf-8

from locust import HttpLocust, TaskSet, task
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class UserTask(TaskSet):
@task(2)
class stayMe(TaskSet):
@task(2)
def MyHome(self):
self.client.get("/belle-ls/", verify=False) @task(4)
def MyCote(self):
self.client.get("/belle-ls/category/1412671.html", verify=False) @task(1)
def Out(self):
self.interrupt() #跳出当前TaskSet类,才有机会执行其他行为 @task(1)
def BlogHome(self):
self.client.get("/", verify=False)
class User(HttpLocust):
task_set = UserTask

写法2:

class stayMe(TaskSet):
@task(2)
def MyHome(self):
self.client.get("/belle-ls/", verify=False) @task(4)
def MyCote(self):
     self.client.get("/belle-ls/category/1412671.html", verify=False) @task(1)
def Out(self):
self.interrupt() class UserTask(TaskSet):
tasks= {stayMe:2}
@task(1)
def BlogHome(self):self.client.get("/", verify=False)class User(HttpLocust):
task_set = UserTask

ResponseContextManager类:

class UserTask(TaskSet):
@task(1)
def BlogHome(self):
with self.client.get("/", headers = header, catch_response = True, verify = False) as response:
if response.status_code == 200:
response.failure('Failed!')
else:
response.success() class User(HttpLocust):
task_set = UserTask

ResponseContextManger类是Response类的子类,多了两个failure()和success()方法。

上面的例子:使用with语句及catch_response参数可以截获原始响应,把所有status_code是200的响应都当做失败响应。这里success()和failure(str)的调用会体现在结果的统计上。

合并请求:

比如进入博客分类,其实属于一条测试用例,但是由于参数的不同,会再测试结果中显示两行,比如:

https://www.cnblogs.com/belle-ls/category/1412671.html

https://www.cnblogs.com/belle-ls/category/1411809.html

如何合并呢?可以在get请求参数中加个name参数来将统计叠加在一起name也可以用来对Name显示为路径进行重命名

class UserTask(TaskSet):
@task(1)
def BlogHome(self):
self.client.get("/belle-ls/category/1412671.html", headers=header, verify=False, name = "category")
self.client.get("/belle-ls/category/1411809.html", headers=header, verify=False, name = "category") class User(HttpLocust):
task_set = UserTask

效果:

参考文章:https://blog.csdn.net/a464057216/article/details/48394213

Locust 类的使用的更多相关文章

  1. 性能测试工具Locust的使用----TaskSet类~~task任务嵌套

    内容来自网络 http://blog.sina.com.cn/s/blog_a7ace3d80102w9r0.html TaskSet类 正如字面意思,TaskSet类定义了每个用户的任务集合,测试任 ...

  2. 性能测试工具Locust

    An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...

  3. Python Locust性能测试框架实践

    [本文出自天外归云的博客园] Locust的介绍 Locust是一个python的性能测试工具,你可以通过写python脚本的方式来对web接口进行负载测试. Locust的安装 首先你要安装pyth ...

  4. Locust性能测试框架,从入门到精通

    1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 主要特点如下: 使用普通的P ...

  5. 性能测试工具Locust的使用

    一.写在前面 官网:https://www.locust.io/ 官方使用文档:https://docs.locust.io/en/latest/ 大并发量测试时,建议在linux系统下进行. 二.L ...

  6. Locust性能测试学习总结

    Locust学习总结分享 简介: Locust是一个用于可扩展的,分布式的,性能测试的,开源的,用Python编写框架/工具,它非常容易使用,也非常好学.它的主要思想就是模拟一群用户将访问你的网站.每 ...

  7. Python3中性能测试工具Locust安装使用

    Locust安装使用: 安装: python3中           ---> pip3 install locust 验证是否安装成功---> 终端中输入 locust --help  ...

  8. python locust 性能测试:HOOKS<钩子方法>

    为locust中不同类型的事件,提供的钩子方法: from locust import TaskSet, task, events, Locust from locust.clients import ...

  9. python locust 性能测试:嵌套

    TaskSet类和TaskSequence类可用于嵌套<可以在TaskSequences中嵌套TaskSets,反之亦然>: from locust import TaskSet, tas ...

随机推荐

  1. Linux下软件常见安装方式

    pasting  分类: Linux2007-12-08 16:31 1909人阅读 评论(0) 收藏 举报 linuxredhat脚本文档managerfile        Linux下软件安装主 ...

  2. atom插件记录

    ├── Zen@0.16.4 写作的时候用,很给力 ├── atom-beautify@0.29.9 美化一切代码 ├── autocomplete-paths@1.0.2 路径自动提示 ├── au ...

  3. CodeForces 346A Alice and Bob (数学最大公约数)

    题意:有一堆数,然后有两个人轮流从中取出两个数,这两个数的差的绝对值不在这个集合,然后把这个数放进这个集合,如果哪个人不能拿了,就是输了,问你谁赢. 析:当时连题意都没看好,以为拿出两个数,就不放回了 ...

  4. 使用word 2007 发布csdn博客

    目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...

  5. 使用GeoServer+QGIS发布WMTS服务 | Publishing WMTS Service Using GeoServer+QGIS

    Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...

  6. .NET基础 (04)基础类型和语法

    基础类型和语法1 .NET中所有内建类型的基类是什么2 System.Object中包含哪些方法,哪些是虚方法3 值类型和引用类型的区别4 简述装箱和拆箱原理5 C#中是否有全局变量6 struct和 ...

  7. Android-AndroidStudio Run 'app'安装APK到设备的过程

    1.AndroidStudio 点击Run ‘app’. 2.点击Run ‘app’就会将所有.class文件用SDK工具集处理成.dex, 用SDK工具集将图片/资源/布局文件/AndroidMan ...

  8. [LeetCode 题解]: Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. vmware虚拟机监控数据

    在vsphere产品中内建一个监控所有虚机包括主机资源的插件,叫做vcenter servcie status,这个插件的主要功能是记录当前虚拟机资源的cpu.硬盘.内存和网络等相关信息.通过它可以查 ...

  10. 谷歌三大核心技术(一)Google File System中文版

    http://www.open-open.com/lib/view/open1328763454608.html