Skip to content

Instantly share code, notes, and snippets.

@AllanM007
Created October 6, 2024 11:17
Show Gist options
  • Save AllanM007/9894584ccb4d7c89e7e1dca043c7df53 to your computer and use it in GitHub Desktop.
Save AllanM007/9894584ccb4d7c89e7e1dca043c7df53 to your computer and use it in GitHub Desktop.
Utilizing pprof golang utility to monitor program behaviour(execution time, memory usage, goroutines e.t.c) to understand time and resource utilization better
package main
import (
"log"
"os"
"runtime/pprof"
"time"
)
func main(){
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
//Simulate some CPU-intensive work
for i:= 0; i< 1000000; i++ {
_ = i * 1
}
//Allow time for all CPU usage to be captured
time.Sleep(2 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment