JAva版sockJs測定プログラム

15249 ワード

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>