shikeying
2024-02-23 1d6e7763f4a30272cc0818ea12f83697b7375c45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.iplatform.tcp;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
public class WebsocketClientTest {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
//    private static final URI wsUri = URI.create("ws://127.0.0.1:60000/websocket");
//    private static final URI wsUri = URI.create("ws://116.198.40.76:60000/websocket");
//    private static final URI wsUri = URI.create("ws://10.8.4.35:60035/websocket");
//    private static final URI wsUri = URI.create("ws://www.shikeying.com:60001/websocket");
//    private static final URI wsUri = URI.create("ws://ctoms.chinapost.com.cn/ws/websocket");
    private static final URI wsUri = URI.create("ws://localhost:60000");
//    private static final URI wsUri = URI.create("ws://10.8.4.98:60011/websocket");
 
    private static Map<String, DemoWebsocketClient> cacheClient = new ConcurrentHashMap<>(32770);
 
    private ExecutorService executorService = Executors.newFixedThreadPool(4);
//    private static int currentSize = 20000;
    private static int currentSize = 1;
    private static final int TOTAL_LINES = 2;
//    private static final int TOTAL_LINES = 16;
//    private static final int BATCH_SIZE = 8;
    private static final int BATCH_SIZE = 2;
 
    public void createBatch(){
        logger.info(".........开始测试通信连接.......... TOTAL_LINES = {}, url = {}", TOTAL_LINES, wsUri);
        while(currentSize < TOTAL_LINES){
            try {
                this.executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        int count = BATCH_SIZE;
                        for(int i=0; i<count; i++){
                            String uri = "mike" + (currentSize);
                            DemoWebsocketClient client = createOneClient(wsUri, uri);
                            logger.info("创建了一个客户端: " + uri);
                            cacheClient.put(uri, client);
                            currentSize ++;
                        }
                    }
                });
            } catch (Exception ex){
                ex.printStackTrace();
            } finally {
                try {
                    logger.info("currentSize = {}", currentSize);
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
 
        logger.info("已累计创建连接:{} 个", currentSize);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
 
//        logger.info("准备关闭所有链接对象...");
//        for(DemoWebsocketClient client : cacheClient.values()){
//            client.close();
//        }
//        logger.info("执行了关闭操作,所有连接稍后会完全关闭");
    }
 
    private DemoWebsocketClient createOneClient(URI uri, String uid){
        DemoWebsocketClient client = new DemoWebsocketClient(uri);
        client.setUid(uid);
        client.setConnectionLostTimeout(0);
        try {
            client.connectBlocking();
        } catch (InterruptedException e) {
            throw new RuntimeException("创建client错误, uid=" + uid + ", " + e.getMessage(), e);
        }
        return client;
    }
 
//    public void createOneClient(){
//        DemoWebsocketClient client = new DemoWebsocketClient(wsUri);
//        client.setUid("mike");
//        try {
//            client.connectBlocking();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        WaitConsoleInput.waitInput();
//    }
}