java版sockJs压测程序

package com.example.demo.config;


import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;

import java.util.ArrayList;
import java.util.List;

public class Test {
    static int j = 1000;
    public static void main(String[] args) throws Exception{
        for (int i = 0; i < 30; i++) {
            new Thread(new Inc()).start();
            //Thread.sleep(1000);
        }
    }
    static synchronized void inc() throws Exception{
        System.out.println(Thread.currentThread().getName() + ": " + j++);
        List<Transport> transports = new ArrayList<>(1);
        transports.add(new RestTemplateXhrTransport());
        SockJsClient sockJsClient = new SockJsClient(transports);
        ListenableFuture<WebSocketSession> wsSession = sockJsClient.doHandshake(new MyHandler(), "ws://ip:端口/sockjs/box/register?param="+j);
        //Thread.sleep(1000);
    }
   static class Inc implements Runnable {
        public void run() {
            for (int i = 0; i < 1000; i++) {
                try{
                    inc();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}





package com.example.demo.config;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

public class MyHandler implements WebSocketHandler {

    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
        log.info("connection is established!");
    }

    @Override
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {

    }

    @Override
    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {

    }

    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {

    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}

注意事项:
1、需要将服务端程序所在服务器文件数调大,例如掉大到600000,可用命令ulimit -n 600000
2、 需要用到的依赖

<dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-websocketartifactId>
dependency>

你可能感兴趣的:(java)