trpc.group/trpc-go/trpc-go@v1.0.3/metrics/histogram_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package metrics_test
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	"trpc.group/trpc-go/trpc-go/metrics"
    23  )
    24  
    25  func Test_histogram_AddSample(t *testing.T) {
    26  
    27  	buckets := metrics.NewDurationBounds(time.Second, time.Second*2, time.Second*5)
    28  	h := metrics.Histogram("req.timecost", buckets)
    29  
    30  	metrics.RegisterMetricsSink(&metrics.ConsoleSink{})
    31  
    32  	type args struct {
    33  		value float64
    34  	}
    35  	tests := []struct {
    36  		name string
    37  		args args
    38  	}{
    39  		{"histogram-sample-0.5s", args{float64(time.Millisecond * 500)}},
    40  		{"histogram-sample-1.5s", args{float64(time.Millisecond * 1500)}},
    41  		{"histogram-sample-2.5s", args{float64(time.Millisecond * 2500)}},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			h.AddSample(tt.args.value)
    46  			b := h.GetBuckets()
    47  			assert.NotEqual(t, 0, len(b))
    48  		})
    49  	}
    50  }