import os
import secrets
import json
import numpy as np
import pandas as pd
import random
from locust import HttpUser, TaskSet, task
class TieniuAPITest(TaskSet):
@task(1)
def test_request(self):
# post_url = "http://test.rasa.com:5005/webhooks/bot/webhook"
post_url = "/tieniuapi"
data = {
"callId": "zzzz" + "-" + str(np.random.random_integers(0, 1000)),
"robotCode": "zzzz",
"sceneCode": "zzzz",
"fromUser": "zzzz",
"data": {
"name": "zzzz",
"gender": "zzzz",
"steps": 1,
"default_amount": "zzzz",
"default_days": "zzzz",
"agent_name": "zzzz",
"due_day": "zzzz",
"num_cases": "zzzz"
},
"question": random.choice(questions),
"sendTime": "202109081625300214",
"answerResult": {}
}
data = json.dumps(data, ensure_ascii=False).encode(encoding="utf-8")
r = self.client.post(post_url, data=data)
r = json.loads(r.text)
class WebsiteUser(HttpUser):
tasks = [TieniuAPITest]
min_wait = 10
max_wait = 17000
sender = secrets.token_urlsafe(16)
# 问题测试数据
questions = pd.read_csv("./test.csv")["user_part"].tolist()
运行脚本:
$ locaust -f rasa_bot.py --host=http://localhost:5005
测试结果:
- Number of users to simulate:设置模拟的用户总数
- Hatch rate (users spawned/second):每秒启动的虚拟用户数