-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathprofiler.go
More file actions
42 lines (37 loc) · 816 Bytes
/
profiler.go
File metadata and controls
42 lines (37 loc) · 816 Bytes
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 valast
import (
"fmt"
"reflect"
"strings"
"time"
)
type profiler struct {
stack []reflect.Value
invertedStackMessages []string
}
func (p *profiler) push(v reflect.Value) {
if p == nil {
return
}
p.stack = append(p.stack, v)
}
func (p *profiler) pop(startTime time.Time) {
if p == nil {
return
}
d := time.Since(startTime)
v := p.stack[len(p.stack)-1].Interface()
p.stack = p.stack[:len(p.stack)-1]
stackSize := len(p.stack)
msg := fmt.Sprintf("%s%vns: %T\n", strings.Repeat(" ", stackSize), d.Nanoseconds(), v)
p.invertedStackMessages = append(p.invertedStackMessages, msg)
}
func (p *profiler) dump() {
if p == nil {
return
}
fmt.Println("valast: profile")
for i := len(p.invertedStackMessages) - 1; i > 0; i-- {
fmt.Print(p.invertedStackMessages[i])
}
}