/
magic3.go
42 lines (34 loc) · 893 Bytes
/
magic3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"bytes"
"fmt"
"runtime/pprof"
"github.com/google/pprof/driver"
"github.com/sudachen/playground/misc/ppftool"
"io/ioutil"
"os"
)
func main() {
var bf bytes.Buffer
pprof.StartCPUProfile(&bf)
for s := ""; len(s) < 100000; {
s = s + fmt.Sprintf("%d", len(s))
}
pprof.StopCPUProfile()
tempfile := "pprof.output.txt"
unit := ppftool.DefaultUnit
rpt := &ppftool.Report{Unit: unit}
driver.PProf(&driver.Options{
Fetch: ppftool.Fetcher(bf.Bytes()),
Flagset: ppftool.Flagset("-top", "-nodecount=5", "-unit="+unit.String(), "-output="+tempfile),
UI: ppftool.FakeUi(),
})
if b, err := ioutil.ReadFile(tempfile); err == nil {
rpt.WriteTop(b)
os.Remove(tempfile)
}
fmt.Printf("%10s %11s %s\n", "flat", "%flat", "function")
for _, row := range rpt.Rows {
fmt.Printf("%10.3f %10.3f%% %s\n", row.Flat, row.FlatPercent, row.Function)
}
}