zoukankan      html  css  js  c++  java
  • Losust性能脚本demo

      我们实际工作中接口测试经常会遇到这样的问题,一个接口的响应值作为另一个接口的请求值,而且处于动态变化中,这样就需要我们编写方法将变量提取出来引用了,否则复制、粘贴将是一个很无聊的过程。性能测试则更是无法运行,所以,提取、引用参数是我们必须要会的点。

      最近在学习locust性能压测,结合实际工作编写了本demo,实现获取登录tocken值,拼接后作为请求头参数参与其他接口请求具体代码如下:

     1 from locust import HttpUser, task
     2 import json
     3 
     4 
     5 class SearchTest(HttpUser):
     6 
     7     def on_start(self):
     8         pass
     9 
    10     def get_authorization(self):
    11         """
    12         执行login
    13         :return:access_token
    14         """
    15         login_interface = "/auth/login"
    16         post_data = {"username": "trader1", "password": "admin123"}
    17         request_headers = {"Content-Type": "application/json;charset=UTF-8"}
    18         with self.client.post(url=login_interface, headers=request_headers, json=post_data,
    19                               catch_response=True, name="登录") as response:
    20             if json.loads(response.text).get("code") == 200:
    21                 print("---------------登录成功--------------")
    22                 print("response code:" + str(json.loads(response.text).get("code")))
    23                 access_token = json.loads(response.text).get("data")["access_token"]
    24                 authorization = "Bearer " + access_token
    25                 return authorization
    26             else:
    27                 print("---------------登录失败--------------")
    28                 print("response code:" + str(json.loads(response.text).get("code")))
    29                 print("response_massage:" + str(json.loads(response.text).get("msg")))
    30 
    31     def on_stop(self):
    32         """
    33         执行logout
    34         :return: response
    35         """
    36         logout_interface = "/auth/logout"
    37         with self.client.delete(url=logout_interface, name="登出") as response:
    38             if json.loads(response.text).get("code") == 200:
    39                 print("---------------登出成功--------------")
    40                 print("response code:" + str(json.loads(response.text).get("code")))
    41                 print(response.text)
    42             else:
    43                 print("---------------登出失败--------------")
    44                 print("response code:" + str(json.loads(response.text).get("code")))
    45                 print("response_massage:" + str(json.loads(response.text).get("msg")))
    46 
    47     @task(1)
    48     def search_interface(self):
    49         """
    50         任务一:查询接口,GET方法
    51         :return: 查询接口性能压测数据
    52         """
    53         get_url = "/system/entry/37"
    54         authorization = self.get_authorization()
    55         print("认证标识 Authorization:" + authorization)
    56         request_headers = {"Authorization": authorization,
    57                            "Content-Type": "application/json;charset=UTF-8"}
    58         with self.client.get(url=get_url, headers=request_headers, catch_response=True, name="入库单查询") as response:
    59             if json.loads(response.text).get("code") == 200:
    60                 print("---------------查询成功--------------")
    61                 print("response code:" + str(json.loads(response.text).get("code")))
    62             else:
    63                 print("---------------查询失败--------------")
    64                 print("response code:" + str(json.loads(response.text).get("code")))
    65                 print("response_massage:" + str(json.loads(response.text).get("msg")))
    66 
    67     @task(1)
    68     def create_form(self):
    69         """
    70         任务二
    71         :return:
    72         """
    73         pass

    运行服务,打开服务地址,录入参数,点击“start swarming”开始测试(具体参数含义及配置请参考我之前的locust基础篇):

    以下是执行结果:

    以上,希望对大家能有所帮助。

  • 相关阅读:
    authentication与网站安全验证
    Android 不一样的原生分享
    iOS学习笔记——AutoLayout的约束
    积累一下SQL
    关于数据链接底层
    减少生成的dll数量
    iOS学习笔记——使用ChildViewController
    iOS学习笔记——键盘处理
    iOS学习笔记——触控与手势
    iOS学习笔记——多控制器管理
  • 原文地址:https://www.cnblogs.com/marvintester/p/14606993.html
Copyright © 2011-2022 走看看