View : 0

12/04/2026 18:15pm

Ep.29 Go and Stress Testing - Measure Your System’s Limits!

Ep.29 Go and Stress Testing - Measure Your System’s Limits!

#Go

#Golang

#Go language

#Go Programming

#Go coding

#Stress Testing

#WebSocket

#Performance Testing

#Bottlenecks

#Connection Simulation

#Programming Education

#Practice programming

#programming

#programming for beginners

#programming language

#programmers

#Superdev School

Go and Stress Testing - Measure Your System’s Limits!

 

In this episode, we will look at how to conduct Stress Testing or performance testing of WebSocket to measure how many simultaneous users your system can handle, as well as identify bottlenecks for performance improvement!

 

What is Stress Testing?

Stress Testing simulates system usage in heavy conditions, such as many simultaneous connections or frequent message sending, to test whether the system can perform as expected.

 

Objectives of Stress Testing

1. Measure the system's maximum capacity.

2. Identify bottlenecks such as CPU, RAM, or network.

3. Improve the performance and stability of the system.

 

Tools for Stress Testing WebSocket

1. autocannon : An easy-to-use HTTP and WebSocket testing tool.

2. wrk : An efficient HTTP testing tool (supports WebSocket through scripts).

3. Custom Go Script : Use Go to create a WebSocket Client to simulate usage.

 

How to Use autocannon with WebSocket:

1. Install autocannon.

npm install -g autocannon

 

2. Run autocannon to test WebSocket.

Assuming the WebSocket Server is running at ws://localhost:8080/ws:

Explanation :

  • c 100 : Number of simultaneous connections (100 connections).
  • d 30 : Duration of the test (30 seconds).
  • p 10 : Number of concurrent requests per connection (10 requests).

Result: autocannon will display results such as the number of requests per second (Requests/sec) and latency.

autocannon -c 100 -d 30 -p 10 ws://localhost:8080/ws

 

Creating Stress Testing with Go

For flexibility, we can write a Go program to simulate a large number of WebSocket connections. Example code for Stress Testing with Go.

Explanation :

  • clientCount : Number of connections to test.
  • testConnection : Function to simulate connections and send messages.
  • sync.WaitGroup : Used to wait for all connections to complete.
package main

import (
    "log"
    "net/url"
    "sync"
    "time"

    "github.com/gorilla/websocket"
)

func testConnection(id int, wg *sync.WaitGroup) {
    defer wg.Done()

    u := url.URL{Scheme: "ws", Host: "localhost:8080", Path: "/ws"}
    conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
    if err != nil {
        log.Printf("Client %d: Error connecting: %v", id, err)
        return
    }
    defer conn.Close()

    for i := 0; i < 10; i++ { // ส่งข้อความ 10 ครั้ง
        msg := []byte("Hello from client " + string(id))
        if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil {
            log.Printf("Client %d: Error writing message: %v", id, err)
            return
        }
        _, response, err := conn.ReadMessage()
        if err != nil {
            log.Printf("Client %d: Error reading response: %v", id, err)
            return
        }
        log.Printf("Client %d: Received: %s", id, response)
        time.Sleep(500 * time.Millisecond) // เว้นช่วงเวลา
    }
}

func main() {
    var wg sync.WaitGroup
    clientCount := 100

    for i := 0; i < clientCount; i++ {
        wg.Add(1)
        go testConnection(i, &wg)
    }

    wg.Wait()
    log.Println("Stress test completed")
}
 

Analyzing Results

After testing, you should analyze:

1. CPU and RAM usage : Check if resources are sufficient.

2. Latency : The time taken to respond to requests.

3. Failed connections : The number of unsuccessful or dropped connections.

 

Performance Improvements After Testing

1. Increase Cluster Size : If the system cannot support a large number of users, add servers to the Cluster.

2. Use Load Balancer : Such as NGINX or Kubernetes to distribute requests.

3. Implement Rate Limiting : Limit requests per user to reduce server load.

 

In Summary

  • Stress Testing helps you understand the limits of your system.
  • Use tools like autocannon or write Go programs to test.
  • Analyze results to make decisions on performance improvements.