Created
October 6, 2024 11:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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