mirror of
https://github.com/astaxie/beego.git
synced 2025-02-16 23:27:05 +00:00
improve the admin module
This commit is contained in:
parent
969464f8ad
commit
9776bb8a03
2
admin.go
2
admin.go
@ -46,7 +46,7 @@ func ProfIndex(rw http.ResponseWriter, r *http.Request) {
|
|||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
command := r.Form.Get("command")
|
command := r.Form.Get("command")
|
||||||
if command != "" {
|
if command != "" {
|
||||||
admin.ProcessInput(command)
|
admin.ProcessInput(command, rw)
|
||||||
} else {
|
} else {
|
||||||
rw.Write([]byte("request url like '/prof?command=lookup goroutine'"))
|
rw.Write([]byte("request url like '/prof?command=lookup goroutine'"))
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -11,7 +12,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var heapProfileCounter int32
|
|
||||||
var startTime = time.Now()
|
var startTime = time.Now()
|
||||||
var pid int
|
var pid int
|
||||||
|
|
||||||
@ -19,20 +19,20 @@ func init() {
|
|||||||
pid = os.Getpid()
|
pid = os.Getpid()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessInput(input string) {
|
func ProcessInput(input string, w io.Writer) {
|
||||||
switch input {
|
switch input {
|
||||||
case "lookup goroutine":
|
case "lookup goroutine":
|
||||||
p := pprof.Lookup("goroutine")
|
p := pprof.Lookup("goroutine")
|
||||||
p.WriteTo(os.Stdout, 2)
|
p.WriteTo(w, 2)
|
||||||
case "lookup heap":
|
case "lookup heap":
|
||||||
p := pprof.Lookup("heap")
|
p := pprof.Lookup("heap")
|
||||||
p.WriteTo(os.Stdout, 2)
|
p.WriteTo(w, 2)
|
||||||
case "lookup threadcreate":
|
case "lookup threadcreate":
|
||||||
p := pprof.Lookup("threadcreate")
|
p := pprof.Lookup("threadcreate")
|
||||||
p.WriteTo(os.Stdout, 2)
|
p.WriteTo(w, 2)
|
||||||
case "lookup block":
|
case "lookup block":
|
||||||
p := pprof.Lookup("block")
|
p := pprof.Lookup("block")
|
||||||
p.WriteTo(os.Stdout, 2)
|
p.WriteTo(w, 2)
|
||||||
case "start cpuprof":
|
case "start cpuprof":
|
||||||
StartCPUProfile()
|
StartCPUProfile()
|
||||||
case "stop cpuprof":
|
case "stop cpuprof":
|
||||||
@ -40,7 +40,7 @@ func ProcessInput(input string) {
|
|||||||
case "get memprof":
|
case "get memprof":
|
||||||
MemProf()
|
MemProf()
|
||||||
case "gc summary":
|
case "gc summary":
|
||||||
PrintGCSummary()
|
PrintGCSummary(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,16 +66,16 @@ func StopCPUProfile() {
|
|||||||
pprof.StopCPUProfile()
|
pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintGCSummary() {
|
func PrintGCSummary(w io.Writer) {
|
||||||
memStats := &runtime.MemStats{}
|
memStats := &runtime.MemStats{}
|
||||||
runtime.ReadMemStats(memStats)
|
runtime.ReadMemStats(memStats)
|
||||||
gcstats := &debug.GCStats{PauseQuantiles: make([]time.Duration, 100)}
|
gcstats := &debug.GCStats{PauseQuantiles: make([]time.Duration, 100)}
|
||||||
debug.ReadGCStats(gcstats)
|
debug.ReadGCStats(gcstats)
|
||||||
|
|
||||||
printGC(memStats, gcstats)
|
printGC(memStats, gcstats, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
|
func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
|
||||||
|
|
||||||
if gcstats.NumGC > 0 {
|
if gcstats.NumGC > 0 {
|
||||||
lastPause := gcstats.Pause[0]
|
lastPause := gcstats.Pause[0]
|
||||||
@ -83,7 +83,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
|
|||||||
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
|
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
|
||||||
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
|
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
|
||||||
|
|
||||||
fmt.Printf("NumGC:%d Pause:%s Pause(Avg):%s Overhead:%3.2f%% Alloc:%s Sys:%s Alloc(Rate):%s/s Histogram:%s %s %s \n",
|
fmt.Fprintf(w, "NumGC:%d Pause:%s Pause(Avg):%s Overhead:%3.2f%% Alloc:%s Sys:%s Alloc(Rate):%s/s Histogram:%s %s %s \n",
|
||||||
gcstats.NumGC,
|
gcstats.NumGC,
|
||||||
toS(lastPause),
|
toS(lastPause),
|
||||||
toS(avg(gcstats.Pause)),
|
toS(avg(gcstats.Pause)),
|
||||||
@ -99,7 +99,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
|
|||||||
elapsed := time.Now().Sub(startTime)
|
elapsed := time.Now().Sub(startTime)
|
||||||
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
|
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
|
||||||
|
|
||||||
fmt.Printf("Alloc:%s Sys:%s Alloc(Rate):%s/s\n",
|
fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n",
|
||||||
toH(memStats.Alloc),
|
toH(memStats.Alloc),
|
||||||
toH(memStats.Sys),
|
toH(memStats.Sys),
|
||||||
toH(uint64(allocatedRate)))
|
toH(uint64(allocatedRate)))
|
||||||
|
14
admin/profile_test.go
Normal file
14
admin/profile_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProcessInput(t *testing.T) {
|
||||||
|
ProcessInput("lookup goroutine", os.Stdout)
|
||||||
|
ProcessInput("lookup heap", os.Stdout)
|
||||||
|
ProcessInput("lookup threadcreate", os.Stdout)
|
||||||
|
ProcessInput("lookup block", os.Stdout)
|
||||||
|
ProcessInput("gc summary", os.Stdout)
|
||||||
|
}
|
@ -64,11 +64,11 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
|
|||||||
func (m *UrlMap) GetMap(rw io.Writer) {
|
func (m *UrlMap) GetMap(rw io.Writer) {
|
||||||
m.lock.RLock()
|
m.lock.RLock()
|
||||||
defer m.lock.RUnlock()
|
defer m.lock.RUnlock()
|
||||||
fmt.Fprintf(rw, "| % -30s| % -10s | % -16s | % -16s | % -16s | % -16s | % -16s |\n", "requestUrl", "method", "times", "used (s)", "avg used (μs)", "max used (μs)", "min used (μs)")
|
fmt.Fprintf(rw, "| % -50s| % -10s | % -16s | % -16s | % -16s | % -16s | % -16s |\n", "requestUrl", "method", "times", "used", "max used", "min used", "avg used")
|
||||||
for k, v := range m.urlmap {
|
for k, v := range m.urlmap {
|
||||||
for kk, vv := range v {
|
for kk, vv := range v {
|
||||||
fmt.Fprintf(rw, "| % -30s| % -10s | % -16d | % -16f | % -16f | % -16f | % -16f |\n", k,
|
fmt.Fprintf(rw, "| % -50s| % -10s | % -16d | % -16s | % -16s | % -16s | % -16s |\n", k,
|
||||||
kk, vv.RequestNum, vv.TotalTime, vv.MaxTime, vv.MinTime, int64(vv.TotalTime)/vv.RequestNum,
|
kk, vv.RequestNum, toS(vv.TotalTime), toS(vv.MaxTime), toS(vv.MinTime), toS(time.Duration(int64(vv.TotalTime)/vv.RequestNum)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestStatics(t *testing.T) {
|
func TestStatics(t *testing.T) {
|
||||||
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(1000000))
|
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(2000))
|
||||||
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(1200000))
|
StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(120000))
|
||||||
StatisticsMap.AddStatistics("GET", "/api/user", "&admin.user", time.Duration(1300000))
|
StatisticsMap.AddStatistics("GET", "/api/user", "&admin.user", time.Duration(13000))
|
||||||
StatisticsMap.AddStatistics("POST", "/api/admin", "&admin.user", time.Duration(1400000))
|
StatisticsMap.AddStatistics("POST", "/api/admin", "&admin.user", time.Duration(14000))
|
||||||
StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(1200000))
|
StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(12000))
|
||||||
StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(1300000))
|
StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000))
|
||||||
StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400000))
|
StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400))
|
||||||
StatisticsMap.GetMap(os.Stdout)
|
StatisticsMap.GetMap(os.Stdout)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user