-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathstats_test.go
287 lines (232 loc) · 8.56 KB
/
stats_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
package stats
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStats_Copy(t *testing.T) {
t.Run("stats is nil", func(t *testing.T) {
var stats *QueryStats
copied := stats.Copy()
assert.Nil(t, copied)
})
t.Run("stats is not nil", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddWallTime(time.Second)
copied := stats.Copy()
// value should be the same
assert.Equal(t, time.Second, copied.LoadWallTime())
p1, p2 := &copied, &stats
// address should be different
assert.False(t, p1 == p2)
})
}
func TestStats_WallTime(t *testing.T) {
t.Run("add and load wall time", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddWallTime(time.Second)
stats.AddWallTime(time.Second)
assert.Equal(t, 2*time.Second, stats.LoadWallTime())
})
t.Run("add and load wall time nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddWallTime(time.Second)
assert.Equal(t, time.Duration(0), stats.LoadWallTime())
})
}
func TestStats_AddFetchedSeries(t *testing.T) {
t.Parallel()
t.Run("add and load series", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddFetchedSeries(100)
stats.AddFetchedSeries(50)
assert.Equal(t, uint64(150), stats.LoadFetchedSeries())
})
t.Run("add and load series nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddFetchedSeries(50)
assert.Equal(t, uint64(0), stats.LoadFetchedSeries())
})
}
func TestQueryStats_AddExtraFields(t *testing.T) {
t.Parallel()
t.Run("add and load extra fields", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddExtraFields("a", "b")
stats.AddExtraFields("c")
checkExtraFields(t, []interface{}{"a", "b", "c", ""}, stats.LoadExtraFields())
})
t.Run("add and load extra fields nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddExtraFields("a", "b")
checkExtraFields(t, []interface{}{}, stats.LoadExtraFields())
})
}
func TestStats_AddFetchedChunkBytes(t *testing.T) {
t.Parallel()
t.Run("add and load bytes", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddFetchedChunkBytes(4096)
stats.AddFetchedChunkBytes(4096)
assert.Equal(t, uint64(8192), stats.LoadFetchedChunkBytes())
})
t.Run("add and load bytes nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddFetchedChunkBytes(1024)
assert.Equal(t, uint64(0), stats.LoadFetchedChunkBytes())
})
}
func TestStats_AddFetchedDataBytes(t *testing.T) {
t.Parallel()
t.Run("add and load bytes", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddFetchedDataBytes(4096)
stats.AddFetchedDataBytes(4096)
assert.Equal(t, uint64(8192), stats.LoadFetchedDataBytes())
})
t.Run("add and load bytes nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddFetchedDataBytes(1024)
assert.Equal(t, uint64(0), stats.LoadFetchedDataBytes())
})
}
func TestStats_AddFetchedChunks(t *testing.T) {
t.Parallel()
t.Run("add and load chunks", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddFetchedChunks(4096)
stats.AddFetchedChunks(4096)
assert.Equal(t, uint64(8192), stats.LoadFetchedChunks())
})
t.Run("add and load chunks nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddFetchedChunks(1024)
assert.Equal(t, uint64(0), stats.LoadFetchedChunks())
})
}
func TestStats_AddFetchedSamples(t *testing.T) {
t.Parallel()
t.Run("add and load samples", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddFetchedSamples(4096)
stats.AddFetchedSamples(4096)
assert.Equal(t, uint64(8192), stats.LoadFetchedSamples())
})
t.Run("add and load samples nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddFetchedSamples(1024)
assert.Equal(t, uint64(0), stats.LoadFetchedSamples())
})
}
func TestStats_AddStoreGatewayTouchedPostings(t *testing.T) {
t.Parallel()
t.Run("add and load touched postings", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddStoreGatewayTouchedPostings(4096)
stats.AddStoreGatewayTouchedPostings(4096)
assert.Equal(t, uint64(8192), stats.LoadStoreGatewayTouchedPostings())
})
t.Run("add and load touched postings nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddStoreGatewayTouchedPostings(4096)
assert.Equal(t, uint64(0), stats.LoadStoreGatewayTouchedPostings())
})
}
func TestStats_AddStoreGatewayTouchedPostingBytes(t *testing.T) {
t.Parallel()
t.Run("add and load touched postings bytes", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddStoreGatewayTouchedPostingBytes(4096)
stats.AddStoreGatewayTouchedPostingBytes(4096)
assert.Equal(t, uint64(8192), stats.LoadStoreGatewayTouchedPostingBytes())
})
t.Run("add and load touched posting bytes nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddStoreGatewayTouchedPostingBytes(4096)
assert.Equal(t, uint64(0), stats.LoadStoreGatewayTouchedPostingBytes())
})
}
func TestStats_StorageWallTime(t *testing.T) {
t.Run("add and load query storage wall time", func(t *testing.T) {
stats, _ := ContextWithEmptyStats(context.Background())
stats.AddQueryStorageWallTime(time.Second)
stats.AddQueryStorageWallTime(time.Second)
assert.Equal(t, 2*time.Second, stats.LoadQueryStorageWallTime())
})
t.Run("add and load query storage wall time nil receiver", func(t *testing.T) {
var stats *QueryStats
stats.AddQueryStorageWallTime(time.Second)
assert.Equal(t, time.Duration(0), stats.LoadQueryStorageWallTime())
})
}
func TestStats_Merge(t *testing.T) {
t.Parallel()
t.Run("merge two stats objects", func(t *testing.T) {
stats1 := &QueryStats{}
stats1.AddWallTime(time.Millisecond)
stats1.AddQueryStorageWallTime(2 * time.Second)
stats1.AddFetchedSeries(50)
stats1.AddFetchedChunkBytes(42)
stats1.AddFetchedDataBytes(100)
stats1.AddStoreGatewayTouchedPostings(200)
stats1.AddStoreGatewayTouchedPostingBytes(300)
stats1.AddFetchedChunks(105)
stats1.AddFetchedSamples(109)
stats1.AddScannedSamples(100)
stats1.AddPeakSamples(100)
stats1.StoreMaxNotOptimizedRegexMatchers(2)
stats1.AddExtraFields("a", "b")
stats1.AddExtraFields("a", "b")
stats2 := &QueryStats{}
stats2.AddWallTime(time.Second)
stats2.AddQueryStorageWallTime(3 * time.Second)
stats2.AddFetchedSeries(60)
stats2.AddFetchedChunkBytes(100)
stats2.AddFetchedDataBytes(101)
stats1.AddStoreGatewayTouchedPostings(201)
stats1.AddStoreGatewayTouchedPostingBytes(301)
stats2.AddFetchedChunks(102)
stats2.AddFetchedSamples(103)
stats2.StoreMaxNotOptimizedRegexMatchers(3)
stats2.AddPeakSamples(105)
stats2.AddScannedSamples(105)
stats2.AddExtraFields("c", "d")
stats1.Merge(stats2)
assert.Equal(t, 1001*time.Millisecond, stats1.LoadWallTime())
assert.Equal(t, 5*time.Second, stats1.LoadQueryStorageWallTime())
assert.Equal(t, uint64(110), stats1.LoadFetchedSeries())
assert.Equal(t, uint64(142), stats1.LoadFetchedChunkBytes())
assert.Equal(t, uint64(201), stats1.LoadFetchedDataBytes())
assert.Equal(t, uint64(207), stats1.LoadFetchedChunks())
assert.Equal(t, uint64(212), stats1.LoadFetchedSamples())
assert.Equal(t, uint64(205), stats1.LoadScannedSamples())
assert.Equal(t, uint64(105), stats1.LoadPeakSamples())
assert.Equal(t, uint64(401), stats1.LoadStoreGatewayTouchedPostings())
assert.Equal(t, uint64(601), stats1.LoadStoreGatewayTouchedPostingBytes())
assert.Equal(t, uint64(3), stats1.LoadNotOptimizedRegexMatchers())
checkExtraFields(t, []interface{}{"a", "b", "c", "d"}, stats1.LoadExtraFields())
})
t.Run("merge two nil stats objects", func(t *testing.T) {
var stats1 *QueryStats
var stats2 *QueryStats
stats1.Merge(stats2)
assert.Equal(t, time.Duration(0), stats1.LoadWallTime())
assert.Equal(t, time.Duration(0), stats1.LoadQueryStorageWallTime())
assert.Equal(t, uint64(0), stats1.LoadFetchedSeries())
assert.Equal(t, uint64(0), stats1.LoadFetchedChunkBytes())
assert.Equal(t, uint64(0), stats1.LoadFetchedDataBytes())
checkExtraFields(t, []interface{}{}, stats1.LoadExtraFields())
})
}
func checkExtraFields(t *testing.T, expected, actual []interface{}) {
t.Parallel()
assert.Equal(t, len(expected), len(actual))
expectedMap := map[string]string{}
actualMap := map[string]string{}
for i := 0; i < len(expected); i += 2 {
expectedMap[expected[i].(string)] = expected[i+1].(string)
actualMap[actual[i].(string)] = actual[i+1].(string)
}
assert.Equal(t, expectedMap, actualMap)
}