| |
| |
| |
|
|
| |
| |
| |
| |
| |
| package benchmark |
|
|
| import ( |
| "fmt" |
| "io" |
| "os" |
| "runtime" |
| "runtime/pprof" |
| "time" |
| "unicode" |
| ) |
|
|
| type Flags int |
|
|
| const ( |
| GC = 1 << iota |
| NoGC Flags = 0 |
| ) |
|
|
| type Metrics struct { |
| gc Flags |
| marks []*mark |
| curMark *mark |
| filebase string |
| pprofFile *os.File |
| } |
|
|
| type mark struct { |
| name string |
| startM, endM, gcM runtime.MemStats |
| startT, endT time.Time |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func New(gc Flags, filebase string) *Metrics { |
| if gc == GC { |
| runtime.GC() |
| } |
| return &Metrics{gc: gc, filebase: filebase} |
| } |
|
|
| |
| |
| func (m *Metrics) Report(w io.Writer) { |
| if m == nil { |
| return |
| } |
|
|
| m.closeMark() |
|
|
| gcString := "" |
| if m.gc == GC { |
| gcString = "_GC" |
| } |
|
|
| var totTime time.Duration |
| for _, curMark := range m.marks { |
| dur := curMark.endT.Sub(curMark.startT) |
| totTime += dur |
| fmt.Fprintf(w, "%s 1 %d ns/op", makeBenchString(curMark.name+gcString), dur.Nanoseconds()) |
| fmt.Fprintf(w, "\t%d B/op", curMark.endM.TotalAlloc-curMark.startM.TotalAlloc) |
| fmt.Fprintf(w, "\t%d allocs/op", curMark.endM.Mallocs-curMark.startM.Mallocs) |
| if m.gc == GC { |
| fmt.Fprintf(w, "\t%d live-B", curMark.gcM.HeapAlloc) |
| } else { |
| fmt.Fprintf(w, "\t%d heap-B", curMark.endM.HeapAlloc) |
| } |
| fmt.Fprintf(w, "\n") |
| } |
| fmt.Fprintf(w, "%s 1 %d ns/op\n", makeBenchString("total time"+gcString), totTime.Nanoseconds()) |
| } |
|
|
| |
| |
| func (m *Metrics) Start(name string) { |
| if m == nil { |
| return |
| } |
| m.closeMark() |
| m.curMark = &mark{name: name} |
| |
| if m.shouldPProf() { |
| f, err := os.Create(makePProfFilename(m.filebase, name, "cpuprof")) |
| if err != nil { |
| panic(err) |
| } |
| m.pprofFile = f |
| if err = pprof.StartCPUProfile(m.pprofFile); err != nil { |
| panic(err) |
| } |
| } |
| runtime.ReadMemStats(&m.curMark.startM) |
| m.curMark.startT = time.Now() |
| } |
|
|
| func (m *Metrics) closeMark() { |
| if m == nil || m.curMark == nil { |
| return |
| } |
| m.curMark.endT = time.Now() |
| if m.shouldPProf() { |
| pprof.StopCPUProfile() |
| m.pprofFile.Close() |
| m.pprofFile = nil |
| } |
| runtime.ReadMemStats(&m.curMark.endM) |
| if m.gc == GC { |
| runtime.GC() |
| runtime.ReadMemStats(&m.curMark.gcM) |
| if m.shouldPProf() { |
| |
| |
| |
| |
| runtime.GC() |
| f, err := os.Create(makePProfFilename(m.filebase, m.curMark.name, "memprof")) |
| if err != nil { |
| panic(err) |
| } |
| err = pprof.WriteHeapProfile(f) |
| if err != nil { |
| panic(err) |
| } |
| err = f.Close() |
| if err != nil { |
| panic(err) |
| } |
| } |
| } |
| m.marks = append(m.marks, m.curMark) |
| m.curMark = nil |
| } |
|
|
| |
| func (m *Metrics) shouldPProf() bool { |
| return m != nil && len(m.filebase) > 0 |
| } |
|
|
| |
| func makeBenchString(name string) string { |
| needCap := true |
| ret := []rune("Benchmark") |
| for _, r := range name { |
| if unicode.IsSpace(r) { |
| needCap = true |
| continue |
| } |
| if needCap { |
| r = unicode.ToUpper(r) |
| needCap = false |
| } |
| ret = append(ret, r) |
| } |
| return string(ret) |
| } |
|
|
| func makePProfFilename(filebase, name, typ string) string { |
| return fmt.Sprintf("%s_%s.%s", filebase, makeBenchString(name), typ) |
| } |
|
|