-
Notifications
You must be signed in to change notification settings - Fork 3
/
statistics_pre37_test.go
365 lines (338 loc) · 9.05 KB
/
statistics_pre37_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2018-2022 VirtualTam.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package ccache
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/alecthomas/units"
)
type pre37TestCase struct {
tname string
inputFilename string
wantStats Statistics
wantErr error
}
type pre37TestSession struct {
osAndVersion string
osAndVersionCode string
ccacheVersion string
testCases []pre37TestCase
}
func TestParsePre37Statistics(t *testing.T) {
sessions := []pre37TestSession{
{
osAndVersion: "Arch Linux",
osAndVersionCode: "arch-rolling",
ccacheVersion: "3.4.3",
testCases: []pre37TestCase{
{
tname: "empty cache",
inputFilename: "empty",
wantStats: Statistics{
CacheSize: "0.0 kB",
},
},
{
tname: "first build",
inputFilename: "firstbuild",
wantStats: Statistics{
CacheMiss: 116,
CalledForLink: 14,
CalledForPreprocessing: 85,
UnsupportedCodeDirective: 2,
NoInputFile: 29,
FilesInCache: 361,
CacheSize: "6.4 MB",
CacheSizeBytes: units.MetricBytes(6400000),
},
},
{
tname: "second build",
inputFilename: "secondbuild",
wantStats: Statistics{
CacheHitDirect: 73,
CacheHitPreprocessed: 4,
CacheMiss: 207,
CacheHitRate: 27.11,
CacheHitRatio: 0.2711,
CalledForLink: 28,
CalledForPreprocessing: 170,
UnsupportedCodeDirective: 4,
NoInputFile: 58,
FilesInCache: 639,
CacheSize: "12.1 MB",
CacheSizeBytes: units.MetricBytes(12100000),
},
},
},
},
{
osAndVersion: "Arch Linux",
osAndVersionCode: "arch-rolling",
ccacheVersion: "3.5",
testCases: []pre37TestCase{
{
tname: "empty cache",
inputFilename: "empty",
wantStats: Statistics{
CacheSize: "0.0 kB",
},
},
{
tname: "first build",
inputFilename: "firstbuild",
wantStats: Statistics{
CacheHitDirect: 1,
CacheHitPreprocessed: 15,
CacheMiss: 342,
CacheHitRate: 4.47,
CacheHitRatio: 0.0447,
CalledForLink: 14,
CalledForPreprocessing: 1,
FilesInCache: 867,
CacheSize: "44.5 MB",
CacheSizeBytes: units.MetricBytes(44500000),
},
},
{
tname: "second build",
inputFilename: "secondbuild",
wantStats: Statistics{
CacheHitDirect: 349,
CacheHitPreprocessed: 10,
CacheMiss: 28,
CacheHitRate: 92.76,
CacheHitRatio: 0.9276000000000001,
CalledForLink: 14,
CalledForPreprocessing: 1,
FilesInCache: 943,
CacheSize: "46.7 MB",
CacheSizeBytes: units.MetricBytes(46700000),
},
},
},
},
{
osAndVersion: "Debian 9",
osAndVersionCode: "debian-9",
ccacheVersion: "3.3.4",
testCases: []pre37TestCase{
{
tname: "empty cache",
inputFilename: "empty",
wantStats: Statistics{
CacheSize: "0.0 kB",
},
},
{
tname: "first build",
inputFilename: "firstbuild",
wantStats: Statistics{
CacheMiss: 52,
CalledForLink: 1,
CalledForPreprocessing: 11,
NoInputFile: 7,
UnsupportedCodeDirective: 1,
FilesInCache: 103,
CacheSize: "1.2 MB",
CacheSizeBytes: units.MetricBytes(1200000),
},
},
{
tname: "second build",
inputFilename: "secondbuild",
wantStats: Statistics{
CacheHitDirect: 50,
CacheHitPreprocessed: 2,
CacheMiss: 52,
CacheHitRate: 50.0,
CacheHitRatio: 0.5,
CalledForLink: 2,
CalledForPreprocessing: 22,
NoInputFile: 14,
UnsupportedCodeDirective: 2,
FilesInCache: 103,
CacheSize: "1.2 MB",
CacheSizeBytes: units.MetricBytes(1200000),
},
},
},
},
{
osAndVersion: "Debian 10",
osAndVersionCode: "debian-10",
ccacheVersion: "3.6",
testCases: []pre37TestCase{
{
tname: "empty cache",
inputFilename: "empty",
wantStats: Statistics{
CacheSize: "0.0 kB",
},
},
{
tname: "first build",
inputFilename: "firstbuild",
wantStats: Statistics{
CacheMiss: 57,
CalledForLink: 1,
CalledForPreprocessing: 11,
NoInputFile: 4,
FilesInCache: 110,
CacheSize: "1.7 MB",
CacheSizeBytes: units.MetricBytes(1700000),
},
},
{
tname: "second build",
inputFilename: "secondbuild",
wantStats: Statistics{
CacheHitDirect: 52,
CacheHitPreprocessed: 5,
CacheMiss: 57,
CacheHitRate: 50.0,
CacheHitRatio: 0.5,
CalledForLink: 2,
CalledForPreprocessing: 22,
NoInputFile: 8,
FilesInCache: 110,
CacheSize: "1.7 MB",
CacheSizeBytes: units.MetricBytes(1700000),
},
},
},
},
{
osAndVersion: "Ubuntu 18.04",
osAndVersionCode: "ubuntu-18.04",
ccacheVersion: "3.4.1",
testCases: []pre37TestCase{
{
tname: "empty cache",
inputFilename: "empty",
wantStats: Statistics{
CacheSize: "0.0 kB",
},
},
{
tname: "first build",
inputFilename: "firstbuild",
wantStats: Statistics{
CacheMiss: 53,
CalledForLink: 1,
CalledForPreprocessing: 11,
NoInputFile: 4,
FilesInCache: 104,
CacheSize: "1.6 MB",
CacheSizeBytes: units.MetricBytes(1600000),
},
},
{
tname: "second build",
inputFilename: "secondbuild",
wantStats: Statistics{
CacheHitDirect: 50,
CacheHitPreprocessed: 3,
CacheMiss: 53,
CacheHitRate: 50.0,
CacheHitRatio: 0.5,
CalledForLink: 2,
CalledForPreprocessing: 22,
NoInputFile: 8,
FilesInCache: 104,
CacheSize: "1.6 MB",
CacheSizeBytes: units.MetricBytes(1600000),
},
},
},
},
}
for _, ts := range sessions {
for _, tc := range ts.testCases {
t.Run(fmt.Sprintf("ccache %s on %s (%s)", ts.ccacheVersion, ts.osAndVersion, tc.tname), func(t *testing.T) {
inputFilepath := filepath.Join(
"testdata",
fmt.Sprintf("%s-ccache-%s", ts.osAndVersionCode, ts.ccacheVersion),
tc.inputFilename,
)
input, err := os.ReadFile(inputFilepath)
if err != nil {
t.Fatalf("failed to open test input: %q", err)
}
s, err := ParsePre37Statistics(string(input))
if tc.wantErr != nil {
if err == nil {
t.Fatal("expected an error, got none")
} else if err.Error() != tc.wantErr.Error() {
t.Fatalf("want error %q, got %q", tc.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("expected no error, got %q", err)
}
assertStatisticsEqual(t, s, &tc.wantStats)
})
}
}
}
func TestParsePre37StatisticsEdgeCases(t *testing.T) {
cases := []struct {
tname string
input string
wantStats Statistics
wantErr error
}{
// ccache cache size units
{
tname: "cache size in kB",
input: `cache size 16.7 kB
max cache size 57.0 kB
`,
wantStats: Statistics{
CacheSize: "16.7 kB",
CacheSizeBytes: units.MetricBytes(16700),
},
},
// error cases
{
tname: "unexpected date format",
input: `stats zeroed not a date
`,
wantErr: errors.New("parsing time \"not a date\" as \"Mon Jan 2 15:04:05 2006\": cannot parse \"not a date\" as \"Mon\""),
},
{
tname: "unexpected cache size unit",
input: `cache size 655.4 zB
`,
wantErr: errors.New("units: unknown unit ZB in 655.4ZB"),
},
{
tname: "unexpected max cache size unit",
input: `max cache size 10.7 dB
`,
wantErr: errors.New("units: unknown unit DB in 10.7DB"),
},
}
for _, tc := range cases {
t.Run(tc.tname, func(t *testing.T) {
s, err := ParsePre37Statistics(tc.input)
if tc.wantErr != nil {
if err == nil {
t.Fatal("expected an error, got none")
} else if err.Error() != tc.wantErr.Error() {
t.Fatalf("want error %q, got %q", tc.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("expected no error, got %q", err)
}
assertStatisticsEqual(t, s, &tc.wantStats)
})
}
}