ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

locust2.0+教程:005 - on_start和on_stop

2021-09-03 17:04:28  阅读:219  来源: 互联网

标签:stop request datetime start json 005 time data def


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

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

locust脚本源码:my_locust.py

from locust import HttpUser, task, between
import logging


class QuickstartUser(HttpUser):
    wait_time = between(1, 2)  # 为方便运行,缩短了一点点时间。

    @task
    def 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"}).text
        logging.info(f"login_result:{login_result}")

    def on_stop(self):
        logout_result = self.client.post("/logout", json={"username": "Jim", "password": "456789"}).text
        logging.info(f"logout_result:{logout_result}")

服务端sanic源码:main.py

from sanic import Sanic
import datetime
from sanic import response

app = 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.json
    print(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.json
    print(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的解析和案例说明。

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

标签:stop,request,datetime,start,json,005,time,data,def
来源: https://blog.csdn.net/hzblucky1314/article/details/120085654

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有