k6 pressure test
This commit is contained in:
68
backend/internal/handlers/websocket_loadtest.go
Normal file
68
backend/internal/handlers/websocket_loadtest.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/M1ngdaXie/realtime-collab/internal/hub"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// loadTestUpgrader allows any origin for load testing
|
||||
var loadTestUpgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // Allow all origins for load testing
|
||||
},
|
||||
}
|
||||
|
||||
// HandleWebSocketLoadTest is a backdoor WebSocket handler that skips all authentication.
|
||||
// WARNING: Only use this for local load testing! Do not expose in production.
|
||||
//
|
||||
// Usage: ws://localhost:8080/ws/loadtest/:roomId
|
||||
// Optional query param: ?user=customUserName
|
||||
func (wsh *WebSocketHandler) HandleWebSocketLoadTest(c *gin.Context) {
|
||||
roomID := c.Param("roomId")
|
||||
if roomID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "roomId is required"})
|
||||
return
|
||||
}
|
||||
|
||||
// Generate or use provided user identifier
|
||||
userName := c.Query("user")
|
||||
if userName == "" {
|
||||
userName = "LoadTestUser-" + uuid.New().String()[:8]
|
||||
}
|
||||
|
||||
// Upgrade connection - NO AUTH CHECK
|
||||
conn, err := loadTestUpgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Printf("Failed to upgrade connection: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create client with full edit permissions (no userID, anonymous)
|
||||
clientID := uuid.New().String()
|
||||
client := hub.NewClient(
|
||||
clientID,
|
||||
nil, // userID - nil for anonymous
|
||||
userName, // userName
|
||||
nil, // userAvatar
|
||||
"edit", // permission - full access for load testing
|
||||
conn,
|
||||
wsh.hub,
|
||||
roomID,
|
||||
)
|
||||
|
||||
// Register client
|
||||
wsh.hub.Register <- client
|
||||
|
||||
// Start goroutines
|
||||
go client.WritePump()
|
||||
go client.ReadPump()
|
||||
|
||||
log.Printf("[LOADTEST] Client connected: %s (user: %s) to room: %s", clientID, userName, roomID)
|
||||
}
|
||||
@@ -51,9 +51,9 @@ type Hub struct {
|
||||
func NewHub() *Hub {
|
||||
return &Hub{
|
||||
rooms: make(map[string]*Room),
|
||||
Register: make(chan *Client),
|
||||
Unregister: make(chan *Client),
|
||||
Broadcast: make(chan *Message, 1024),
|
||||
Register: make(chan *Client, 256),
|
||||
Unregister: make(chan *Client, 256),
|
||||
Broadcast: make(chan *Message, 4096),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user