3969cd3b by astaxie

toolbox: improve the profile

1 parent f4867aad
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
13 "fmt" 13 "fmt"
14 "io" 14 "io"
15 "log" 15 "log"
16 "net/http"
16 "os" 17 "os"
17 "runtime" 18 "runtime"
18 "runtime/debug" 19 "runtime/debug"
...@@ -43,40 +44,48 @@ func ProcessInput(input string, w io.Writer) { ...@@ -43,40 +44,48 @@ func ProcessInput(input string, w io.Writer) {
43 case "lookup block": 44 case "lookup block":
44 p := pprof.Lookup("block") 45 p := pprof.Lookup("block")
45 p.WriteTo(w, 2) 46 p.WriteTo(w, 2)
46 case "start cpuprof": 47 case "get cpuprof":
47 StartCPUProfile() 48 GetCPUProfile(w.(http.ResponseWriter))
48 case "stop cpuprof":
49 StopCPUProfile()
50 case "get memprof": 49 case "get memprof":
51 MemProf() 50 MemProf(w)
52 case "gc summary": 51 case "gc summary":
53 PrintGCSummary(w) 52 PrintGCSummary(w)
54 } 53 }
55 } 54 }
56 55
57 // record memory profile in pprof 56 // record memory profile in pprof
58 func MemProf() { 57 func MemProf(w io.Writer) {
59 if f, err := os.Create("mem-" + strconv.Itoa(pid) + ".memprof"); err != nil { 58 filename := "mem-" + strconv.Itoa(pid) + ".memprof"
60 log.Fatal("record memory profile failed: ", err) 59 if f, err := os.Create(filename); err != nil {
60 fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
61 log.Fatal("record heap profile failed: ", err)
61 } else { 62 } else {
62 runtime.GC() 63 runtime.GC()
63 pprof.WriteHeapProfile(f) 64 pprof.WriteHeapProfile(f)
64 f.Close() 65 f.Close()
66 fmt.Fprintf(w, "create heap profile %s \n", filename)
67 fmt.Fprintf(w, "Now you can use this to check it: go tool pprof <program> %s\n", filename)
65 } 68 }
66 } 69 }
67 70
68 // start cpu profile monitor 71 // start cpu profile monitor
69 func StartCPUProfile() { 72 func GetCPUProfile(rw http.ResponseWriter) {
70 f, err := os.Create("cpu-" + strconv.Itoa(pid) + ".pprof") 73 sec := 30
74 rw.Header().Set("Content-Type", "application/octet-stream")
75 filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
76 f, err := os.Create(filename)
71 if err != nil { 77 if err != nil {
72 log.Fatal(err) 78 rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
79 rw.WriteHeader(http.StatusInternalServerError)
80 fmt.Fprintf(rw, "Could not enable CPU profiling: %s\n", err)
81 log.Fatal("record cpu profile failed: ", err)
73 } 82 }
83 fmt.Fprintf(rw, "start cpu profileing\n")
74 pprof.StartCPUProfile(f) 84 pprof.StartCPUProfile(f)
75 } 85 time.Sleep(time.Duration(sec) * time.Second)
76
77 // stop cpu profile monitor
78 func StopCPUProfile() {
79 pprof.StopCPUProfile() 86 pprof.StopCPUProfile()
87 fmt.Fprintf(rw, "create cpu profile %s \n", filename)
88 fmt.Fprintf(rw, "Now you can use this to check it: go tool pprof <program> %s\n", filename)
80 } 89 }
81 90
82 // print gc information to io.Writer 91 // print gc information to io.Writer
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!