locust websocket 压测

1、首先建立一个websocket服务端

class LocustWebSocketClient(Locust):
    def __init__(self, ws_url='per.zldda.cn', callback=None):
        super().__init__()
        self.ws = websocket.create_connection("wss://%s/wsapi" % ws_url, timeout=10)
        self.ws_url = ws_url
        self.callback = callback

    def web_socket_request(self, dictmsg):
        resp = None
        total_time = None
        try:
            start_time = time.time()
            print('send:', dictmsg)
            self.ws.send(simplejson.dumps(dictmsg))
            result = self.ws.recv()
            resp = simplejson.loads(result)
            print("recv:", resp)
            total_time = int((time.time() - start_time) * 1000)
            if resp['code'] == (1001 or 1000 or 1002):
                pass
            else:
                print('error send:', dictmsg, 11111)
                events.request_failure.fire(
                    request_type='wss',
                    name='web_socket_request',
                    response_time=total_time,
                    exception=resp
                )
            events.request_success.fire(
                request_type='wss',
                name='web_socket_request',
                response_time=total_time,
                response_length=len(resp)
            )
            if self.callback is None:
                return resp
            else:
                return self.ws
        except Exception as e:
            print(e)
            events.request_failure.fire(
                request_type='wss',
                name='web_socket_request',
                response_time=total_time,
                exception=resp
            )

然后就开始写呗

class UserBehavior(TaskSet):
    def on_start(self):
        self.user_id_list = []
        self.all_sid = get_sid()
        self.url = 'perfor.zldhz.com'
        all_worker_info = worker_contract_list(get_ws_driver(ws_url='perfor.zldhz.com'), self.all_sid[0][0], flag=1,
                                               limit='15', page=1, project_id=self.all_sid[0][1], request_status='完成',
                                               status=0, with_charkt_value=True)
        for worker_info in all_worker_info['data']['worker_contract']:
            self.user_id_list.append(worker_info['id'])
        print(self.user_id_list)
        

    @task
    def test_project1(self):
        ws = LocustWebSocketClient(self.url)
        project_list(ws_driver=ws, sid=self.all_sid[0][0])
        ws.ws.close()
class WebUserLocust(LocustWebSocketClient):
    weight = 1
    task_set = UserBehavior
    min_wait = 3000
    max_wait = 7000

然后 locust -f xxx.py --no-web -c1 -r1 -t100

分布

主:

locust -f locust_test.py --master

locust -f locust_test.py --slave --master-host=192.168.22.62

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值