github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/otlp/metric_helper_test.go (about)

     1  // Copyright 2022 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package otlp
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/alibaba/ilogtail/pkg/models"
    24  )
    25  
    26  func TestComposeBucket(t *testing.T) {
    27  	tests := []struct {
    28  		lower      float64
    29  		upper      float64
    30  		isPositive bool
    31  		expected   string
    32  	}{
    33  		{
    34  			lower:      0.5,
    35  			upper:      1000,
    36  			isPositive: true,
    37  			expected:   "(0.5,1000]",
    38  		},
    39  		{
    40  			lower:      -0.5,
    41  			upper:      1000,
    42  			isPositive: true,
    43  			expected:   "(-0.5,1000]",
    44  		},
    45  		{
    46  			lower:      0.5,
    47  			upper:      1000,
    48  			isPositive: false,
    49  			expected:   "[-1000,-0.5)",
    50  		},
    51  		{
    52  			lower:      math.Inf(-1),
    53  			upper:      1000,
    54  			isPositive: true,
    55  			expected:   "(-Inf,1000]",
    56  		},
    57  		{
    58  			lower:      0,
    59  			upper:      math.Inf(1),
    60  			isPositive: false,
    61  			expected:   "[-Inf,-0)",
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		bucketRange := ComposeBucketFieldName(test.lower, test.upper, test.isPositive)
    67  		assert.Equal(t, test.expected, bucketRange)
    68  	}
    69  }
    70  
    71  func TestComputeBucketBoundary(t *testing.T) {
    72  	tests := []struct {
    73  		bucketRange    string
    74  		isPositive     bool
    75  		expectedBucket float64
    76  	}{
    77  		{
    78  			bucketRange:    "(0.5,1000]",
    79  			isPositive:     true,
    80  			expectedBucket: 0.5,
    81  		},
    82  		{
    83  			bucketRange:    "(-0.5,1000]",
    84  			isPositive:     true,
    85  			expectedBucket: -0.5,
    86  		},
    87  		{
    88  			bucketRange:    "[-1000,-0.5)",
    89  			isPositive:     false,
    90  			expectedBucket: -0.5,
    91  		},
    92  		{
    93  			bucketRange:    "(-Inf,1000]",
    94  			isPositive:     true,
    95  			expectedBucket: math.Inf(-1),
    96  		},
    97  		{
    98  			bucketRange:    "[-Inf,-0)",
    99  			isPositive:     false,
   100  			expectedBucket: 0,
   101  		},
   102  	}
   103  
   104  	for _, test := range tests {
   105  		bucket, err := ComputeBucketBoundary(test.bucketRange, test.isPositive)
   106  		assert.NoError(t, err)
   107  		assert.Equal(t, test.expectedBucket, bucket)
   108  	}
   109  }
   110  
   111  func TestComputeBuckets(t *testing.T) {
   112  	tests := []struct {
   113  		multiValues          map[string]float64
   114  		isPositive           bool
   115  		expectedBucketBounds []float64
   116  		expectedBucketCounts []float64
   117  	}{
   118  		{
   119  			multiValues:          map[string]float64{"(1,2]": 1, "(2,3]": 4, "(1": 1, "(3,+inf]": 100},
   120  			isPositive:           true,
   121  			expectedBucketBounds: []float64{1, 2, 3},
   122  			expectedBucketCounts: []float64{1, 4, 100},
   123  		},
   124  
   125  		{
   126  			multiValues:          map[string]float64{"[-2,-1)": 1, "[-3,-2)": 4, "(1": 1, "[-inf,-4)": 100},
   127  			isPositive:           false,
   128  			expectedBucketBounds: []float64{-1, -2, -4},
   129  			expectedBucketCounts: []float64{1, 4, 100},
   130  		},
   131  	}
   132  
   133  	for _, test := range tests {
   134  		v := models.NewMetricMultiValueWithMap(test.multiValues).GetMultiValues()
   135  		bucketBounds, bucketCounts := ComputeBuckets(v, test.isPositive)
   136  		assert.Equal(t, test.expectedBucketBounds, bucketBounds)
   137  		assert.Equal(t, test.expectedBucketCounts, bucketCounts)
   138  	}
   139  
   140  }
   141  
   142  func TestReverseSlice(t *testing.T) {
   143  	var inputEmpty []float64
   144  	ReverseSlice(inputEmpty)
   145  	assert.Equal(t, []float64(nil), inputEmpty)
   146  
   147  	inputFloat := []float64{3, 2, 1, 10}
   148  	ReverseSlice(inputFloat)
   149  	assert.Equal(t, []float64{10, 1, 2, 3}, inputFloat)
   150  
   151  	inputInt := []int{1, 2, 3}
   152  	ReverseSlice(inputInt)
   153  	assert.Equal(t, []int{3, 2, 1}, inputInt)
   154  
   155  }