-
-
Save Theodore-Rose/ee1ac25c2add7afa321cc3ad7f61c2f7 to your computer and use it in GitHub Desktop.
Adding memory profile
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
func writeHeap() { | |
log.Info("Writing profiles") | |
resp, err := http.Get("http://localhost:8024/debug/pprof/heap") | |
if err != nil { | |
log.Infof("Failed at http.Get error: %+v", err) | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Infof("Failed at read body error: %+v", err) | |
return | |
} | |
now := time.Now() | |
dirName := "/tmp/goprofile" | |
if _, err := os.Stat(dirName); err != nil { | |
err := os.MkdirAll(dirName, os.ModePerm) | |
if err != nil { | |
log.Errorf("Failed to create dir: %s, error: %+v", dirName, err) | |
return | |
} | |
} | |
fileName := fmt.Sprintf("%s/heap.%v.pprof", dirName, *utils.ToUnix(&now)) | |
err = ioutil.WriteFile(fileName, body, 0644) | |
if err != nil { | |
log.Infof("Failed at write body error: %+v", err) | |
return | |
} | |
log.Infof("Wrote heap file: %s", fileName) | |
} | |
func main() { func main() { | |
defer pf.Start(pf.MemProfile).Stop() | |
ticker := time.NewTicker(1 * time.Hour) | |
go func() { | |
for { | |
select { | |
case <-ticker.C: | |
writeHeap() | |
} | |
} | |
}() | |
go func() { | |
err := http.ListenAndServe(":8024", nil) | |
if err != nil { | |
log.Errorf("Failed at http server: %+v", err) | |
} | |
}() | |
//block - should replace with real main service code | |
select{ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment