本文整理汇总了Golang中runtime/pprof.Profile.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:Golang Profile.WriteTo方法的具体用法?Golang Profile.WriteTo怎么用?Golang Profile.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类runtime/pprof.Profile
的用法示例。
在下文中一共展示了Profile.WriteTo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: profileRead
// profileRead reads from a Profile.
func profileRead(p *pprof.Profile, debug int) func(context.Context) ([]byte, time.Time, error) {
return func(_ context.Context) ([]byte, time.Time, error) {
var b bytes.Buffer
err := p.WriteTo(&b, debug)
if err != nil {
return nil, time.Time{}, err
}
return b.Bytes(), time.Now(), nil
}
}
示例2: init
func init() {
go func() {
for {
if input, err := ioutil.ReadFile("unitest.cmd"); err == nil && len(input) > 0 {
ioutil.WriteFile("unitest.cmd", []byte(""), 0744)
cmd := strings.Trim(string(input), " \n\r\t")
var (
profile *pprof.Profile
filename string
)
switch cmd {
case "lookup goroutine":
profile = pprof.Lookup("goroutine")
filename = "unitest.goroutine"
case "lookup heap":
profile = pprof.Lookup("heap")
filename = "unitest.heap"
case "lookup threadcreate":
profile = pprof.Lookup("threadcreate")
filename = "unitest.thread"
default:
if CommandHandler == nil || !CommandHandler(cmd) {
println("unknow command: '" + cmd + "'")
}
}
if profile != nil {
file, err := os.Create(filename)
if err != nil {
println("couldn't create " + filename)
} else {
profile.WriteTo(file, 2)
}
}
}
time.Sleep(2 * time.Second)
}
}()
}
示例3: saveBlockingProfiles
func saveBlockingProfiles(profiler *pprof.Profile) {
runtime.SetBlockProfileRate(1)
t0 := time.Now()
for t := range time.NewTicker(20 * time.Second).C {
startms := int(t.Sub(t0).Seconds() * 1000)
fd, err := os.Create(fmt.Sprintf("block-%05d-%07d.pprof", syscall.Getpid(), startms))
if err != nil {
panic(err)
}
err = profiler.WriteTo(fd, 0)
if err != nil {
panic(err)
}
err = fd.Close()
if err != nil {
panic(err)
}
}
}
示例4: init
func init() {
// traceDispose = true
rand.Seed(time.Now().UnixNano())
go func() {
for {
input, err := ioutil.ReadFile("test.cmd")
if err == nil && len(input) > 0 {
ioutil.WriteFile("test.cmd", []byte(""), 0744)
cmd := strings.Trim(string(input), " \n\r\t")
var p *pprof.Profile
switch cmd {
case "lookup goroutine":
p = pprof.Lookup("goroutine")
case "lookup heap":
p = pprof.Lookup("heap")
case "lookup threadcreate":
p = pprof.Lookup("threadcreate")
default:
println("unknow command: '" + cmd + "'")
}
if p != nil {
file, err := os.Create("test.out")
if err != nil {
println("couldn't create test.out")
} else {
p.WriteTo(file, 2)
}
}
}
time.Sleep(2 * time.Second)
}
}()
}
示例5: run
func (c *CommandProf) run(args []string) string {
if len(args) == 0 {
return c.usage()
}
var (
p *pprof.Profile
fn string
)
switch args[0] {
case "goroutine":
p = pprof.Lookup("goroutine")
fn = profileName() + ".gprof"
case "heap":
p = pprof.Lookup("heap")
fn = profileName() + ".hprof"
case "thread":
p = pprof.Lookup("threadcreate")
fn = profileName() + ".tprof"
case "block":
p = pprof.Lookup("block")
fn = profileName() + ".bprof"
default:
return c.usage()
}
f, err := os.Create(fn)
if err != nil {
return err.Error()
}
defer f.Close()
err = p.WriteTo(f, 0)
if err != nil {
return err.Error()
}
return fn
}
示例6: run
func (c *CommandProf) run(args []string) string {
if len(args) == 0 { //参数为0
return c.usage() //返回用法信息
}
var (
p *pprof.Profile
fn string
)
switch args[0] { //识别命令
case "goroutine":
p = pprof.Lookup("goroutine") //查找goroutine的profile
fn = profileName() + ".gprof" //文件名
case "heap":
p = pprof.Lookup("heap") //查找heap的profile
fn = profileName() + ".hprof" //文件名
case "thread":
p = pprof.Lookup("threadcreate") //查找threadcreate的profile
fn = profileName() + ".tprof" //文件名
case "block":
p = pprof.Lookup("block") //查找block的profile
fn = profileName() + ".bprof" //文件名
default: //未识别命令
return c.usage() //返回用法信息
}
f, err := os.Create(fn) //创建文件
if err != nil { //出错
return err.Error() //返回错误信息
}
defer f.Close() //延迟关闭文件
err = p.WriteTo(f, 0) //写入文件
if err != nil { //写入文件出错
return err.Error() //返回出错信息
}
return fn //返回文件名
}