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
Hide 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) {
r
.
ParseForm
()
command
:=
r
.
Form
.
Get
(
"command"
)
if
command
!=
""
{
admin
.
ProcessInput
(
command
)
admin
.
ProcessInput
(
command
,
rw
)
}
else
{
rw
.
Write
([]
byte
(
"request url like '/prof?command=lookup goroutine'"
))
}
...
...
admin/profile.go
View file @
9776bb8a
...
...
@@ -2,6 +2,7 @@ package admin
import
(
"fmt"
"io"
"log"
"os"
"runtime"
...
...
@@ -11,7 +12,6 @@ import (
"time"
)
var
heapProfileCounter
int32
var
startTime
=
time
.
Now
()
var
pid
int
...
...
@@ -19,20 +19,20 @@ func init() {
pid
=
os
.
Getpid
()
}
func
ProcessInput
(
input
string
)
{
func
ProcessInput
(
input
string
,
w
io
.
Writer
)
{
switch
input
{
case
"lookup goroutine"
:
p
:=
pprof
.
Lookup
(
"goroutine"
)
p
.
WriteTo
(
os
.
Stdout
,
2
)
p
.
WriteTo
(
w
,
2
)
case
"lookup heap"
:
p
:=
pprof
.
Lookup
(
"heap"
)
p
.
WriteTo
(
os
.
Stdout
,
2
)
p
.
WriteTo
(
w
,
2
)
case
"lookup threadcreate"
:
p
:=
pprof
.
Lookup
(
"threadcreate"
)
p
.
WriteTo
(
os
.
Stdout
,
2
)
p
.
WriteTo
(
w
,
2
)
case
"lookup block"
:
p
:=
pprof
.
Lookup
(
"block"
)
p
.
WriteTo
(
os
.
Stdout
,
2
)
p
.
WriteTo
(
w
,
2
)
case
"start cpuprof"
:
StartCPUProfile
()
case
"stop cpuprof"
:
...
...
@@ -40,7 +40,7 @@ func ProcessInput(input string) {
case
"get memprof"
:
MemProf
()
case
"gc summary"
:
PrintGCSummary
()
PrintGCSummary
(
w
)
}
}
...
...
@@ -66,16 +66,16 @@ func StopCPUProfile() {
pprof
.
StopCPUProfile
()
}
func
PrintGCSummary
()
{
func
PrintGCSummary
(
w
io
.
Writer
)
{
memStats
:=
&
runtime
.
MemStats
{}
runtime
.
ReadMemStats
(
memStats
)
gcstats
:=
&
debug
.
GCStats
{
PauseQuantiles
:
make
([]
time
.
Duration
,
100
)}
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
{
lastPause
:=
gcstats
.
Pause
[
0
]
...
...
@@ -83,7 +83,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
overhead
:=
float64
(
gcstats
.
PauseTotal
)
/
float64
(
elapsed
)
*
100
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
,
toS
(
lastPause
),
toS
(
avg
(
gcstats
.
Pause
)),
...
...
@@ -99,7 +99,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats) {
elapsed
:=
time
.
Now
()
.
Sub
(
startTime
)
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
.
Sys
),
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
func
(
m
*
UrlMap
)
GetMap
(
rw
io
.
Writer
)
{
m
.
lock
.
RLock
()
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
kk
,
vv
:=
range
v
{
fmt
.
Fprintf
(
rw
,
"| % -
30s| % -10s | % -16d | % -16f | % -16f | % -16f | % -16f
|
\n
"
,
k
,
kk
,
vv
.
RequestNum
,
vv
.
TotalTime
,
vv
.
MaxTime
,
vv
.
MinTime
,
int64
(
vv
.
TotalTime
)
/
vv
.
RequestNum
,
fmt
.
Fprintf
(
rw
,
"| % -
50s| % -10s | % -16d | % -16s | % -16s | % -16s | % -16s
|
\n
"
,
k
,
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 (
)
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
(
120000
0
))
StatisticsMap
.
AddStatistics
(
"GET"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
13000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/admin"
,
"&admin.user"
,
time
.
Duration
(
14000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/astaxie"
,
"&admin.user"
,
time
.
Duration
(
12000
00
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/xiemengjun"
,
"&admin.user"
,
time
.
Duration
(
13000
00
))
StatisticsMap
.
AddStatistics
(
"DELETE"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1400
000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
2
000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
120000
))
StatisticsMap
.
AddStatistics
(
"GET"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
13000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/admin"
,
"&admin.user"
,
time
.
Duration
(
14000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/astaxie"
,
"&admin.user"
,
time
.
Duration
(
12000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/xiemengjun"
,
"&admin.user"
,
time
.
Duration
(
13000
))
StatisticsMap
.
AddStatistics
(
"DELETE"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1400
))
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