1. import os
    2. import secrets
    3. import json
    4. import numpy as np
    5. import pandas as pd
    6. import random
    7. from locust import HttpUser, TaskSet, task
    8. class TieniuAPITest(TaskSet):
    9. @task(1)
    10. def test_request(self):
    11. # post_url = "http://test.rasa.com:5005/webhooks/bot/webhook"
    12. post_url = "/tieniuapi"
    13. data = {
    14. "callId": "zzzz" + "-" + str(np.random.random_integers(0, 1000)),
    15. "robotCode": "zzzz",
    16. "sceneCode": "zzzz",
    17. "fromUser": "zzzz",
    18. "data": {
    19. "name": "zzzz",
    20. "gender": "zzzz",
    21. "steps": 1,
    22. "default_amount": "zzzz",
    23. "default_days": "zzzz",
    24. "agent_name": "zzzz",
    25. "due_day": "zzzz",
    26. "num_cases": "zzzz"
    27. },
    28. "question": random.choice(questions),
    29. "sendTime": "202109081625300214",
    30. "answerResult": {}
    31. }
    32. data = json.dumps(data, ensure_ascii=False).encode(encoding="utf-8")
    33. r = self.client.post(post_url, data=data)
    34. r = json.loads(r.text)
    35. class WebsiteUser(HttpUser):
    36. tasks = [TieniuAPITest]
    37. min_wait = 10
    38. max_wait = 17000
    39. sender = secrets.token_urlsafe(16)
    40. # 问题测试数据
    41. questions = pd.read_csv("./test.csv")["user_part"].tolist()

    运行脚本:

    1. $ locaust -f rasa_bot.py --host=http://localhost:5005

    测试结果:

    • Number of users to simulate:设置模拟的用户总数
    • Hatch rate (users spawned/second):每秒启动的虚拟用户数

    image.png

    image.png

    image.png