on_start:开始前执行;
on_stop:结束后执行。

这两个方法可以帮助我们在进行性能测试时,把一些前置操作和后置处理进行规范化管理。例如在on_start获取登录的token,在on_stop清理运行产生的冗余数据。

locust脚本源码:my_locust.py

from locust import HttpUser, task, between
import loggingclass QuickstartUser(HttpUser):wait_time = between(1, 2)  # 为方便运行,缩短了一点点时间。@taskdef hello(self):self.client.get("/hello")@task(3)def world(self):self.client.get("/world")def on_start(self):login_result = self.client.post("/login", json={"username": "Tom", "password": "123456"}).textlogging.info(f"login_result:{login_result}")def on_stop(self):logout_result = self.client.post("/logout", json={"username": "Jim", "password": "456789"}).textlogging.info(f"logout_result:{logout_result}")

服务端sanic源码:main.py

from sanic import Sanic
import datetime
from sanic import responseapp = Sanic('myapp')@app.get('/hello')
def handle_request(request):time = str(datetime.datetime.now())[:-7]return response.json({"hello time": time})@app.get('/world')
def handle_request(request):time = str(datetime.datetime.now())[:-7]return response.json({"world time": time})@app.post('/login')
def handle_request(request):time = str(datetime.datetime.now())[:-7]data = request.jsonprint(f"{data}")if data:if data["username"] == "Tom" and data["password"] == "123456":return response.text("{} login success".format(data["username"]))else:return response.json({"login time": time})@app.post('/logout')
def handle_request(request):time = str(datetime.datetime.now())[:-7]data = request.jsonprint(f"{data}")if data:if data["username"] == "Jim" and data["password"] == "456789":return response.text("{} logout success".format(data["username"]))else:return response.json({"logout time": time})if __name__ == "__main__":app.run(host="127.0.0.1", port=7890, auto_reload=True)

再次运行:
1、命令行执行:locust -f my_locust.py
2、打开http://localhost:8089/。
3、用户数,孵化率,host分别输入1,1,http://127.0.0.1:7890
4、点击运行

服务端:可以看到在点击运行后,服务端接收到了on_start发起了登录的请求。

客户端:接收到了登录成功的响应。

locust-ui点击停止运行

服务端:可以看到在点击结束运行后,服务端接收到了on_stop发起了退出登录的请求。

客户端:接收服务端退出登录的响应。

on_start和on_stop:在单次前后,每次运行有且仅有运行1次。

以上,即on_start和on_stop的解析和案例说明。

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

locust2.0+教程:005 - on_start和on_stop相关推荐

  1. locust2.0+教程:007 - 分布式压测执行

    前言: locust2.0+教程:001 - 安装和目录结构 locust2.0+教程:002 - 快速使用 locust2.0+教程:003 - headless无界面模式 locust2.0+教程 ...

  2. OpenGL3.0教程 第十六课:阴影贴图

    OpenGL3.0教程 原文链接:http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-12-opengl-extensions ...

  3. 零基础快速入门SpringBoot2.0教程 (二)

    一.SpringBoot2.x使用Dev-tool热部署简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用官方地址:https://docs.spring.io ...

  4. Android Studio2.0 教程从入门到精通Windows版

    系列教程 Android Studio2.0 教程从入门到精通Windows版 - 安装篇 Android Studio2.0 教程从入门到精通Windows版 - 入门篇 Android Studi ...

  5. SpringBoot 2.0 系列005 --启动实战之SpringApplication应用

    为什么80%的码农都做不了架构师?>>>    SpringBoot 2.0 系列005 --启动实战之SpringApplication应用 2.X 官方示例 注意是只使用了@En ...

  6. IoT:BLE4.0教程一 蓝牙协议连接过程与广播分析

    IoT:BLE4.0教程一 蓝牙协议连接过程与广播分析 1.蓝牙简介 什么是蓝牙4.0 蓝牙无线技术是使用范围最广泛的全球短距离无线标准之一,蓝牙4.0版本涵盖了三种蓝牙技术,即传统蓝牙.高速蓝牙和低 ...

  7. iOS OpenGL ES2.0教程   Lesson03 旋转

    iOS OpenGL ES2.0教程    Lesson03  旋转 注:可供翻译的课程只有前两课.从这节课起,我试着根据我对OpenGL ES的理解写接下去的课程,希望能和大家一起学习. 在上节课中 ...

  8. TensorFlow2.0教程-使用keras训练模型

    TensorFlow2.0教程-使用keras训练模型 Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/article/details ...

  9. 001-TensorFlow 2.0 教程-Transformer

    TensorFlow 2.0 教程-Transformer 原文地址:https://blog.csdn.net/qq_31456593/article/details/89923913 Tensor ...

最新文章

  1. this.options[selectedIndex]的使用
  2. JQuery $post函数
  3. aes算法的C语言实现代码,AES加密算法c语言实现代码
  4. Zookeeper知识点详解
  5. 鱼不长肢蛇不长脚,多多益善更烦恼?人猴嵌合肥胖有救,生活还要压力要小!...
  6. Android 中的BroadCastReceiver【转】
  7. Monocular slam 的理论基础(1)
  8. 第七章 线程的调度、优先级和亲缘性(4)
  9. 睡前1分钟 坚持瘦下来(信不信由你)
  10. Safari 搜寻引擎被 Yahoo 绑架,试试这 4 种解决方法
  11. sql2005 脚本中给字段写说明
  12. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platf...
  13. 轻松修复iOS系统的工具:iToolab FixGo mac中文版
  14. 魅族17用鸿蒙系统,发布一年还很香,魅族17升级Flyme 9后太好用了
  15. 听趣拍云产品经理剖析视频基础知识
  16. PDF图纸转换CAD图纸怎样操作呢?
  17. Unity场景中的双相机设置,3D相机和2D相机设置
  18. installshield打包mysql_实现Installshield对Php+mysql+apache做的程序在WINDOWS下打包
  19. 【蓝桥杯】 算法训练 数字三角形
  20. Oracle JDE 供需包含规则

热门文章

  1. java正则匹配换行_正则表达式怎么匹配换行符
  2. xml文件Unable to resolve table ‘xxx‘解决
  3. 广东计算机水平测试报名,广东计算机等级考试报名时间
  4. iBATIS框架理论学习篇(公司内部培训文档)
  5. 前端转golang从小白到实战自学笔记(2023/3/1)
  6. Excel清除隐藏的引号或空格
  7. OpenCV快速寻找图像差异
  8. 三、SIGNALING PACKET FORMATS
  9. 电子产品满载高低温老化试验方法
  10. 凯云科技——高实时仿真平台ETest_Sim