Python locust性能测试框架模板

本文详细介绍使用Locust框架进行性能测试的过程,包括虚拟用户行为定义、登录流程处理、获取关键参数、设备列表请求及响应状态码检查。通过具体代码示例,展示了如何设置HTTP请求头、构造JSON数据并发送POST请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

locust框架模板

from locust import HttpLocust, TaskSet, task
import Queue


class UserBehavior(TaskSet):

    def setup(self):
        print('task setup')

    def teardown(self):
        print('task teardown')

    def on_start(self):

        # key = self.login()
        request_url = "/api/user/getkey"
        request_json = {"username": "qxzn", "userpwd": "qx6688"}

        response = self.client.post(url=request_url, json=request_json)
        # print response.status_code
        # print response.text
        if response.status_code != 200:
            print u"返回异常,请求返回状态码:", response.status_code
        elif response.status_code == 200:
            print u"返回正常:", response.status_code
        global key_1, key_2
        key_1 = response.text.split('''appkey":"''')[1].split('"')[0]
        key_2 = response.text.split('''infokey":"''')[1].split('"')[0]
        print key_1 + '-' + key_2
        data = self.locust.user_data_queu
        print data

    def on_stop(self):
        # 虚拟用户结束Task时运行
        print('end')

    @task(1)
    def resList(self):

        # key = self.login()
        header = {'Accept': 'application/json, text/plain, */*',
                  'Accept-Encoding': 'gzip, deflate',
                  'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
                  'Connection': 'keep-alive',
                  'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0',
                  'Content-Type': 'application/json;charset=utf-8',
                  'Authorization': key_1 + '-' + key_2}
        data = {"num": 15, "current_page": 1, "province": "", "city": "", "type": ""}
        request_url = "/api/desk_device/devicelist"

        response = self.client.post(url=request_url, headers=header, json=data)
        if response.status_code != 200:
            print u"返回异常,请求返回状态码:", response.status_code
        elif response.status_code == 200:
            print u"返回正常", response.status_code


class WebsiteUser(HttpLocust):

    def setup(self):
        print('locust setup')

    def teardown(self):
        print('locust teardown')

    user_data_queue = Queue.Queue()
    task_set = UserBehavior
    host = "http://xxxx.com:8095"
    min_wait = 5000
    max_wait = 8000

 

转载于:https://www.cnblogs.com/monogem/p/11367943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值