-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathparse.go
More file actions
108 lines (92 loc) · 2.37 KB
/
parse.go
File metadata and controls
108 lines (92 loc) · 2.37 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func parseErrorAt(lineno uint, format string, args ...interface{}) error {
s := fmt.Sprintf("parse error at line:%d: %s", lineno, format)
return fmt.Errorf(s, args...)
}
func parseDuration(s string, lineno uint) (time.Duration, error) {
s = strings.TrimSuffix(s, ":")
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return time.Duration(0), parseErrorAt(lineno, "cannot parse field '%s' as millisec duration", s)
}
return time.Duration(f*1000) * time.Microsecond, nil
}
func parseStartuptimeEntity(line string, lineno uint) (*measurementEntry, error) {
e := &measurementEntry{}
ss := strings.Fields(line)
if len(ss) <= 2 {
return nil, parseErrorAt(lineno, "lack of fields: '%s'", line)
}
d, err := parseDuration(ss[0], lineno)
if err != nil {
return nil, err
}
e.elapsed = d
d, err = parseDuration(ss[1], lineno)
if err != nil {
return nil, err
}
e.total = d
if strings.HasSuffix(ss[1], ":") {
e.script = false
e.self = time.Duration(0)
e.name = strings.Join(ss[2:], " ")
return e, nil
}
e.script = true
if len(ss) < 4 {
return nil, parseErrorAt(lineno, "failed to parse script measurement line '%s'. too few fields", line)
}
d, err = parseDuration(ss[2], lineno)
if err != nil {
return nil, err
}
e.self = d
if ss[3] == "sourcing" {
e.name = strings.Join(ss[4:], " ")
if e.name == "" {
return nil, parseErrorAt(lineno, "failed to parse script measurement line '%s'. script name is missing", line)
}
} else if strings.HasPrefix(ss[3], "require(") {
e.name = strings.Join(ss[3:], " ")
} else {
return nil, parseErrorAt(lineno, "'sourcing' token or 'require(...)' token is expected but got '%s'", ss[3])
}
return e, nil
}
func parseStartuptime(file *os.File) (*measurement, error) {
m := &measurement{}
s := bufio.NewScanner(file)
l := uint(1)
for s.Scan() {
if l < 7 {
// Skip header
l++
continue
}
t := s.Text()
if t == "" {
// Neovim appends an extra empty line at the end of input (#4)
continue
}
e, err := parseStartuptimeEntity(t, l)
if err != nil {
return nil, err
}
m.entries = append(m.entries, e)
l++
}
if len(m.entries) == 0 {
return nil, fmt.Errorf("broken --startuptime output while parsing file %s. no entry was parsed", file.Name())
}
m.elapsedTotal = m.entries[len(m.entries)-1].elapsed
return m, nil
}