Python locust基本使用

脚本


from locust import HttpUser, SequentialTaskSet, task, between
import time, urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class MyTaskWebLive(SequentialTaskSet):
    # 当类里面的任务请求有先后顺序时继承SequentialTaskSet类;
    # 没有先后顺序,可以使用继承TaskSet类
    
    # 初始化方法,相当于 setup
    def on_start(self, host_hellotalk="https://127.0.0.1", room_id="874128777"):
        self.host_hellotalk = host_hellotalk
        self.room_id = room_id
        self.headers = {
            'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDgyMDUxMzAsInVpZCI6MTAyODMyMzN9.gl9sYd1oS6p8i7EeC7ZRtCTM5HmpZZ82M8bf7zcRZ7s',
            'Content-type': 'application/json'
        }
        
    # @task python中的装饰器,告诉下面的方法是一个任务
    # 要用这个装饰器,需要头部引入 从locust中,引入 task
    @task
    def purchase_live_virtual_product(self):
        '''直播礼物接口'''
        url = "https://127.0.0.1/virtual_pay/v1/purchase_live_virtual_product"
        payload = {
            "user_id": 10283233,
            "to_user_ids": [10283238],
        }
        res = self.client.request(method="POST", url=url, json=payload, headers=self.headers, verify=False)


# 定义一个运行类 继承HttpUser类, 所以要从locust中引入 HttpUser类
class MyUser(HttpUser):
    tasks = [MyTaskWebLive]  # 指定用户运行的任务类
    # 设置运行过程中间隔时间 需要从locust中 引入 between
    wait_time = between(1, 3)  # 等待时间

 

运行

  • cmd运行:locust -f WebLive.py --host=http://localhost:8089
  • 打开页面后填写:
posted @ 2022-03-24 00:08  青山原  阅读(149)  评论(0编辑  收藏  举报