zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    python 迭代器&&生成器
    windows 10 扩大C盘空间
    robot framework 接口自动化测试和关键字开发
    Robot framework 环境搭建+图标处理
    Docker 安装-在centos7下安装Docker(二)
    win10系统rational rose 安装后打开弹框显示java.lang.ClassNotFoundException 解决方案
    mysql 关系表 分组读取的方法
    关于浮点型计算遇到的小问题
    dom 的介绍
    网站前端相关的知识点
  • 原文地址:https://www.cnblogs.com/monogem/p/11367943.html
Copyright © 2011-2022 走看看