@@ -331,3 +331,95 @@ func TestHandlerTimeout(t *testing.T) {
331331
332332 close (c .Block ) // To not leak a goroutine.
333333}
334+
335+ func BenchmarkEncoding (b * testing.B ) {
336+ benchmarks := []struct {
337+ name string
338+ encodingType string
339+ }{
340+ {
341+ name : "test with gzip encoding" ,
342+ encodingType : "gzip" ,
343+ },
344+ {
345+ name : "test with zstd encoding" ,
346+ encodingType : "zstd" ,
347+ },
348+ {
349+ name : "test with no encoding" ,
350+ encodingType : "identity" ,
351+ },
352+ }
353+ sizes := []struct {
354+ name string
355+ metricCount int
356+ labelCount int
357+ labelLength int
358+ metricLength int
359+ }{
360+ {
361+ name : "small" ,
362+ metricCount : 50 ,
363+ labelCount : 5 ,
364+ labelLength : 5 ,
365+ metricLength : 5 ,
366+ },
367+ {
368+ name : "medium" ,
369+ metricCount : 500 ,
370+ labelCount : 10 ,
371+ labelLength : 5 ,
372+ metricLength : 10 ,
373+ },
374+ {
375+ name : "large" ,
376+ metricCount : 5000 ,
377+ labelCount : 10 ,
378+ labelLength : 5 ,
379+ metricLength : 10 ,
380+ },
381+ {
382+ name : "extra-large" ,
383+ metricCount : 50000 ,
384+ labelCount : 20 ,
385+ labelLength : 5 ,
386+ metricLength : 10 ,
387+ },
388+ }
389+
390+ for _ , size := range sizes {
391+ reg := prometheus .NewRegistry ()
392+ handler := HandlerFor (reg , HandlerOpts {})
393+
394+ // Generate Metrics
395+ // Original source: https://github.com/prometheus-community/avalanche/blob/main/metrics/serve.go
396+ labelKeys := make ([]string , size .labelCount )
397+ for idx := 0 ; idx < size .labelCount ; idx ++ {
398+ labelKeys [idx ] = fmt .Sprintf ("label_key_%s_%v" , strings .Repeat ("k" , size .labelLength ), idx )
399+ }
400+ labelValues := make ([]string , size .labelCount )
401+ for idx := 0 ; idx < size .labelCount ; idx ++ {
402+ labelValues [idx ] = fmt .Sprintf ("label_val_%s_%v" , strings .Repeat ("v" , size .labelLength ), idx )
403+ }
404+ metrics := make ([]* prometheus.GaugeVec , size .metricCount )
405+ for idx := 0 ; idx < size .metricCount ; idx ++ {
406+ gauge := prometheus .NewGaugeVec (prometheus.GaugeOpts {
407+ Name : fmt .Sprintf ("avalanche_metric_%s_%v_%v" , strings .Repeat ("m" , size .metricLength ), 0 , idx ),
408+ Help : "A tasty metric morsel" ,
409+ }, append ([]string {"series_id" , "cycle_id" }, labelKeys ... ))
410+ reg .MustRegister (gauge )
411+ metrics [idx ] = gauge
412+ }
413+
414+ for _ , benchmark := range benchmarks {
415+ b .Run (benchmark .name + "_" + size .name , func (b * testing.B ) {
416+ for i := 0 ; i < b .N ; i ++ {
417+ writer := httptest .NewRecorder ()
418+ request , _ := http .NewRequest ("GET" , "/" , nil )
419+ request .Header .Add ("Accept-Encoding" , benchmark .encodingType )
420+ handler .ServeHTTP (writer , request )
421+ }
422+ })
423+ }
424+ }
425+ }
0 commit comments