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")
}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
