Profile with pprof

Profile Go programs by capturing CPU or memory data with runtime/pprof and analyzing it using the pprof tool.

Use runtime/pprof to capture CPU or memory profiles and analyze them with the pprof tool. Start a CPU profile by calling pprof.StartCPUProfile with a file handle, let your program run, then stop it with pprof.StopCPUProfile and analyze the output file.

package main

import (
	"os"
	"runtime/pprof"
)

func main() {
	f, _ := os.Create("cpu.prof")
	pprof.StartCPUProfile(f)
	// Run your workload here
	pprof.StopCPUProfile()
	f.Close()
}

Run go tool pprof cpu.prof to visualize the results.