From 2ae5e75f9a0942053de4eb379ff3902abc0ddcd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Fri, 10 Sep 2021 10:20:46 +0200 Subject: [PATCH] Quick snippet --- guide/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/guide/README.md b/guide/README.md index 13c075a..2b96d6a 100644 --- a/guide/README.md +++ b/guide/README.md @@ -141,6 +141,14 @@ You can enable the CPU profiler via various APIs: - [`pprof.StartCPUProfile(w)`](https://pkg.go.dev/runtime/pprof#StartCPUProfile) will capture a CPU profile to `w` that covers the time span until [`pprof.StopCPUProfile()`](https://pkg.go.dev/runtime/pprof#StopCPUProfile) is called. - [`import _ "net/http/pprof"`](https://pkg.go.dev/net/http/pprof) allows you to request a 30s CPU profile by hitting the `GET /debug/pprof/profile?seconds=30` of the default http server that you can start via `http.ListenAndServe("localhost:6060", nil)`. +If you need a quick snippet to paste into your `main()` function, you can use the code below: + +```go +file, _ := os.Create("./cpu.pprof") +pprof.StartCPUProfile(file) +defer pprof.StopCPUProfile() +``` + Regardless of how you activate the CPU profiler, the resulting profile will be a frequency table of stack traces formatted in the binary [pprof](../pprof.md) format. A simplified version of such a table is shown below: |stack trace|samples/count|cpu/nanoseconds|