zoukankan      html  css  js  c++  java
  • locust做并发测试实战

    一、安装

     1.安装 python3

      locust是基于 python3的,根据你的系统(mac、window、Linux)安装就行。

      mac参考 mac安装python3

      验证 :python --version

     2.安装locust

      locust是python3的一个外部库

      命令:pip3 install locust

      验证:locust -V

    二、使用

     1.编写python脚本

     给两个示例:

     get请求

    from locust import task, between
    from locust.contrib.fasthttp import FastHttpUser, FastResponse
    
    
    class MinApp(FastHttpUser):
    
        wait_time = between(1, 2)
        host = "https://localhost:10001"
    
        '''
        1.banner
          - get请求无参数
        '''
        @task
        def sel_banners(self):
            response = self.client.get("/banners/list")
            if response.status_code != 200:
                print("error content:", response.text)
    
        '''
        2.查询商品
          -get请求,问号传参数
        '''
        @task(2)
        def sel_item(self):
    
            list1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    
            for itemId in list1:
                response = self.client.get(f"/items/findItem?itemId={itemId}")
            if response.status_code != 200:
                print("error content:", response.text)
            else:
                print(response.text)

    post请求,body传参数

    from locust import task, between
    from locust.contrib.fasthttp import FastHttpUser, FastResponse
    
    
    class ShellCard(FastHttpUser):
    
        wait_time = between(1, 5)
        host = "http://localhost:8201/shelldataservice/openApi/service"'''
        1.查询卡余额     
        '''
    
        @task(2)
        def sel_balance(self):
    
            header = {
                "Content-Type": "application/json",
                "clientId": "gysitzes2h5Dc",
                "sign": "DE34DF43138E541FAD8BB9D7F7002139"
            }
    
            payload = {
                "cardCode": "733637631082057728",
                "panCode": "82057728"
            }
            response = self.client.post("/selBalance", data=None, json=payload, headers=header)
            if response.status_code != 200:
                print("error content:", response.text)

     2.运行脚本

      locust -f minApp.py

      注:通过参数 -f 指定脚本文件路径

     3.访问 locust UI界面

       地址:localhost:8089

     4.模拟用户访问

       

    charts:

    statistics:

    三、总结

     1.Locust是一款易于使用的分布式负载测试工具,完全基于事件,即一个locust节点也可以在一个进程中支持数千并发用户;

      不需要编写笨重的UI或者臃肿的XML代码,基于协程而不是回调,脚本编写简单易读

     2.更多信息参考官网 : locust官网

    stay hungry stay foolish!
  • 相关阅读:
    爱国论
    windows cmd: 打开windows系统程序或服务的常见命令
    Windows最常用的几个网络CMD命令总结
    什么是DNS劫持和DNS污染?
    windows cmd: 增强windows命令行
    NAT
    电信光纤猫 f412超级密码
    jvm之java类加载机制和类加载器(ClassLoader)的详解
    经典中的经典算法 动态规划(详细解释,从入门到实践,逐步讲解)
    首次适应算法、最佳适应算法和最差适应算法
  • 原文地址:https://www.cnblogs.com/shog808/p/13563515.html
Copyright © 2011-2022 走看看