Created
October 4, 2017 22:33
-
-
Save wcharczuk/5ccc55ff5f808c459bc0fdba46b57068 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/pprof" | |
| "time" | |
| ) | |
| func main() { | |
| duration := time.After(45 * time.Second) | |
| abort := make(chan bool) | |
| aborted := make(chan bool) | |
| go profile(abort, aborted) | |
| go dummyWork() | |
| <-duration | |
| abort <- true | |
| <-aborted | |
| println("complete") | |
| } | |
| func profile(abort chan bool, aborted chan bool) error { | |
| var f *os.File | |
| var err error | |
| ticker := time.Tick(5 * time.Second) | |
| for { | |
| select { | |
| case <-ticker: | |
| fmt.Printf("f: %#v\n", f) | |
| if f != nil { | |
| println("timeout") | |
| pprof.StopCPUProfile() | |
| f.Close() | |
| println("timeout complete") | |
| } | |
| println("tick") | |
| f, err = os.Create(fmt.Sprintf("cpuprofile_%d", time.Now().Unix())) | |
| if err != nil { | |
| return err | |
| } | |
| println("start profile") | |
| if err := pprof.StartCPUProfile(f); err != nil { | |
| return err | |
| } | |
| println("tick complete") | |
| continue | |
| case <-abort: | |
| aborted <- true | |
| return nil | |
| } | |
| } | |
| return nil | |
| } | |
| func dummyWork() { | |
| if fib(99) < 1 { | |
| panic("wat") | |
| } | |
| } | |
| func fib(n int) int { | |
| if n < 2 { | |
| return 1 | |
| } | |
| return fib(n-1) + fib(n-2) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment