Refactor memory profiler, change output format

This commit is contained in:
Dan Sosedoff 2014-11-23 14:44:42 -06:00
parent 22bcfafa76
commit 4baa5ec3c6
2 changed files with 17 additions and 11 deletions

View File

@ -204,7 +204,7 @@ func main() {
} }
if options.Debug { if options.Debug {
go startRuntimeProfiler() startRuntimeProfiler()
} }
startServer() startServer()

View File

@ -1,7 +1,8 @@
package main package main
import ( import (
"fmt" "log"
"os"
"runtime" "runtime"
"time" "time"
) )
@ -9,16 +10,21 @@ import (
const MEGABYTE = 1024 * 1024 const MEGABYTE = 1024 * 1024
func startRuntimeProfiler() { func startRuntimeProfiler() {
m := &runtime.MemStats{} go func() {
logger := log.New(os.Stdout, "", 0)
m := &runtime.MemStats{}
for { for {
runtime.ReadMemStats(m) runtime.ReadMemStats(m)
fmt.Println("-----------------------") logger.Printf(
fmt.Println("Goroutines:", runtime.NumGoroutine()) "[DEBUG] Goroutines: %v, Mem used: %v (%v mb), Mem acquired: %v (%v mb)\n",
fmt.Println("Memory acquired:", m.Sys, "bytes,", m.Sys/MEGABYTE, "mb") runtime.NumGoroutine(),
fmt.Println("Memory used:", m.Alloc, "bytes,", m.Alloc/MEGABYTE, "mb") m.Alloc, m.Alloc/MEGABYTE,
m.Sys, m.Sys/MEGABYTE,
)
time.Sleep(time.Second * 30) time.Sleep(time.Second * 30)
} }
}()
} }