Created
May 2, 2023 10:42
-
-
Save fujimaki-k/33317701473160f1ceef20c68c4bc772 to your computer and use it in GitHub Desktop.
CPU and memory profiler example
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 ( | |
"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