#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())
Locust简单的一些性能脚本编写
最新推荐文章于 2024-04-22 18:04:00 发布