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
3969cd3b
Commit
3969cd3b
authored
Aug 15, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
toolbox: improve the profile
parent
f4867aad
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
15 deletions
+24
-15
profile.go
toolbox/profile.go
+24
-15
No files found.
toolbox/profile.go
View file @
3969cd3b
...
...
@@ -13,6 +13,7 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"runtime/debug"
...
...
@@ -43,40 +44,48 @@ func ProcessInput(input string, w io.Writer) {
case
"lookup block"
:
p
:=
pprof
.
Lookup
(
"block"
)
p
.
WriteTo
(
w
,
2
)
case
"start cpuprof"
:
StartCPUProfile
()
case
"stop cpuprof"
:
StopCPUProfile
()
case
"get cpuprof"
:
GetCPUProfile
(
w
.
(
http
.
ResponseWriter
))
case
"get memprof"
:
MemProf
()
MemProf
(
w
)
case
"gc summary"
:
PrintGCSummary
(
w
)
}
}
// record memory profile in pprof
func
MemProf
()
{
if
f
,
err
:=
os
.
Create
(
"mem-"
+
strconv
.
Itoa
(
pid
)
+
".memprof"
);
err
!=
nil
{
log
.
Fatal
(
"record memory profile failed: "
,
err
)
func
MemProf
(
w
io
.
Writer
)
{
filename
:=
"mem-"
+
strconv
.
Itoa
(
pid
)
+
".memprof"
if
f
,
err
:=
os
.
Create
(
filename
);
err
!=
nil
{
fmt
.
Fprintf
(
w
,
"create file %s error %s
\n
"
,
filename
,
err
.
Error
())
log
.
Fatal
(
"record heap profile failed: "
,
err
)
}
else
{
runtime
.
GC
()
pprof
.
WriteHeapProfile
(
f
)
f
.
Close
()
fmt
.
Fprintf
(
w
,
"create heap profile %s
\n
"
,
filename
)
fmt
.
Fprintf
(
w
,
"Now you can use this to check it: go tool pprof <program> %s
\n
"
,
filename
)
}
}
// start cpu profile monitor
func
StartCPUProfile
()
{
f
,
err
:=
os
.
Create
(
"cpu-"
+
strconv
.
Itoa
(
pid
)
+
".pprof"
)
func
GetCPUProfile
(
rw
http
.
ResponseWriter
)
{
sec
:=
30
rw
.
Header
()
.
Set
(
"Content-Type"
,
"application/octet-stream"
)
filename
:=
"cpu-"
+
strconv
.
Itoa
(
pid
)
+
".pprof"
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
rw
.
Header
()
.
Set
(
"Content-Type"
,
"text/plain; charset=utf-8"
)
rw
.
WriteHeader
(
http
.
StatusInternalServerError
)
fmt
.
Fprintf
(
rw
,
"Could not enable CPU profiling: %s
\n
"
,
err
)
log
.
Fatal
(
"record cpu profile failed: "
,
err
)
}
fmt
.
Fprintf
(
rw
,
"start cpu profileing
\n
"
)
pprof
.
StartCPUProfile
(
f
)
}
// stop cpu profile monitor
func
StopCPUProfile
()
{
time
.
Sleep
(
time
.
Duration
(
sec
)
*
time
.
Second
)
pprof
.
StopCPUProfile
()
fmt
.
Fprintf
(
rw
,
"create cpu profile %s
\n
"
,
filename
)
fmt
.
Fprintf
(
rw
,
"Now you can use this to check it: go tool pprof <program> %s
\n
"
,
filename
)
}
// print gc information to io.Writer
...
...
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