1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 08:40:54 +00:00

fix the responseWriter

This commit is contained in:
astaxie 2014-08-15 15:24:46 +08:00
parent b611b9dab6
commit c1234e7c6d

View File

@ -13,7 +13,6 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net/http"
"os" "os"
"path" "path"
"runtime" "runtime"
@ -46,7 +45,7 @@ func ProcessInput(input string, w io.Writer) {
p := pprof.Lookup("block") p := pprof.Lookup("block")
p.WriteTo(w, 2) p.WriteTo(w, 2)
case "get cpuprof": case "get cpuprof":
GetCPUProfile(w.(http.ResponseWriter)) GetCPUProfile(w)
case "get memprof": case "get memprof":
MemProf(w) MemProf(w)
case "gc summary": case "gc summary":
@ -71,24 +70,21 @@ func MemProf(w io.Writer) {
} }
// start cpu profile monitor // start cpu profile monitor
func GetCPUProfile(rw http.ResponseWriter) { func GetCPUProfile(w io.Writer) {
sec := 30 sec := 30
rw.Header().Set("Content-Type", "application/octet-stream")
filename := "cpu-" + strconv.Itoa(pid) + ".pprof" filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
rw.Header().Set("Content-Type", "text/plain; charset=utf-8") fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "Could not enable CPU profiling: %s\n", err)
log.Fatal("record cpu profile failed: ", 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) time.Sleep(time.Duration(sec) * time.Second)
pprof.StopCPUProfile() pprof.StopCPUProfile()
fmt.Fprintf(rw, "create cpu profile %s \n", filename)
fmt.Fprintf(w, "create cpu profile %s \n", filename)
_, fl := path.Split(os.Args[0]) _, fl := path.Split(os.Args[0])
fmt.Fprintf(rw, "Now you can use this to check it: go tool pprof %s %s\n", fl, filename) fmt.Fprintf(w, "Now you can use this to check it: go tool pprof %s %s\n", fl, filename)
} }
// print gc information to io.Writer // print gc information to io.Writer