go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/pkg/app/stats.go

44 lines
855 B

package app
import (
"os"
"reflect"
"runtime/pprof"
)
var stats struct {
v8workerSend int
v8workerRespond int
v8workerRecv int
v8workerBytesSent int
v8workerBytesRecv int
}
func initStats(args []string) {
initCpuProfile(strArg(args, "--cpuprof"))
initMemoryProfile(strArg(args, "--memprof"))
OnFree(freeStats)
}
func initCpuProfile(cpuprof string) {
if cpuprof != "" {
cpuProfile, err := os.Create(cpuprof)
check(err)
check(pprof.StartCPUProfile(cpuProfile))
OnStop(pprof.StopCPUProfile)
}
}
func initMemoryProfile(memprof string) {
if memprof != "" {
memProfile, err := os.Create(memprof)
check(err)
check(pprof.WriteHeapProfile(memProfile))
OnStop(func() { check(memProfile.Close()) })
}
}
func freeStats() {
newStats := reflect.New(reflect.TypeOf(stats))
reflect.ValueOf(stats).Set(newStats.Elem())
}