ces

197 阅读2分钟

locustfile.py

# 导入相关的locust的类、成员
from locust import HttpUser, task, between
import logging
import json
from read_data import ReadData
from read_excel import ReadExcel

# 定义一个用户类
class MyUser(HttpUser):

    @task
    def hello_post(self):
        # 请求头
        self.headers = {"Content-Type": "application/json"}
        url="/v1/ds/mb/jrhx/get?_consumer=py"
        # test_data=ReadExcel.get_data("test_data.xlsx")
        test_data=ReadData.get_data("data.csv")
        for i in range(10):
            payload= json.dumps(test_data[i])
            response = self.client.post(url=url, data=payload, headers=self.headers, catch_response=True)
            print(payload)
            print("Response status code:", response.status_code)
            print("Response text:", response.text)
        wait_time = between(2, 5)

read_data.py

import csv
import json

class ReadData:

    @classmethod
    def get_data(cls,file_name):
        with open(file_name,'r') as f:
            reader = csv.reader(f)
            head = next(reader)
            test_data = []
            for row in reader:
                row_data = {"iidType":row[0],"iexid": row[1],"itemplateCode":row[2]}
                test_data.append(row_data)
        return test_data


if __name__ == '__main__':
    test_data=ReadData.get_data("data.csv")
    print(test_data)
    for i in range(10):
        payload = json.dumps(test_data[i])
        print(type(payload))

test_data.csv

iidType,iexid,itemplateCode
010105,13942733443,578949157576310784
010105,13942206400,578949157576310784
010105,13941442491,578949157576310784
010105,13940220280,578949157576310784
010105,13943197313,578949157576310784
010105,13945843807,578949157576310784
010105,13949134272,578949157576310784
010105,13940978229,578949157576310784
010105,13944604276,578949157576310784
010105,13944233948,578949157576310784