Skip to content

Instantly share code, notes, and snippets.

@fujimaki-k
Created May 2, 2023 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimaki-k/33317701473160f1ceef20c68c4bc772 to your computer and use it in GitHub Desktop.
Save fujimaki-k/33317701473160f1ceef20c68c4bc772 to your computer and use it in GitHub Desktop.
CPU and memory profiler example
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
)
func main() {
cpu, err := os.Create("/dev/shm/cpu.pprof")
if err != nil {
panic(err)
}
defer func() {
if err = cpu.Close(); err != nil {
panic(err)
}
}()
if err := pprof.StartCPUProfile(cpu); err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
fmt.Println("Fight dayo!")
memory, err := os.Create("/dev/shm/memory.prof")
if err != nil {
panic(err)
}
defer func() {
if err = memory.Close(); err != nil {
panic(err)
}
}()
runtime.GC()
if err := pprof.WriteHeapProfile(os.Stdout); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment