要对Spring Boot的WebSocket接口进行10万连接的压测,可以使用多线程来模拟并发连接,并发送消息。

以下是一个简单的示例代码,可以作为参考:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

@SpringBootApplication
@EnableWebSocket
public class WebSocketLoadTester implements WebSocketConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(WebSocketLoadTester.class, args);
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new LoadTestWebSocketHandler(), "/ws")
                .setAllowedOrigins("*");
    }

    private static class LoadTestWebSocketHandler extends TextWebSocketHandler {
        private static final int TOTAL_CONNECTIONS = 100000;
        private static final String MESSAGE = "Hello, WebSocket!";

        private final CountDownLatch latch = new CountDownLatch(TOTAL_CONNECTIONS);

        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            super.afterConnectionEstablished(session);
            latch.countDown();
        }

        public void sendMessage() {
            try {
                latch.await();  // 等待所有连接建立完成
                StandardWebSocketClient client = new StandardWebSocketClient();
                for (int i = 0; i < TOTAL_CONNECTIONS; i++) {
                    client.doHandshake(this, "ws://localhost:8080/ws").get();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
            session.sendMessage(new TextMessage(MESSAGE));
        }
    }
}

在上面的代码中,LoadTestWebSocketHandler是处理WebSocket消息的处理器。通过CountDownLatch来等待所有连接建立完成,然后使用StandardWebSocketClient创建10万个WebSocket连接,并发送消息。

LoadTestWebSocketHandlerhandleTextMessage方法中,我们可以设置发送的消息内容。

最后,在main方法中,使用SpringApplication.run启动Spring Boot应用,并在LoadTestWebSocketHandler中调用sendMessage方法来开始压测。

请注意,这只是一个简单的示例,并没有考虑性能优化和资源管理等方面。在实际的生产环境中,需要根据实际情况进行性能调优和资源管理

springboot 对WebSocket接口进行10w 连接多线程发送消息压测

原文地址: https://www.cveoy.top/t/topic/ijMN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录