Skip to content

Instantly share code, notes, and snippets.

@DarinM223
Last active December 4, 2015 20:48
Show Gist options
  • Save DarinM223/b2ef6859f1f740a16361 to your computer and use it in GitHub Desktop.
Save DarinM223/b2ef6859f1f740a16361 to your computer and use it in GitHub Desktop.
Profiling Golang apps

Profiling Go Apps

One of the great things about Go is that you can easily profile your code to find and optimize bottlenecks. Here are the steps that I went through to profile my Go apps.

Write Benchmark

// Inside test file
func BenchmarkBlah(b *testing.B) {
	for n := 0; n < b.N; n++ {
		// do some constant function here
	}
}

Compile the test into an executable and run the benchmarks

go test -c
./blah.test -test.bench=. -test.cpuprofile=cpu.out

Run the pprof tool with the test executable and the generated profile file

go tool pprof blah.test cpu.out

Some commands you can do inside the profile file

See the top 10 slowest cumulative functions

top10 -cum 

Generate a svg with a graph of functions and dependencies

web

Inspect inside a function and see how long each line takes on average

list <function_name>

Same as list but displays the results through a webpage

weblist <function_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment