Python locust性能测试框架模板

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

时间: 2024-11-10 17:54:58

Python locust性能测试框架模板的相关文章

Python Locust性能测试框架实践

Locust的介绍 Locust是一个python的性能测试工具,你可以通过写python脚本的方式来对web接口进行负载测试. Locust的安装 首先你要安装python2.6以上版本,而且有pip工具.之后打开命令行,分别安装locustio和pyzmq(命令如下): pip install locustio pip install pyzmq 之后我们就可以写性能测试脚本了. Locust脚本编写 接下来我们拿两个接口做一下测试,编写脚本如下(每一步都有注释).我来解释一下,首先我们要i

python locust 性能测试:locust 关联---提取返回数据并使用

from locust import HttpLocust, TaskSet, taskimport jsonfrom common import readConfig class UserBehavior(TaskSet): # 定义用户行为 def on_start(self): # 当模拟用户开始执行TaskSet类的时候,on_start方法会被调用 pass def get_headers(self): """会员登录""" heade

python locust 性能测试:locust 参数化(list) ---循环取数据,数据可重复使用

from locust import TaskSet, task, HttpLocust class UserBehavior(TaskSet): def on_start(self): # 当模拟用户开始执行TaskSet类的时候,on_start方法会被调用 self.index = 0 @task def test_visit(self): url = self.locust.share_data[ self.index] # 取 self.locust.share_data<等于 Web

python locust 性能测试:locust参数-保证并发测试数据唯一性,循环取数据

from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test_register(self): try: # get_nowait() 取不到数据直接崩溃:get() 取不到数据会一直等待 data = self.locust.user_data_queue.get_nowait() # 取值顺序 'username': 'test0000'.'usernam

python locust 性能测试:locsut参数化-保证并发测试数据唯一性,不循环取数据

from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test_register(self): try: # get_nowait() 取不到数据直接崩溃:get() 取不到数据会一直等待 data = self.locust.user_data_queue.get_nowait() # 取值顺序 'username': 'test0000'.'usernam

python locust 性能测试:HttpSession

官网解释:用于在请求之间执行Web请求和保留(会话)cookie的类(以便能够登录和退出网站):记录每个请求,以便locust可以显示统计信息: from locust import TaskSet, task, HttpLocust, Locust from locust.clients import HttpSession import subprocess import random from common.sec_encrypt import AESEncrypt, md5_time f

python locust 性能测试:嵌套

TaskSet类和TaskSequence类可用于嵌套<可以在TaskSequences中嵌套TaskSets,反之亦然>: from locust import TaskSet, task, HttpLocust, TaskSequence, seq_task import subprocess class WebUser(TaskSet): @task(5) def first_task(self): print('执行5次:') @task(2) class IosUser(TaskSe

python locust 性能测试:HOOKS&lt;钩子方法&gt;

为locust中不同类型的事件,提供的钩子方法: from locust import TaskSet, task, events, Locust from locust.clients import HttpSession import subprocess import random, traceback from common.sec_encrypt import AESEncrypt, md5_time from common import read_config from common

python使用django框架模板的基本使用

定义:模板是html页面,可以根据师徒中传递过来的数据进行填充 创建模板:在sunck的下一个project下创建目录templates,再在此目录下创建myapp. 配置路径:修改setting.py文件的TEMPLATES下的DIRS,方括号中添加:os.path.join(BASE_DIR,'templates') 在templates的myapp下面,新建定义grades.html和student.html模板. grades.html:<!DOCTYPE html><html