-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmeasure_test.go
157 lines (148 loc) · 3.54 KB
/
measure_test.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var measureCmpOpts = cmp.AllowUnexported(collectedMeasurements{}, measurementSummary{}, entrySummary{})
func TestCollectMeasurementsOK(t *testing.T) {
for _, path := range []string{"vim", "nvim"} {
t.Run(path, func(t *testing.T) {
opts := &options{
count: 2,
vimPath: path,
warmup: 1,
extraArgs: []string{"-N", "-u", "NONE"},
}
collected, err := collectMeasurements(opts)
if err != nil {
t.Fatal(err)
}
if len(collected.total) != 2 {
t.Error("2 total times should be collected but", len(collected.total))
}
if len(collected.entries) == 0 {
t.Error("Collected entries are empty")
}
for s, ds := range collected.entries {
if len(ds) < 2 {
t.Error("Source time for", s, " should be collected twice but", ds)
}
}
})
}
}
func TestCollectMeasurementsVimStartError(t *testing.T) {
for _, path := range []string{"vim", "nvim"} {
t.Run(path, func(t *testing.T) {
opts := &options{count: 2, vimPath: path, extraArgs: []string{"--foo"}}
_, err := collectMeasurements(opts)
if err == nil {
t.Fatal("No error occurred")
}
if !strings.Contains(err.Error(), "failed to run \""+path+"\" with args [--foo") {
t.Fatal("Unexpected error:", err.Error())
}
})
}
}
func TestCollectMeasurementsVimWarmupError(t *testing.T) {
opts := &options{warmup: 1, count: 1, vimPath: "vim", extraArgs: []string{"--foo"}}
_, err := collectMeasurements(opts)
if err == nil {
t.Fatal("No error occurred")
}
if !strings.Contains(err.Error(), "error while warm-up") {
t.Fatal("Unexpected error:", err.Error())
}
}
func TestSummarizeStartuptime(t *testing.T) {
collected := &collectedMeasurements{
total: []time.Duration{
time.Duration(10000),
time.Duration(20000),
time.Duration(30000),
},
entries: map[string][]time.Duration{
"/foo/bar": {
time.Duration(110),
time.Duration(130),
time.Duration(120),
},
"$VIM/vimrc": {
time.Duration(1500),
time.Duration(1400),
time.Duration(1300),
},
},
}
want := &measurementSummary{
total: entrySummary{
"Total",
time.Duration(20000),
time.Duration(30000),
time.Duration(10000),
},
sortedEntries: []entrySummary{
{
"$VIM/vimrc",
time.Duration(1400),
time.Duration(1500),
time.Duration(1300),
},
{
"/foo/bar",
time.Duration(120),
time.Duration(130),
time.Duration(110),
},
},
}
have, err := summarizeStartuptime(collected, false)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, have, measureCmpOpts) {
t.Fatal(cmp.Diff(want, have, measureCmpOpts))
}
}
func TestSummarizeStartuptimeError(t *testing.T) {
for _, tc := range []struct {
what string
collected *collectedMeasurements
msg string
}{
{
"no total time",
&collectedMeasurements{
total: []time.Duration{},
entries: map[string][]time.Duration{
"/foo/bar": {time.Duration(110)},
},
},
"no total time was collected",
},
{
"no entry profile result",
&collectedMeasurements{
total: []time.Duration{time.Duration(110)},
entries: map[string][]time.Duration{
"$VIM/vimrc": {},
},
},
"no profile was collected for '$VIM/vimrc'",
},
} {
t.Run(tc.what, func(t *testing.T) {
_, err := summarizeStartuptime(tc.collected, false)
if err == nil {
t.Fatal("Error should happen")
}
msg := err.Error()
if !strings.Contains(msg, tc.msg) {
t.Fatalf("Unexpected error '%s', it should contain '%s'", msg, tc.msg)
}
})
}
}