Python locust 新手问下 locust 自定义一个 socket 客户端该如何实现

denki · 2020年09月11日 · 最后由 denki 回复于 2020年09月11日 · 3657 次阅读

最近想学习一下 locust 跑一下压测,locust 本身不支持 socket,所以打算按照文档里自定义 xml-rpc 的方法自己先写一个简单的试试手。发现客户端在 getattr 里加入 wrapper 后运行会抛出一个 Nonetype 的错误,找了很久资料不知道如何解决,希望 testhome 的各位大佬能给点意见,如果有简易版的实现方法能让我参考一下就更好了。谷歌到的要么是 websocket 的要么是完全不能用的代码。
服务端代码

#-*- coding:utf-8 -*-

import time
import socket
import threading


def tcplink(sock, addr):
    msg = 'welcome'
    sock.send(msg.encode())
    while True:
        try:
            data = sock.recv(1024).decode()
            time.sleep(1)
            if data == 'exit' or not data:
                break
            sock.send('hello {}'.format(data).encode())
        except Exception as e:
            print(e)
            break
    sock.close()
    print('connect close from {}'.format(addr))


def main():
    addr = ('127.0.0.1', 9999)
    tctime = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tctime.bind(addr)
    tctime.listen(3)
    print('waiting for connection')
    while True:
        sock, addr = tctime.accept()
        print('new connect from {}'.format(addr))
        t = threading.Thread(target=tcplink, args=(sock, addr))
        t.start()
main()

客户端代码

import time
import random
import socket
from locust import User, TaskSet, events, task, event, HttpUser
from locust_plugins import run_single_user


class TcpSocketClient(socket.socket):

    _locust_environment = None

    def __init__(self, af_inet, socket_type):
        print('start init tcp socekt')
        super().__init__(af_inet, socket_type)

    def __getattr__(self, item):
        func = socket.socket.__getattr__(self, item)
        print('is it tcp wrapper')

        def wrapper(*args, **kwargs):
            start_time = time.time()
            try:

                result = func(*args, **kwargs)
            except Exception as e:
                total_time = int((time.time() - start_time) * 1000)
                self._locust_environment.events.request_failure.fire(request_type="tcpsocket", name=item,
                                                                     response_time=total_time, exception=e)
            else:
                total_time = int((time.time() - start_time) * 1000)
                print(total_time, 'total time is ')
                self._locust_environment.events.request_success.fire(request_type="tcpsocket", name=item,
                                                                    response_time=total_time, response_length=0)
                return result
            return wrapper


class TcpSocketLocust(User):
    abstract = True

    def __init__(self, *args, **kwargs):
        print('it is tcpsocket locust')
        super().__init__(*args, **kwargs)
        self.client = TcpSocketClient(socket.AF_INET, socket.SOCK_STREAM)
        self.client._locust_environment = self.environment
        ADDR = ('127.0.0.1', 9999)
        self.client.connect(ADDR)


class TcpTestUser(TcpSocketLocust):
    min_wait = 10
    max_wait = 20

    @task
    def send_data(self):
        msg = "User"

        self.client.send(msg.encode())

        data = self.client.recv(1024).decode()
        print(data)


if __name__ == '__main__':
    run_single_user(TcpTestUser)

抛出的 error

Traceback (most recent call last):
  File "D:/test-project/locustTcp.py", line 64, in <module>
    run_single_user(TcpTestUser)
  File "D:\test-project\venv\lib\site-packages\locust_plugins\debug.py", line 61, in run_single_user
    locust_class(env).run()
  File "D:/test-project/locustTcp.py", line 46, in __init__
    self.client.connect(ADDR)
  File "D:\test-project\venv\lib\site-packages\gevent\_socket3.py", line 405, in connect
    err = self.getsockopt(SOL_SOCKET, SO_ERROR)
TypeError: 'NoneType' object is not callable
共收到 2 条回复 时间 点赞

学些一下 socketio,官网应该有现成的例子

denki 关闭了讨论 09月11日 16:05
denki 重新开启了讨论 09月11日 16:05

破案了,我自己傻逼 return wrapper 多缩进了一行😅

denki 关闭了讨论 09月11日 16:05
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册