@@ -4,19 +4,21 @@ import (
4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
- "strconv"
8
- "strings"
9
- "testing"
10
-
11
7
"github.com/efficientgo/core/testutil"
12
8
"github.com/go-kit/log"
13
9
"github.com/oklog/ulid"
14
10
"github.com/prometheus/client_golang/prometheus"
15
11
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
16
12
"github.com/prometheus/prometheus/model/labels"
17
13
"github.com/prometheus/prometheus/storage"
14
+ "github.com/stretchr/testify/require"
18
15
storecache "github.com/thanos-io/thanos/pkg/store/cache"
19
16
"github.com/thanos-io/thanos/pkg/tenancy"
17
+ "math/rand"
18
+ "strconv"
19
+ "strings"
20
+ "testing"
21
+ "time"
20
22
)
21
23
22
24
func TestInMemoryIndexCache_UpdateItem (t * testing.T ) {
@@ -139,3 +141,296 @@ func TestInMemoryIndexCacheSetOverflow(t *testing.T) {
139
141
cache .StoreSeries (id , 2 , []byte (sb .String ()), tenancy .DefaultTenant )
140
142
testutil .Equals (t , float64 (1 ), prom_testutil .ToFloat64 (counter ))
141
143
}
144
+
145
+ //func BenchmarkInMemoryIndexCacheStore(b *testing.B) {
146
+ // logger := log.NewNopLogger()
147
+ // reg := prometheus.NewRegistry()
148
+ // cfg := InMemoryIndexCacheConfig{
149
+ // MaxSizeBytes: uint64(storecache.DefaultInMemoryIndexCacheConfig.MaxSize),
150
+ // }
151
+ // cache, err := newInMemoryIndexCache(cfg, logger, reg)
152
+ // require.NoError(b, err)
153
+ //
154
+ // blockID := ulid.MustNew(ulid.Now(), nil)
155
+ // // 1KB is a common size for series
156
+ // data := make([]byte, 1024)
157
+ // r := rand.New(rand.NewSource(time.Now().Unix()))
158
+ // r.Read(data)
159
+ // b.ReportAllocs()
160
+ // b.ResetTimer()
161
+ // for i := 0; i < b.N; i++ {
162
+ // cache.StoreSeries(blockID, storage.SeriesRef(i), data, tenancy.DefaultTenant)
163
+ // }
164
+ //}
165
+
166
+ func BenchmarkInMemoryIndexCacheStore (b * testing.B ) {
167
+ logger := log .NewNopLogger ()
168
+ cfg := InMemoryIndexCacheConfig {
169
+ MaxSizeBytes : uint64 (storecache .DefaultInMemoryIndexCacheConfig .MaxSize ),
170
+ }
171
+
172
+ blockID := ulid .MustNew (ulid .Now (), nil )
173
+ r := rand .New (rand .NewSource (time .Now ().Unix ()))
174
+ // 1KB is a common size for series
175
+ seriesData := make ([]byte , 1024 )
176
+ r .Read (seriesData )
177
+ // 10MB might happen for large postings.
178
+ postingData := make ([]byte , 10 * 1024 * 1024 )
179
+ r .Read (postingData )
180
+
181
+ b .Run ("FastCache" , func (b * testing.B ) {
182
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
183
+ require .NoError (b , err )
184
+ b .ReportAllocs ()
185
+ b .ResetTimer ()
186
+ for i := 0 ; i < b .N ; i ++ {
187
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
188
+ }
189
+ })
190
+
191
+ b .Run ("ThanosCache" , func (b * testing.B ) {
192
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
193
+ require .NoError (b , err )
194
+ b .ReportAllocs ()
195
+ b .ResetTimer ()
196
+ for i := 0 ; i < b .N ; i ++ {
197
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
198
+ }
199
+ })
200
+
201
+ b .Run ("FastCacheLargeItem" , func (b * testing.B ) {
202
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
203
+ require .NoError (b , err )
204
+ b .ReportAllocs ()
205
+ b .ResetTimer ()
206
+ for i := 0 ; i < b .N ; i ++ {
207
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), postingData , tenancy .DefaultTenant )
208
+ }
209
+ })
210
+
211
+ b .Run ("ThanosCacheLargeItem" , func (b * testing.B ) {
212
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
213
+ require .NoError (b , err )
214
+ b .ReportAllocs ()
215
+ b .ResetTimer ()
216
+ for i := 0 ; i < b .N ; i ++ {
217
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), postingData , tenancy .DefaultTenant )
218
+ }
219
+ })
220
+ }
221
+
222
+ func BenchmarkInMemoryIndexCacheStoreConcurrent (b * testing.B ) {
223
+ logger := log .NewNopLogger ()
224
+ cfg := InMemoryIndexCacheConfig {
225
+ MaxSizeBytes : uint64 (storecache .DefaultInMemoryIndexCacheConfig .MaxSize ),
226
+ }
227
+
228
+ blockID := ulid .MustNew (ulid .Now (), nil )
229
+ r := rand .New (rand .NewSource (time .Now ().Unix ()))
230
+ // 1KB is a common size for series
231
+ seriesData := make ([]byte , 1024 )
232
+ r .Read (seriesData )
233
+ // 10MB might happen for large postings.
234
+ postingData := make ([]byte , 10 * 1024 * 1024 )
235
+ r .Read (postingData )
236
+
237
+ b .Run ("FastCache" , func (b * testing.B ) {
238
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
239
+ require .NoError (b , err )
240
+ ch := make (chan int )
241
+ b .ReportAllocs ()
242
+ b .ResetTimer ()
243
+
244
+ for i := 0 ; i < 500 ; i ++ {
245
+ go func () {
246
+ for j := range ch {
247
+ cache .StoreSeries (blockID , storage .SeriesRef (j ), seriesData , tenancy .DefaultTenant )
248
+ testutil .Ok (b , err )
249
+ }
250
+ }()
251
+ }
252
+
253
+ for i := 0 ; i < b .N ; i ++ {
254
+ ch <- i
255
+ }
256
+ close (ch )
257
+ })
258
+
259
+ b .Run ("ThanosCache" , func (b * testing.B ) {
260
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
261
+ require .NoError (b , err )
262
+ ch := make (chan int )
263
+ b .ReportAllocs ()
264
+ b .ResetTimer ()
265
+
266
+ for i := 0 ; i < 500 ; i ++ {
267
+ go func () {
268
+ for j := range ch {
269
+ cache .StoreSeries (blockID , storage .SeriesRef (j ), seriesData , tenancy .DefaultTenant )
270
+ testutil .Ok (b , err )
271
+ }
272
+ }()
273
+ }
274
+
275
+ for i := 0 ; i < b .N ; i ++ {
276
+ ch <- i
277
+ }
278
+ close (ch )
279
+ })
280
+
281
+ b .Run ("FastCacheLargeItem" , func (b * testing.B ) {
282
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
283
+ require .NoError (b , err )
284
+ ch := make (chan int )
285
+ b .ReportAllocs ()
286
+ b .ResetTimer ()
287
+
288
+ for i := 0 ; i < 500 ; i ++ {
289
+ go func () {
290
+ for j := range ch {
291
+ cache .StoreSeries (blockID , storage .SeriesRef (j ), postingData , tenancy .DefaultTenant )
292
+ testutil .Ok (b , err )
293
+ }
294
+ }()
295
+ }
296
+
297
+ for i := 0 ; i < b .N ; i ++ {
298
+ ch <- i
299
+ }
300
+ close (ch )
301
+ })
302
+
303
+ b .Run ("ThanosCacheLargeItem" , func (b * testing.B ) {
304
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
305
+ require .NoError (b , err )
306
+ ch := make (chan int )
307
+ b .ReportAllocs ()
308
+ b .ResetTimer ()
309
+
310
+ for i := 0 ; i < 500 ; i ++ {
311
+ go func () {
312
+ for j := range ch {
313
+ cache .StoreSeries (blockID , storage .SeriesRef (j ), postingData , tenancy .DefaultTenant )
314
+ testutil .Ok (b , err )
315
+ }
316
+ }()
317
+ }
318
+
319
+ for i := 0 ; i < b .N ; i ++ {
320
+ ch <- i
321
+ }
322
+ close (ch )
323
+ })
324
+ }
325
+
326
+ func BenchmarkInMemoryIndexCacheFetch (b * testing.B ) {
327
+ logger := log .NewNopLogger ()
328
+ cfg := InMemoryIndexCacheConfig {
329
+ MaxSizeBytes : uint64 (storecache .DefaultInMemoryIndexCacheConfig .MaxSize ),
330
+ }
331
+
332
+ blockID := ulid .MustNew (ulid .Now (), nil )
333
+ r := rand .New (rand .NewSource (time .Now ().Unix ()))
334
+ // 1KB is a common size for series
335
+ seriesData := make ([]byte , 1024 )
336
+ r .Read (seriesData )
337
+ ctx := context .Background ()
338
+ items := 10000
339
+ ids := make ([]storage.SeriesRef , items )
340
+ for i := 0 ; i < items ; i ++ {
341
+ ids [i ] = storage .SeriesRef (i )
342
+ }
343
+
344
+ b .Run ("FastCache" , func (b * testing.B ) {
345
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
346
+ require .NoError (b , err )
347
+ for i := 0 ; i < items ; i ++ {
348
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
349
+ }
350
+ b .ReportAllocs ()
351
+ b .ResetTimer ()
352
+ for i := 0 ; i < b .N ; i ++ {
353
+ cache .FetchMultiSeries (ctx , blockID , ids , tenancy .DefaultTenant )
354
+ }
355
+ })
356
+
357
+ b .Run ("ThanosCache" , func (b * testing.B ) {
358
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
359
+ require .NoError (b , err )
360
+ for i := 0 ; i < items ; i ++ {
361
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
362
+ }
363
+ b .ReportAllocs ()
364
+ b .ResetTimer ()
365
+ for i := 0 ; i < b .N ; i ++ {
366
+ cache .FetchMultiSeries (ctx , blockID , ids , tenancy .DefaultTenant )
367
+ }
368
+ })
369
+ }
370
+
371
+ func BenchmarkInMemoryIndexCacheFetchConcurrent (b * testing.B ) {
372
+ logger := log .NewNopLogger ()
373
+ cfg := InMemoryIndexCacheConfig {
374
+ MaxSizeBytes : uint64 (storecache .DefaultInMemoryIndexCacheConfig .MaxSize ),
375
+ }
376
+
377
+ blockID := ulid .MustNew (ulid .Now (), nil )
378
+ r := rand .New (rand .NewSource (time .Now ().Unix ()))
379
+ // 1KB is a common size for series
380
+ seriesData := make ([]byte , 1024 )
381
+ r .Read (seriesData )
382
+ ctx := context .Background ()
383
+ items := 10000
384
+ ids := make ([]storage.SeriesRef , items )
385
+ for i := 0 ; i < items ; i ++ {
386
+ ids [i ] = storage .SeriesRef (i )
387
+ }
388
+
389
+ b .Run ("FastCache" , func (b * testing.B ) {
390
+ cache , err := newInMemoryIndexCache (cfg , logger , prometheus .NewRegistry ())
391
+ require .NoError (b , err )
392
+ for i := 0 ; i < items ; i ++ {
393
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
394
+ }
395
+ b .ReportAllocs ()
396
+ b .ResetTimer ()
397
+
398
+ ch := make (chan int )
399
+ for i := 0 ; i < 500 ; i ++ {
400
+ go func () {
401
+ for range ch {
402
+ cache .FetchMultiSeries (ctx , blockID , ids , tenancy .DefaultTenant )
403
+ }
404
+ }()
405
+ }
406
+
407
+ for i := 0 ; i < b .N ; i ++ {
408
+ ch <- i
409
+ }
410
+ close (ch )
411
+ })
412
+
413
+ b .Run ("ThanosCache" , func (b * testing.B ) {
414
+ cache , err := storecache .NewInMemoryIndexCacheWithConfig (logger , nil , prometheus .NewRegistry (), storecache .DefaultInMemoryIndexCacheConfig )
415
+ require .NoError (b , err )
416
+ for i := 0 ; i < items ; i ++ {
417
+ cache .StoreSeries (blockID , storage .SeriesRef (i ), seriesData , tenancy .DefaultTenant )
418
+ }
419
+ b .ReportAllocs ()
420
+ b .ResetTimer ()
421
+
422
+ ch := make (chan int )
423
+ for i := 0 ; i < 500 ; i ++ {
424
+ go func () {
425
+ for range ch {
426
+ cache .FetchMultiSeries (ctx , blockID , ids , tenancy .DefaultTenant )
427
+ }
428
+ }()
429
+ }
430
+
431
+ for i := 0 ; i < b .N ; i ++ {
432
+ ch <- i
433
+ }
434
+ close (ch )
435
+ })
436
+ }
0 commit comments