mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 15:30:55 +00:00
toolbox: improve the profile
This commit is contained in:
parent
f4867aad5a
commit
3969cd3b40
@ -13,6 +13,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@ -43,40 +44,48 @@ func ProcessInput(input string, w io.Writer) {
|
|||||||
case "lookup block":
|
case "lookup block":
|
||||||
p := pprof.Lookup("block")
|
p := pprof.Lookup("block")
|
||||||
p.WriteTo(w, 2)
|
p.WriteTo(w, 2)
|
||||||
case "start cpuprof":
|
case "get cpuprof":
|
||||||
StartCPUProfile()
|
GetCPUProfile(w.(http.ResponseWriter))
|
||||||
case "stop cpuprof":
|
|
||||||
StopCPUProfile()
|
|
||||||
case "get memprof":
|
case "get memprof":
|
||||||
MemProf()
|
MemProf(w)
|
||||||
case "gc summary":
|
case "gc summary":
|
||||||
PrintGCSummary(w)
|
PrintGCSummary(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// record memory profile in pprof
|
// record memory profile in pprof
|
||||||
func MemProf() {
|
func MemProf(w io.Writer) {
|
||||||
if f, err := os.Create("mem-" + strconv.Itoa(pid) + ".memprof"); err != nil {
|
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
|
||||||
log.Fatal("record memory profile failed: ", err)
|
if f, err := os.Create(filename); err != nil {
|
||||||
|
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
|
||||||
|
log.Fatal("record heap profile failed: ", err)
|
||||||
} else {
|
} else {
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
pprof.WriteHeapProfile(f)
|
pprof.WriteHeapProfile(f)
|
||||||
f.Close()
|
f.Close()
|
||||||
|
fmt.Fprintf(w, "create heap profile %s \n", filename)
|
||||||
|
fmt.Fprintf(w, "Now you can use this to check it: go tool pprof <program> %s\n", filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// start cpu profile monitor
|
// start cpu profile monitor
|
||||||
func StartCPUProfile() {
|
func GetCPUProfile(rw http.ResponseWriter) {
|
||||||
f, err := os.Create("cpu-" + strconv.Itoa(pid) + ".pprof")
|
sec := 30
|
||||||
|
rw.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
|
||||||
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(rw, "Could not enable CPU profiling: %s\n", err)
|
||||||
|
log.Fatal("record cpu profile failed: ", err)
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(rw, "start cpu profileing\n")
|
||||||
pprof.StartCPUProfile(f)
|
pprof.StartCPUProfile(f)
|
||||||
}
|
time.Sleep(time.Duration(sec) * time.Second)
|
||||||
|
|
||||||
// stop cpu profile monitor
|
|
||||||
func StopCPUProfile() {
|
|
||||||
pprof.StopCPUProfile()
|
pprof.StopCPUProfile()
|
||||||
|
fmt.Fprintf(rw, "create cpu profile %s \n", filename)
|
||||||
|
fmt.Fprintf(rw, "Now you can use this to check it: go tool pprof <program> %s\n", filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// print gc information to io.Writer
|
// print gc information to io.Writer
|
||||||
|
Loading…
Reference in New Issue
Block a user