Locust简单的一些性能脚本编写

#coding:utf-8
from locust import task,TaskSet,HttpLocust
import os
import queue
import pymysql
import requests
import json

class OrderUser(TaskSet):

    @task(1)  #中文支持
    def addOrder(self): #OrderUser类下面可以有多个方法(就是执行的场景)@task(1) @task(2)可以看做分配的用户比
        path = "/order/add_order.json"
        sku_list = self.locust.sku_list.get()

        header = {
            "source": "IOS",
        }

        data = {"goodsList":[{"price":sku_list["price"],"goodsSkuId":sku_list["id"],"cartGoodsId":""}],"shopkeeperFlag":2,"payFlag":True}
        response = self.client.post(url = path,headers=header,json = data ,verify = False, catch_response=True)
        if response.status_code == 200:
            json_data = response.json()
            if json_data["resultCode"] == 1:
                print("接口请求成功")
                response.success()
            else:
                body_data = json.dumps(data,ensure_ascii=False)
                print("请求失败")
                print("失败的请求头:%s"%header)
                print("失败的请求体:%s"%body_data)
                print("返回的结果:%s"%json_data)
                response.failure("接口请求失败")
        else:
            print("状态错误")
        self.locust.sku_list.put(sku_list)

class OrderRun(HttpLocust):
    task_set = OrderUser
    min_wait = 1000
    max_wait = 2000
    host = "http://ip:8080"

    #创建一个队列
    sku_list = queue.Queue()

    #连接数据库获取数据库数据把数据放到创建的队列sku_list里面
    con = pymysql.connect("ip", "name", "password", "数据库名")
    cur = con.cursor()
    cur.execute("SELECT id FROM mall_goods WHERE status=3 ORDER BY create_time DESC LIMIT 50")
    goods_data = cur.fetchall()
    for goods_id in goods_data:

        cur.execute("SELECT id FROM  mall_goods_sku WHERE goods_id = %s LIMIT 1" % goods_id[0])
        sku_id = cur.fetchone()
        urls = "http://ip:8080/goods/v1/get_newest_skus.json"
        header = {
            "customerId": "1078221126401851392"
        }
        params = {
            "skuIdList": "%s" % sku_id[0]
        }
        response = requests.get(url=urls, headers=header, params=params)
        response_json = response.json()
        try:
            if response_json.get("data") is None:
                continue
            if response_json.get("data")[0].get("actTypeId") is None:
                price = response_json.get("data")[0].get("goodsPrice")
            else:
                price = response_json.get("data")[0].get("goodsPrice")

            sku_dict = {
                "id": "%s" % sku_id[0],
                "price": price
            }
            sku_list.put(sku_dict)
        except Exception as e:
            print("这个goodid:%s有问题" % goods_id[0])
            print(e)
        response.close()
    con.close()
if __name__ == '__main__':
    f = os.popen("locust -f Order.py OrderRun")
    print(f.read())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值