性能测试工具 locust 测试 tcp 长连接,只能建立一个连接,如何解决

luo · 2019年02月19日 · 最后由 chuan2324 回复于 2020年05月09日 · 3333 次阅读

使用 locust 对 tcp 做长连接的压测,代码如下

import time
import random
# from socket import socket, AF_INET, SOCK_STREAM
import socket
from locust import Locust, TaskSet, events, task


class TcpSocketClient(socket.socket):
    def __init__(self, af_inet, socket_type):
        super(TcpSocketClient, self).__init__(af_inet, socket_type)

    def connect(self, addr):
        start_time = time.time()
        try:
            super(TcpSocketClient, self).connect(addr)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="tcpsocket", name="connect", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="tcpsocket", name="connect", response_time=total_time,
                                        response_length=0)

    def send(self, msg):
        start_time = time.time()
        try:
            super(TcpSocketClient, self).send(msg)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="tcpsocket", name="send", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="tcpsocket", name="send", response_time=total_time,
                                        response_length=0)

    def recv(self, bufsize):
        recv_data = ''
        start_time = time.time()
        try:
            recv_data = super(TcpSocketClient, self).recv(bufsize)
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type="tcpsocket", name="recv", response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type="tcpsocket", name="recv", response_time=total_time,
                                        response_length=0)
        return recv_data
class TcpSocketLocust(Locust):
    """
    This is the abstract Locust class which should be subclassed. It provides an TCP socket client
    that can be used to make TCP socket requests that will be tracked in Locust's statistics.
    """
    def __init__(self, *args, **kwargs):
        super(TcpSocketLocust, self).__init__(*args, **kwargs)
        self.client = TcpSocketClient(socket.AF_INET, socket.SOCK_STREAM)
        ADDR = (self.host, self.port)
        self.client.connect(ADDR)


class TcpTestUser(TcpSocketLocust):
    host = "10.20.28.133"
    port = 8173

    class task_set(TaskSet):
        @task
        def send_data(self):
            self.client.send(b"\xff\xff\x00\n\xc9\x00\x01\x00\xa6\r")
            time.sleep(5)


if __name__ == "__main__":
    user = TcpTestUser()
    user.run()

建立一个链接时正常,建立 2 个连接时报错如下:

请大神答疑解惑

共收到 2 条回复 时间 点赞

没发现你哪里有问题,跟例子的一样的,我拿你的直接跑的,没报错

Name # reqs 50% 66% 75% 80% 90% 95% 98% 99% 100%

tcpsocket connect 11 2 2 3 3 3 3 3 3 3
tcpsocket recv 208 0 0 0 0 0 0 0 0 1

tcpsocket send 208 0 0 0 0 0 0 0 0 0

Total 427 0 0 0 0 0 0 2 2 3

楼主系统内置参数可能有问题,限制了 socket 最大连接数,但是不应该 2 个就报错呀

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册