github.com/rudderlabs/rudder-go-kit@v0.30.0/stats/otel_benchmark_test.go (about)

     1  package stats
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/rudderlabs/rudder-go-kit/config"
    13  	"github.com/rudderlabs/rudder-go-kit/logger"
    14  	"github.com/rudderlabs/rudder-go-kit/stats/metric"
    15  	statsTest "github.com/rudderlabs/rudder-go-kit/stats/testhelper"
    16  	"github.com/rudderlabs/rudder-go-kit/testhelper/rand"
    17  )
    18  
    19  func BenchmarkOTel(b *testing.B) {
    20  	cwd, err := os.Getwd()
    21  	require.NoError(b, err)
    22  	_, grpcEndpoint := statsTest.StartOTelCollector(b, metricsPort,
    23  		filepath.Join(cwd, "testdata", "otel-collector-config.yaml"),
    24  	)
    25  
    26  	type testCase struct {
    27  		name         string
    28  		grpcEndpoint string
    29  	}
    30  	for _, tc := range []testCase{
    31  		{"reachable", grpcEndpoint},
    32  		{"unreachable", "unreachable:4317"},
    33  	} {
    34  		b.Run(tc.name, func(b *testing.B) {
    35  			b.Run("counter creation", func(b *testing.B) {
    36  				randString := rand.UniqueString(10)
    37  				s := getStatsForBenchmark(tc.grpcEndpoint)
    38  				b.ResetTimer()
    39  				for i := 1; i <= b.N; i++ {
    40  					s.NewTaggedStat("test_counter_"+randString+strconv.Itoa(i), CountType, Tags{"tag1": "value1"})
    41  				}
    42  			})
    43  
    44  			b.Run("timer creation", func(b *testing.B) {
    45  				randString := rand.UniqueString(10)
    46  				s := getStatsForBenchmark(tc.grpcEndpoint)
    47  				b.ResetTimer()
    48  				for i := 1; i <= b.N; i++ {
    49  					s.NewTaggedStat("test_timer_"+randString+strconv.Itoa(i), TimerType, Tags{"tag1": "value1"})
    50  				}
    51  			})
    52  
    53  			b.Run("gauge creation", func(b *testing.B) {
    54  				randString := rand.UniqueString(10)
    55  				s := getStatsForBenchmark(tc.grpcEndpoint)
    56  				b.ResetTimer()
    57  				for i := 1; i <= b.N; i++ {
    58  					s.NewTaggedStat("test_gauge_"+randString+strconv.Itoa(i), GaugeType, Tags{"tag1": "value1"})
    59  				}
    60  			})
    61  
    62  			b.Run("histogram creation", func(b *testing.B) {
    63  				randString := rand.UniqueString(10)
    64  				s := getStatsForBenchmark(tc.grpcEndpoint)
    65  				b.ResetTimer()
    66  				for i := 1; i <= b.N; i++ {
    67  					s.NewTaggedStat("test_histogram_"+randString+strconv.Itoa(i), HistogramType, Tags{"tag1": "value1"})
    68  				}
    69  			})
    70  
    71  			b.Run("use counter", func(b *testing.B) {
    72  				randString := rand.UniqueString(10)
    73  				s := getStatsForBenchmark(tc.grpcEndpoint)
    74  				m := s.NewTaggedStat("test_counter_"+randString, CountType, Tags{"tag1": "value1"})
    75  				b.ResetTimer()
    76  				for i := 0; i < b.N; i++ {
    77  					m.Count(i)
    78  				}
    79  			})
    80  
    81  			b.Run("use timer", func(b *testing.B) {
    82  				randString := rand.UniqueString(10)
    83  				s := getStatsForBenchmark(tc.grpcEndpoint)
    84  				m := s.NewTaggedStat("test_timer_"+randString, TimerType, Tags{"tag1": "value1"})
    85  				start := time.Now()
    86  				b.ResetTimer()
    87  				for i := 0; i < b.N; i++ {
    88  					m.Since(start)
    89  				}
    90  			})
    91  
    92  			b.Run("use gauge", func(b *testing.B) {
    93  				randString := rand.UniqueString(10)
    94  				s := getStatsForBenchmark(tc.grpcEndpoint)
    95  				m := s.NewTaggedStat("test_gauge_"+randString, GaugeType, Tags{"tag1": "value1"})
    96  				b.ResetTimer()
    97  				for i := 0; i < b.N; i++ {
    98  					m.Gauge(i)
    99  				}
   100  			})
   101  
   102  			b.Run("use histogram", func(b *testing.B) {
   103  				randString := rand.UniqueString(10)
   104  				s := getStatsForBenchmark(tc.grpcEndpoint)
   105  				m := s.NewTaggedStat("test_histogram_"+randString, HistogramType, Tags{"tag1": "value1"})
   106  				b.ResetTimer()
   107  				for i := 0; i < b.N; i++ {
   108  					m.Observe(float64(i))
   109  				}
   110  			})
   111  		})
   112  	}
   113  }
   114  
   115  func getStatsForBenchmark(grpcEndpoint string) Stats {
   116  	c := config.New()
   117  	c.Set("INSTANCE_ID", "my-instance-id")
   118  	c.Set("OpenTelemetry.enabled", true)
   119  	c.Set("OpenTelemetry.metrics.endpoint", grpcEndpoint)
   120  	c.Set("OpenTelemetry.metrics.exportInterval", 10*time.Second)
   121  	c.Set("RuntimeStats.enabled", false)
   122  	l := logger.NewFactory(c)
   123  	m := metric.NewManager()
   124  	return NewStats(c, l, m)
   125  }