Golang / GoLang System Architecture and Testing Interview Questions
How do you load test a Go microservice and interpret the results?
Load testing validates that a service meets performance requirements under expected and peak traffic. Go services are typically tested with k6, vegeta, or the Go-native go-wrk. The key metrics: throughput (RPS), latency percentiles (p50, p95, p99), and error rate.
// Vegeta: Go-native load testing library import vegeta "github.com/tsenart/vegeta/v12/lib" func LoadTestGetUser(t *testing.T) { if testing.Short() { t.Skip("skipping load test in short mode") } rate := vegeta.Rate{Freq: 100, Per: time.Second} // 100 RPS duration := 30 * time.Second targeter := vegeta.NewStaticTargeter(vegeta.Target{ Method: "GET", URL: "http://localhost:8080/users/1", }) attacker := vegeta.NewAttacker() var metrics vegeta.Metrics for res := range attacker.Attack(targeter, rate, duration, "load test") { metrics.Add(res) } metrics.Close() t.Logf("Requests: %d", metrics.Requests) t.Logf("Success: %.2f%%", metrics.Success*100) t.Logf("Throughput: %.2f rps", metrics.Throughput) t.Logf("Latency p50: %v", metrics.Latencies.P50) t.Logf("Latency p95: %v", metrics.Latencies.P95) t.Logf("Latency p99: %v", metrics.Latencies.P99) // Assertions if metrics.Success < 0.999 { t.Errorf("success rate %.2f%% below 99.9%%", metrics.Success*100) } if metrics.Latencies.P99 > 50*time.Millisecond { t.Errorf("p99 latency %v exceeds 50ms SLO", metrics.Latencies.P99) } } // Benchmark as a proxy for load test (lower overhead) func BenchmarkHandlerThroughput(b *testing.B) { srv := httptest.NewServer(buildRouter()) defer srv.Close() client := srv.Client() b.SetParallelism(10) // 10 goroutines à GOMAXPROCS concurrent b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { resp, _ := client.Get(srv.URL + "/users/1") io.Discard.Write(resp.Body) resp.Body.Close() } }) b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "rps") }
More Related questions...