Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
9776bb8a
Commit
9776bb8a
authored
Nov 15, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve the admin module
parent
969464f8
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
23 deletions
+37
-23
admin.go
admin.go
+1
-1
profile.go
admin/profile.go
+12
-12
profile_test.go
admin/profile_test.go
+14
-0
statistics.go
admin/statistics.go
+3
-3
statistics_test.go
admin/statistics_test.go
+7
-7
No files found.
admin.go
View file @
9776bb8a
...
@@ -46,7 +46,7 @@ func ProfIndex(rw http.ResponseWriter, r *http.Request) {
...
@@ -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'"
))
}
}
...
...
admin/profile.go
View file @
9776bb8a
...
@@ -2,6 +2,7 @@ package admin
...
@@ -2,6 +2,7 @@ package admin
import
(
import
(
"fmt"
"fmt"
"io"
"log"
"log"
"os"
"os"
"runtime"
"runtime"
...
@@ -11,7 +12,6 @@ import (
...
@@ -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() {
...
@@ -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) {
...
@@ -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() {
...
@@ -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) {
...
@@ -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) {
...
@@ -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
)))
...
...
admin/profile_test.go
0 → 100644
View file @
9776bb8a
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
)
}
admin/statistics.go
View file @
9776bb8a
...
@@ -64,11 +64,11 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
...
@@ -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
))
,
)
)
}
}
}
}
...
...
admin/statistics_test.go
View file @
9776bb8a
...
@@ -7,12 +7,12 @@ import (
...
@@ -7,12 +7,12 @@ import (
)
)
func
TestStatics
(
t
*
testing
.
T
)
{
func
TestStatics
(
t
*
testing
.
T
)
{
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1000
000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
2
000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
120000
0
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
120000
))
StatisticsMap
.
AddStatistics
(
"GET"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
13000
00
))
StatisticsMap
.
AddStatistics
(
"GET"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
13000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/admin"
,
"&admin.user"
,
time
.
Duration
(
14000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/admin"
,
"&admin.user"
,
time
.
Duration
(
14000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/astaxie"
,
"&admin.user"
,
time
.
Duration
(
12000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/astaxie"
,
"&admin.user"
,
time
.
Duration
(
12000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/xiemengjun"
,
"&admin.user"
,
time
.
Duration
(
13000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/xiemengjun"
,
"&admin.user"
,
time
.
Duration
(
13000
))
StatisticsMap
.
AddStatistics
(
"DELETE"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1400
000
))
StatisticsMap
.
AddStatistics
(
"DELETE"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1400
))
StatisticsMap
.
GetMap
(
os
.
Stdout
)
StatisticsMap
.
GetMap
(
os
.
Stdout
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment