python code is following ,file name is my.py;
#!./venv/bin/python
# Author: Adam
# Data : 2024-02-28
from locust import User, task, between, constant
import asyncio, websockets
import requests, time, struct, json, random
from my_stress_package import LogIn_pb2, MyEvent_pb2, MyQuest_pb2, Quote_pb2, UserInfo_pb2
from my_stress_package.Mybase import log_writer
Logger = log_writer()
exception_status = {
"8888": "Heartbeat", "8889": "Quotes", "8890": "Quotes Subscribe", "8885": "Disable Login",
"8886": "Login Failed", "8887": "Muitple Login", "9998": "Market Closed", "9999": "Login Successed", "10000": "Login",
"10001": "Products Search", "10002": "Products info", "10003": "Products Smary Search", "10004": "Products Smary Info",
"10032": "Buy Successed", "10034": "Buy Limit successed", "l0036": "Sell successed", "10038": "Sell Limit successed"
}
class MyLocustTrade(User):
# INIT
async def on_start(self):
self.wscon = await websockets.connect("ws://192.168.130.100:9000/api/test/auth/websocks/my5SockBin", timeout=5)
async def on_stop(self):
if self.wscon:
await self.wscon.close()
# LOGIN_DATA TO BINARY PACKAGE
def my_login_data(account, pwd, device):
header_code = 10000
int_part = struct.pack('!i', header_code)
login_pb_data = LogIn_pb2.LogIn()
login_pb_data.login = account
login_pb_data.pwd = pwd
login_pb_data.device = device
pb_serialized_data = login_pb_data.SerializeToString()
data = int_part + pb_serialized_data
return data
# TRADE_DATA TO BINARY PACKAGE
def my_trade_data(data):
header_code = 10031
int_part = struct.pack('!i', header_code)
trade_pb_data = MyQuest_pb2.NewOrder()
trade_pb_data.sbl = data["sbl"]
trade_pb_data.act= data["act"]
trade_pb_data.type= data["type"]
trade_pb_data.price= data["price"]
trade_pb_data.vol= data["vol"]
trade_pb_data.sl= data["sl"]
trade_pb_data.tp= data["tp"]
pb_serialized_data = trade_pb_data.SerializeToString()
data = int_part + pb_serialized_data
return data
# TRADE_DATA SEND
async def my_data_send(self, data):
await self.wscon.send(data, timeout=5)
# TRADE_DATA RECEIVE
async def my_data_receive(self):
data = await self.wscon.recv(timeout=5)
login_rep_code = data[0:4]
login_rep_data = data[4:]
rep_code_str = struct.unpack('!i', login_rep_code)[0]
return [rep_code_str, login_rep_data]
wait_time = constant(300 )
@task
async def my_locust_task(self):
# THIS IS A TRUE SINGLE DATA
account = 3101
pwd = "12345678"
device = random.choice([1,2,3,4])
buy_data_demo = {"sbl": "DOGEUSDT", "act": 200, "type": 0 ,"price": 0.5, "vol": 100, "sl": 0.01, "tp": 1.99}
login_data = self.my_login_data(account, pwd, device)
trade_data = self.my_trade_data(buy_data_demo)
# HERE SEND LOGIN DATA AND RECEIVE
await self.my_data_send(login_data)
recv_binary2str = await self.my_data_receive()
# ACTUALLY THE RETURN DATA IS A LIST IT'S INCLUDE 2 ELEMENTS,THE PART-1 IS A INT-STATUS-CODE AND DATA IS PART-2;
rep_code, rep_data = recv_binary2str
if rep_code == 9999:
# HERE WE USE PROTO-BUFF-FILE PARSE THE PART-2 DATA
userinfo_pb_data = UserInfo_pb2.UserInfo
login_rep_data_str = userinfo_pb_data.ParseFromString(rep_data)
print(login_rep_data_str)
# SEND AND RECEIVE TRADE DATA
self.my_data_send(trade_data)
rep_code, rep_data = recv_binary2str
#
if rep_code in [10032, 10034, 10036, 10038]:
Quest_pb_data = MyQuest_pb2.Response
rep_data_str = Quest_pb_data.ParseFromString(rep_data)
Logger.info(f"登录返回 {rep_data_str}")
print(rep_data_str)
return rep_data_str
else :
status = exception_status[str(rep_code)]
return {"status": status}
when i run "locust -f my.py --headless -u 1 -r 1 --run-time 5"
How can i fix it ?
My confusion:
0. i used "websockets" and "asyncio" modules in Locust as loading test the websocket-api with python, am i use a wrong-solution ? if it is what is the better solution ? if not go ahead the following.
1. am i not clear to understand the Locust-calss-usage ?
2. am i not clear to understand the websockests-usage ?
3. am i not clear to understand the asyncio-usage in python?
