利用locustio进行关联接口的性能测试

locustio通常用来做单接口压测,脚本编写成本低,并发度高。新版locustio对补充了关联接口的测试场景,利用seq_task可以较为快速的定义执行的先后顺序,同时利用全局的queue,实现参数的传递。
本例模拟停车入场和出场的场景,按照业务逻辑,停车入场后生成一个订单号order_id,出场时会根据order_id计算停车时长,并产生费用。parkin成功生成order_id,并放入data_queue中,parkout从data_queue取出order_id,并作为出场参数调用出场接口。

import datetime 
import queue 
import random 
from locust import HttpLocust, TaskSet, task,seq_task 
import time 

class MyTaskSet(TaskSet): 
    @seq_task(1) 
    def parkin(self): 
        i=random.randint(1,999999) 
        #depotcode=["znpark0000000001","znpark0000000003","znpark0000000014","znpark0000000015","znpark0000000016","znpark0000000020","znpark0000000021","znpark0000000022","znpark0000000023"] 
        platenos=["川A4D628","川A4F47A","川A4H58B","川A4MP63"] 
        #param = ["znpark00000000%s" % str(random.randint(1, 18)).zfill(2), "B%s" % str(i).zfill(6)]   
        depot_id="znpark0000000"+str(random.randint(100,499)) 
        #platno="B%s" % str(i).zfill(6) 
        platno=platenos[random.randint(0,len(platenos)-1)] 
        data='''[{ 
                    "privateInfo": 
                    { 
                        "depot_id":"%s", 
                        "start_time":"2018-10-21 20:25:22", 
                        "plate_number":"%s", 
                        "car_type":"1" 
                    } 
                }]'''%(depot_id,platno) 

        with self.client.post("http://api:9080/service/cocoa/json/com.service.eparking.dubbo.service.DeviceParkInService/1.0/parkIn", data.encode("utf-8"),name="DeviceParkInService",catch_response = True) as resp: 
            try: 
                if resp.json().get("result") in ("0"): 
                    resp.success() 
                    orderid=resp.json().get("data").get("order_id") 
                    paramout=[depot_id,platno,orderid] 
                    self.locust.data_queue.put_nowait(paramout) 
                else: 
                    resp.failure(resp.json()) 
                    print(data,depot_id,platno) 
            except Exception as e: 
                resp.failure(e) 

    @seq_task(2) 
    def parkout(self): 
        try: 
            paramin = self.locust.data_queue.get_nowait() 
            depot_id=paramin[0] 
            platno=paramin[1] 
            orderid=paramin[2] 
            dataout='''[{ 
                        "privateInfo": 
                        { 
                            "depot_id":"%s", 
                            "start_time":"2018-10-25 19:25:22", 
                            "end_time":"2018-10-25 20:23:22", 
                            "mobile":"123", 
                            "plate_number":"%s", 
                            "order_id":"%s", 
                            "depot_mid":"272", 
                            "is_out":"2", 
                            "actual_pay":"0.01" 
                        } 
                    }]'''%(depot_id,platno,orderid) 
            with self.client.post( 
                    "http://api:9080/service/cocoa/json/com.service.eparking.dubbo.service.DeviceParkOutService/1.0/parkOut", 
                    dataout.encode("utf-8"), name="DeviceParkOutService", catch_response=True) as respout: 
                try: 
                    if respout.json().get("result") in ("0"): 
                        respout.success() 
                    else: 
                        print("sendout:", dataout) 
                        print("respout:", respout.json()) 
                        respout.failure(respout.json().get("errmsg")) 
                except Exception as e: 
                    print("sendout:", dataout) 
                    print(e) 
                    respout.failure(respout.json()) 
        except queue.Empty: 
            #print ('data run out, test end') 
            pass 

class MyLocust(HttpLocust): 
    task_set = MyTaskSet 
    min_wait = 0 
    max_wait = 0 
    host = "eparking" 
    data_queue = queue.Queue()
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页