@@ -331,3 +331,147 @@ func TestHandlerTimeout(t *testing.T) {
331
331
332
332
close (c .Block ) // To not leak a goroutine.
333
333
}
334
+
335
+ func TestEncodingAccepted (t * testing.T ) {
336
+ testCases := []struct {
337
+ name string
338
+ header http.Header
339
+ encodingType string
340
+ expected bool
341
+ }{
342
+ {
343
+ name : "test with gzip accept-encoding" ,
344
+ header : http.Header {"Accept-Encoding" : {"gzip" }},
345
+ encodingType : "gzip" ,
346
+ expected : true ,
347
+ },
348
+ {
349
+ name : "test with zstd accept-encoding" ,
350
+ header : http.Header {"Accept-Encoding" : {"zstd" }},
351
+ encodingType : "zstd" ,
352
+ expected : true ,
353
+ },
354
+ {
355
+ name : "test with zstd + gzip accept-encoding" ,
356
+ header : http.Header {"Accept-Encoding" : {"zstd;gzip" }},
357
+ encodingType : "zstd" ,
358
+ expected : true ,
359
+ },
360
+ {
361
+ name : "test with gzip accept-encoding and zstd allowed" ,
362
+ header : http.Header {"Accept-Encoding" : {"gzip" }},
363
+ encodingType : "zstd" ,
364
+ expected : false ,
365
+ },
366
+ {
367
+ name : "test with plain encoding" ,
368
+ header : http.Header {"Accept-Encoding" : {"plain" }},
369
+ encodingType : "zstd" ,
370
+ expected : false ,
371
+ },
372
+ {
373
+ name : "test with encoding header" ,
374
+ header : http.Header {},
375
+ encodingType : "zstd" ,
376
+ expected : false ,
377
+ },
378
+ }
379
+
380
+ for _ , test := range testCases {
381
+ if actual := EncodingAccepted (test .header , test .encodingType ); test .expected != actual {
382
+ t .Fatalf ("%v: expected %v, actual %v" , test .name , test .expected , actual )
383
+ }
384
+ }
385
+ }
386
+
387
+ func BenchmarkEncoding (b * testing.B ) {
388
+ benchmarks := []struct {
389
+ name string
390
+ encodingType string
391
+ }{
392
+ {
393
+ name : "test with gzip encoding" ,
394
+ encodingType : "gzip" ,
395
+ },
396
+ {
397
+ name : "test with zstd encoding" ,
398
+ encodingType : "zstd" ,
399
+ },
400
+ {
401
+ name : "test with no encoding" ,
402
+ encodingType : "identity" ,
403
+ },
404
+ }
405
+ sizes := []struct {
406
+ name string
407
+ metricCount int
408
+ labelCount int
409
+ labelLength int
410
+ metricLength int
411
+ }{
412
+ {
413
+ name : "small" ,
414
+ metricCount : 50 ,
415
+ labelCount : 5 ,
416
+ labelLength : 5 ,
417
+ metricLength : 5 ,
418
+ },
419
+ {
420
+ name : "medium" ,
421
+ metricCount : 500 ,
422
+ labelCount : 10 ,
423
+ labelLength : 5 ,
424
+ metricLength : 10 ,
425
+ },
426
+ {
427
+ name : "large" ,
428
+ metricCount : 5000 ,
429
+ labelCount : 10 ,
430
+ labelLength : 5 ,
431
+ metricLength : 10 ,
432
+ },
433
+ {
434
+ name : "extra-large" ,
435
+ metricCount : 50000 ,
436
+ labelCount : 20 ,
437
+ labelLength : 5 ,
438
+ metricLength : 10 ,
439
+ },
440
+ }
441
+
442
+ for _ , size := range sizes {
443
+ reg := prometheus .NewRegistry ()
444
+ handler := HandlerFor (reg , HandlerOpts {})
445
+
446
+ // Generate Metrics
447
+ // Original source: https://github.com/prometheus-community/avalanche/blob/main/metrics/serve.go
448
+ labelKeys := make ([]string , size .labelCount )
449
+ for idx := 0 ; idx < size .labelCount ; idx ++ {
450
+ labelKeys [idx ] = fmt .Sprintf ("label_key_%s_%v" , strings .Repeat ("k" , size .labelLength ), idx )
451
+ }
452
+ labelValues := make ([]string , size .labelCount )
453
+ for idx := 0 ; idx < size .labelCount ; idx ++ {
454
+ labelValues [idx ] = fmt .Sprintf ("label_val_%s_%v" , strings .Repeat ("v" , size .labelLength ), idx )
455
+ }
456
+ metrics := make ([]* prometheus.GaugeVec , size .metricCount )
457
+ for idx := 0 ; idx < size .metricCount ; idx ++ {
458
+ gauge := prometheus .NewGaugeVec (prometheus.GaugeOpts {
459
+ Name : fmt .Sprintf ("avalanche_metric_%s_%v_%v" , strings .Repeat ("m" , size .metricLength ), 0 , idx ),
460
+ Help : "A tasty metric morsel" ,
461
+ }, append ([]string {"series_id" , "cycle_id" }, labelKeys ... ))
462
+ reg .MustRegister (gauge )
463
+ metrics [idx ] = gauge
464
+ }
465
+
466
+ for _ , benchmark := range benchmarks {
467
+ b .Run (benchmark .name + "_" + size .name , func (b * testing.B ) {
468
+ for i := 0 ; i < b .N ; i ++ {
469
+ writer := httptest .NewRecorder ()
470
+ request , _ := http .NewRequest ("GET" , "/" , nil )
471
+ request .Header .Add ("Accept-Encoding" , benchmark .encodingType )
472
+ handler .ServeHTTP (writer , request )
473
+ }
474
+ })
475
+ }
476
+ }
477
+ }
0 commit comments