06/05/2026 08:38am

EP.107 Profiling and Benchmarking Your WebSocket Server
#WebSocket
#Profiling
#Benchmark
#Go
#Golang
Once your WebSocket Server is production-ready, the goal is no longer just "it works" β it must be fast, stable, and efficiently use system resources. π
In this episode, weβll dive into the world of Profiling and Benchmarking to assess how well your code performs β plus techniques to optimize your server beyond its current limits!
Tools weβll use:
π¬ pprof β Analyze CPU, memory, and goroutine usage
π£ wrk β Load testing and request throughput
β‘ hey β Latency and connection success rate testing
π§© 1. Why Do Profiling and Benchmarking Matter?
Because without measuring, youβll never know why your code is slow.
Hereβs what you can uncover:
| What to Check | Why It Matters |
|---|---|
| CPU hotspots | Optimize logic or avoid nested loops |
| High memory usage | Detect memory leaks |
| Goroutines stuck or bloated | Prevent deadlocks or panics |
| High latency | Locate processing bottlenecks |
| Max concurrent connections | Plan accurate system scaling |
π 2. Using pprof to Analyze Runtime Code
pprof is a built-in tool in Go that lets you inspect your system's performance in real-time.
You can expose a /debug/pprof/ endpoint and analyze it via browser or CLI.
Example code to start pprof:
import (
"log"
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
log.Println("Start pprof on :6060")
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
log.Println("WebSocket Server on :8080")
http.ListenAndServe(":8080", nil)
}
βοΈ 3. Using wrk to Test Load and Throughput
wrk is a powerful benchmark tool for firing high-volume HTTP or WebSocket requests.
Example command:
wrk -t12 -c400 -d30s http://localhost:8080/ws
| Parameter | Description |
|---|---|
-t12 | Use 12 threads |
-c400 | Simulate 400 concurrent connections |
-d30s | Run test for 30 seconds |
Output includes:
- Requests/sec
- Average Latency
- Transfer/sec
β‘ 4. Using hey to Measure Latency and Error Rate
hey is a CLI tool that helps test response time and system stability.
Install:
go install github.com/rakyll/hey@latest
Example usage:
hey -n 1000 -c 50 http://localhost:8080/ws
Output includes:
π Average latency
β Error rate
β
Success rate
π§ 5. Optimization Techniques for Production
Once you gather performance data, use it to improve your system:
| Issue | Solution |
|---|---|
| High CPU usage | Use goroutine pool / minimize JSON parsing |
| High memory usage | Use sync.Pool for buffer reuse |
| High latency | Use binary protocol / reduce compression overhead |
| Frequent disconnects | Tune timeout & retry logic |
| Heavy GC pressure | Tweak runtime.GCPercent / check for leaks |
π 6. Example Benchmark Summary
| Metric | Average | Good Target |
|---|---|---|
| Latency | 8.5 ms | < 10 ms |
| Throughput | 22,000 req/sec | > 20,000 |
| Memory Usage | 85 MB | < 100 MB |
| Error Rate | 0.03% | < 0.1% |
Creating a summary like this every sprint helps your team plan scaling and optimization like professionals.
π Challenge Time!
β
Enable pprof on your actual WebSocket Server
β
Use wrk and hey to benchmark from another machine
β
Compare results before and after optimization
β
Create a dashboard to track benchmark changes every sprint
π Next EP
π EP.108: Advanced Timeout and Connection Cleanup
Weβll explore techniques to handle dropped, idle, or slow connections to prevent resource leaks and ensure maximum stability in production environments. π§
Read more
π΅ Facebook: Superdev Academy
π΄ YouTube: Superdev Academy
πΈ Instagram: Superdev Academy
π¬ TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
π Website: https://www.superdevacademy.com/en