socket.io java client - 使用线程

时间:2017-09-11 07:40:54

标签: java multithreading websocket socket.io-java-client

我正在使用socket.io-client java库版本1.0.0(通过pom.xml包含它)

我想对nodejs中基于socket.io的服务器进行压力/性能测试(服务器似乎运行良好,这里不感兴趣)。

当我建立联系时,我遇到了一种非常奇怪的行为 每个新连接之间的Thread.sleep。 如果一次建立连接,则没有问题。当我等待(Thead.sleep())几毫秒时会出现问题。

始终在第5次建立的连接之后,连接需要很长时间才能建立连接(已收到连接的回叫)。下一个连接似乎要等到第一个连接断开,但此后建立了几个连接。无论我等多久,都没有建立很多联系。这真的很奇怪。为什么会这样?

无所谓,如果我异步建立连接(在一个单独的线程中立即启动所有线程的每个连接)或者一个接一个地同步 - 如果我不在每个新的套接字/线程之间等待它就可以正常工作创建

这是完整的代码:

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

import java.net.URISyntaxException;

public class SocketStressTest
{
    private static final String SOCKET_URI = "http://test.mydomain.com/";
    public static int AMOUNT_OF_SOCKETS = 100;
    IO.Options opts;
    private int connectionCounter = 0;
    private int disConnectionCounter = 0;


    public static void main(String[] args)
    {
        new SocketStressTest().start();
    }

    private void start()
    {
        opts = new IO.Options();
        opts.forceNew = true;
        opts.reconnection = false;

        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                initSockets();
            }
        });

        thread.start();
        System.out.println("Generated all sockets");

    }

    private void initSockets()
    {
        for (int i = 0; i < AMOUNT_OF_SOCKETS; i++)
        {
            //when I comment out sleeping and try-catch below, everything works fine
            try
            {
                Thread.sleep(400);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            createSocket(i);
        }
    }

    private void createSocket(final int nr)
    {

        //does not matter if I do this threaded or not:
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    System.out.println("nr " + nr + " creating socket");
                    final Socket socket = IO.socket(SOCKET_URI, opts);
                    socket
                            .on(Socket.EVENT_CONNECT, new Emitter.Listener()
                            {

                                public void call(Object... args)
                                {
                                    connectionCounter++;
                                    System.out.println("nr " + nr + " connected id:" + socket.id() + " concounter:" + connectionCounter);
                                    if (connectionCounter == AMOUNT_OF_SOCKETS)
                                        System.out.println("===> ALL connected!");
                                }
                            })

                            .on(Socket.EVENT_DISCONNECT, new Emitter.Listener()
                            {

                                public void call(Object... args)
                                {
                                    disConnectionCounter++;
                                    System.out.println("nr " + nr + " disconnected. discounter=" + disConnectionCounter);
                                    if (disConnectionCounter == AMOUNT_OF_SOCKETS)
                                        System.out.println("<=== ALL DISCONNECTED!");
                                }
                            });

                    socket.connect();
                    System.out.println("nr " + nr + " connect called");
                }
                catch (URISyntaxException e)
                {
                    e.printStackTrace();
                }

            }
        });

        thread.start();

    }

}

这是示例输出(缩短),在循环内等待400ms:

Generated all sockets
nr 0 creating socket
nr 0 connect called
nr 1 creating socket
nr 1 connect called
nr 0 connected id:7JLvH0hHNF0pg36mAAW3 concounter:1
nr 1 connected id:5fj3I_bFIa1JeUlXAAW4 concounter:2
nr 2 creating socket
nr 2 connect called
nr 2 connected id:RQTLEjftWna2JPuFAAW5 concounter:3
nr 3 creating socket
nr 3 connect called
nr 3 connected id:dg1xL9ddnLqwAlDsAAW6 concounter:4
nr 4 creating socket
nr 4 connect called
nr 4 connected id:y_zIvI4BXdhmEiuwAAW7 concounter:5
nr 5 creating socket
nr 5 connect called
nr 6 creating socket
nr 6 connect called
...
nr 25 creating socket
nr 25 connect called
nr 26 creating socket
nr 26 connect called
nr 0 disconnected. discounter=1
nr 1 disconnected. discounter=2
nr 5 connected id:zCoCg1qG1vJA7pezAAW8 concounter:6
nr 6 connected id:QZJA3yhcXzpRzCwgAAW9 concounter:7
nr 7 connected id:aNZMGdiY8bTeylz3AAW- concounter:8
nr 8 connected id:vitG7xSlEXO5AhnoAAW_ concounter:9
nr 9 connected id:kWirqWwxE5V4ITRiAAXA concounter:10
nr 10 connected id:gCbDdV62pzPRq71qAAXB concounter:11
nr 11 connected id:4ERh1JvC654ky96AAAXC concounter:12
nr 12 connected id:4QMQni7Ohjk0IO7XAAXD concounter:13
nr 27 creating socket
nr 27 connect called
nr 13 connected id:Xb8i-VeDaE_G9N2PAAXE concounter:14
nr 14 connected id:AccfpvKWkWoGY7TEAAXF concounter:15

示例输出无需等待(注释掉睡眠(400)(缩短):

Generated all sockets
nr 0 creating socket
nr 1 creating socket
...
nr 97 creating socket
nr 99 creating socket
nr 24 connect called
nr 21 connect called
nr 52 connect called
...
nr 78 connect called
nr 18 connect called
nr 24 connected id:N4MyGCp4IIWwMtJhAAXV concounter:1
nr 85 connected id:0ahc2QOlGpzPqUwjAAXW concounter:2
nr 68 connected id:W280V6PpH-gUxogOAAXX concounter:3
...
nr 38 connected id:_4c8Ll0cCM_1oQYzAAY2 concounter:98
nr 11 connected id:lIyRiuxK8pmb9voAAAY3 concounter:99
nr 12 connected id:pZkW0Y5DxXgw-Sy6AAY4 concounter:100
===> ALL connected!

我在2台不同的机器上试过了。相同的行为。

我的目标是通过一些等待调整我的压力测试,但是这种行为不可能进行测试。任何想法?我做错了什么吗? 我正在Windows 10上进行测试。 我还从它创建了一个jar并在debian系统上进行了测试。相同的行为。 我还创建了一个类似的javascript测试,用于同时进行超过500个套接字连接 - 就像一个魅力。

在这个socket.io java客户端库中是否存在一些奇怪的编码,它同时不允许超过5个连接?

0 个答案:

没有答案