Standard library / runtime/pprof
pprof.StartCPUProfile()
Start process-wide CPU sampling. Only one CPU profile may be active. Stop it before using the complete output. Sampling may miss short-lived work. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func StartCPUProfile(w io.Writer) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Destination writer; keep it open until profiling or tracing has stopped. |
Return Value
error
Nil on successful startup; error if profiling is already active.
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "runtime" "runtime/pprof" "time")
func main() { var b bytes.Buffer if err := pprof.StartCPUProfile(&b); err != nil { panic(err) } end := time.Now().Add(120 * time.Millisecond) var n uint64 = 1 for time.Now().Before(end) { for i := 0; i < 1000; i++ { n = n*1664525 + 1013904223 } } runtime.KeepAlive(n) pprof.StopCPUProfile() fmt.Println(b.Len() > 2 && b.Bytes()[0] == 0x1f && b.Bytes()[1] == 0x8b)}