利用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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章