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
  • 相关阅读:
    12、闭包函数、装饰器、迭代器
    11、函数对象、函数的嵌套、名称空间与作用域
    10、初识函数
    9、文件操作
    8、字符编码-Python(转)
    7、str字符串、int整数、list列表、dict字典、set集合、tuple元祖功能详解
    python day11 学习整理
    python day9 学习整理
    python day8 学习整理
    python day7 学习整理
  • 原文地址:https://www.cnblogs.com/monogem/p/11367943.html
Copyright © 2011-2022 走看看