Go has two excellent ways to analyze the performance of your code. We have benchmark tests and the fantastic pprof.
Benchmarking is a way of measuring the performance of your code by executing it multiple times with a fixed workload. We took a look at this briefly in Chapter 1, Introduction to Microservices, where we ascertained that the json.Marshal
method was slower than the json.Encode
method. While this is a useful feature, I find it tough to work out what I should benchmark. If I am writing an algorithm, then this is relatively straightforward. However, when writing a microservice that is predominately interacting with a database, it is far more challenging.
To demonstrate how easy it is to execute benchmarks in Go, take a look at chandlers/search_bench_test.go
:
11 func BenchmarkSearchHandler(b *testing.B) { 12 mockStore = &data.MockStore{} 13 mockStore.On("Search", "Fat Freddy's Cat").Return([]data.Kitten{ 14 data.Kitten{ ...