github.com/wfusion/gofusion@v1.1.14/test/metrics/cases/push_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"os"
     7  	"runtime"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/prometheus/client_golang/prometheus"
    13  	"github.com/stretchr/testify/suite"
    14  
    15  	"github.com/wfusion/gofusion/common/utils"
    16  	"github.com/wfusion/gofusion/log"
    17  	"github.com/wfusion/gofusion/metrics"
    18  	"github.com/wfusion/gofusion/test/internal/mock"
    19  
    20  	testMetrics "github.com/wfusion/gofusion/test/metrics"
    21  )
    22  
    23  func TestPush(t *testing.T) {
    24  	testingSuite := &Push{Test: new(testMetrics.Test)}
    25  	testingSuite.Init(testingSuite)
    26  	suite.Run(t, testingSuite)
    27  }
    28  
    29  type Push struct {
    30  	*testMetrics.Test
    31  }
    32  
    33  func (t *Push) BeforeTest(suiteName, testName string) {
    34  	t.Catch(func() {
    35  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    36  	})
    37  }
    38  
    39  func (t *Push) AfterTest(suiteName, testName string) {
    40  	t.Catch(func() {
    41  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    42  	})
    43  }
    44  
    45  func (t *Push) TestPrometheus() {
    46  	t.testDefault(namePrometheusPush)
    47  }
    48  
    49  func (t *Push) testDefault(name string) {
    50  	t.Run("testPush", func() { t.testPush(name) })
    51  	t.Run("testConcurrency", func() { t.testConcurrency(name) })
    52  }
    53  
    54  func (t *Push) testPush(name string) {
    55  	t.Catch(func() {
    56  		// Given
    57  		ctx := context.Background()
    58  		labels := []metrics.Label{
    59  			{
    60  				Key:   "hostname",
    61  				Value: utils.Must(os.Hostname()),
    62  			},
    63  			{
    64  				Key:   "ip",
    65  				Value: utils.ClientIP(),
    66  			},
    67  		}
    68  
    69  		job := name + "TestPush"
    70  		sink := metrics.Use(name, job, metrics.AppName(t.AppName()))
    71  
    72  		// When
    73  		sink.SetGauge(ctx, []string{"gauge", "without", "labels"}, mock.GenObj[float64]())
    74  		sink.SetGauge(ctx, []string{"gauge", "with", "labels"}, mock.GenObj[float64](),
    75  			metrics.Labels(labels))
    76  		sink.SetGauge(ctx, []string{"gauge", "with", "buckets"}, mock.GenObj[float64](),
    77  			metrics.Labels(labels),
    78  			metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)))
    79  		sink.SetGauge(ctx, []string{"gauge", "with", "precision"}, mock.GenObj[float64](),
    80  			metrics.Labels(labels),
    81  			metrics.Precision())
    82  
    83  		sink.IncrCounter(ctx, []string{"counter", "without", "labels"}, float64(mock.GenObj[uint8]()))
    84  		sink.IncrCounter(ctx, []string{"counter", "with", "labels"}, float64(mock.GenObj[uint8]()),
    85  			metrics.Labels(labels))
    86  
    87  		sink.AddSample(ctx, []string{"sample", "without", "labels"}, mock.GenObj[float64]())
    88  		sink.AddSample(ctx, []string{"sample", "with", "labels"}, mock.GenObj[float64](),
    89  			metrics.Labels(labels))
    90  		sink.AddSample(ctx, []string{"sample", "with", "buckets"}, mock.GenObj[float64](),
    91  			metrics.Labels(labels),
    92  			metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)))
    93  		sink.AddSample(ctx, []string{"sample", "with", "precision"}, mock.GenObj[float64](),
    94  			metrics.Labels(labels),
    95  			metrics.Precision())
    96  
    97  		sink.MeasureSince(ctx, []string{"measure", "without", "labels"}, mock.GenObj[time.Time]())
    98  		sink.MeasureSince(ctx, []string{"measure", "with", "labels"}, mock.GenObj[time.Time](),
    99  			metrics.Labels(labels))
   100  		sink.MeasureSince(ctx, []string{"measure", "with", "buckets"}, mock.GenObj[time.Time](),
   101  			metrics.Labels(labels),
   102  			metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)))
   103  		sink.MeasureSince(ctx, []string{"measure", "with", "precision"}, mock.GenObj[time.Time](),
   104  			metrics.Labels(labels),
   105  			metrics.Precision())
   106  
   107  		sink.AddSample(ctx, []string{"sample", "with", "timeout"}, mock.GenObj[float64](),
   108  			metrics.Labels(labels),
   109  			metrics.Precision(), metrics.Timeout(100*time.Millisecond))
   110  
   111  		sink.AddSample(ctx, []string{"sample", "with", "must"}, mock.GenObj[float64](),
   112  			metrics.Labels(labels),
   113  			metrics.Precision(), metrics.WithoutTimeout())
   114  
   115  		// Then
   116  		time.Sleep(timeout)
   117  	})
   118  }
   119  
   120  func (t *Push) testConcurrency(name string) {
   121  	t.Catch(func() {
   122  		// Given
   123  		ctx := context.Background()
   124  
   125  		labels := []metrics.Label{
   126  			{
   127  				Key:   "hostname",
   128  				Value: utils.Must(os.Hostname()),
   129  			},
   130  			{
   131  				Key:   "ip",
   132  				Value: utils.ClientIP(),
   133  			},
   134  		}
   135  
   136  		job := name + "TestConcurrency"
   137  		sink := metrics.Use(name, job, metrics.AppName(t.AppName()))
   138  
   139  		// When
   140  		wg := new(sync.WaitGroup)
   141  		round := 1000
   142  		concurrency := runtime.NumCPU()
   143  		for i := 0; i < concurrency; i++ {
   144  			wg.Add(1)
   145  			go func() {
   146  				defer wg.Done()
   147  				for i := 0; i < round; i++ {
   148  					sink.SetGauge(ctx, []string{"gauge", "without", "labels"}, mock.GenObj[float64](),
   149  						metrics.WithoutTimeout())
   150  
   151  					sink.SetGauge(ctx, []string{"gauge", "with", "labels"},
   152  						mock.GenObj[float64](),
   153  						metrics.Labels(labels),
   154  						metrics.WithoutTimeout())
   155  
   156  					sink.SetGauge(ctx, []string{"gauge", "with", "buckets"}, mock.GenObj[float64](),
   157  						metrics.Labels(labels),
   158  						metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)),
   159  						metrics.WithoutTimeout())
   160  
   161  					sink.SetGauge(ctx, []string{"gauge", "with", "precision"}, mock.GenObj[float64](),
   162  						metrics.Labels(labels),
   163  						metrics.Precision(), metrics.WithoutTimeout())
   164  
   165  					sink.IncrCounter(ctx, []string{"counter", "without", "labels"}, float64(mock.GenObj[uint8]()),
   166  						metrics.WithoutTimeout())
   167  
   168  					sink.IncrCounter(ctx, []string{"counter", "with", "labels"},
   169  						float64(mock.GenObj[uint8]()),
   170  						metrics.Labels(labels),
   171  						metrics.WithoutTimeout())
   172  
   173  					sink.AddSample(ctx, []string{"sample", "without", "labels"}, mock.GenObj[float64](),
   174  						metrics.WithoutTimeout())
   175  
   176  					sink.AddSample(ctx, []string{"sample", "with", "labels"}, mock.GenObj[float64](),
   177  						metrics.Labels(labels),
   178  						metrics.WithoutTimeout())
   179  
   180  					sink.AddSample(ctx, []string{"sample", "with", "buckets"}, mock.GenObj[float64](),
   181  						metrics.Labels(labels),
   182  						metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)),
   183  						metrics.WithoutTimeout())
   184  
   185  					sink.AddSample(ctx, []string{"sample", "with", "precision"},
   186  						mock.GenObj[float64](),
   187  						metrics.Labels(labels),
   188  						metrics.Precision(),
   189  						metrics.WithoutTimeout())
   190  
   191  					sink.MeasureSince(ctx, []string{"measure", "without", "labels"}, mock.GenObj[time.Time](),
   192  						metrics.WithoutTimeout())
   193  
   194  					sink.MeasureSince(ctx, []string{"measure", "with", "labels"},
   195  						mock.GenObj[time.Time](),
   196  						metrics.Labels(labels),
   197  						metrics.WithoutTimeout())
   198  
   199  					sink.MeasureSince(ctx, []string{"measure", "with", "buckets"},
   200  						mock.GenObj[time.Time](),
   201  						metrics.Labels(labels),
   202  						metrics.PrometheusBuckets(prometheus.ExponentialBucketsRange(1, math.MaxInt64, 50)),
   203  						metrics.WithoutTimeout())
   204  
   205  					sink.MeasureSince(ctx, []string{"measure", "with", "precision"},
   206  						mock.GenObj[time.Time](),
   207  						metrics.Labels(labels),
   208  						metrics.Precision(),
   209  						metrics.WithoutTimeout())
   210  				}
   211  			}()
   212  		}
   213  
   214  		// Then
   215  		wg.Wait()
   216  		time.Sleep(timeout)
   217  	})
   218  }