github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/rate_calculator_test.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     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 processors
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"k8s.io/heapster/metrics/core"
    24  )
    25  
    26  func TestRateCalculator(t *testing.T) {
    27  	key := core.PodContainerKey("ns1", "pod1", "c")
    28  	now := time.Now()
    29  
    30  	prev := &core.DataBatch{
    31  		Timestamp: now.Add(-time.Minute),
    32  		MetricSets: map[string]*core.MetricSet{
    33  			key: {
    34  				CollectionStartTime: now.Add(-time.Hour),
    35  				ScrapeTime:          now.Add(-60 * time.Second),
    36  
    37  				Labels: map[string]string{
    38  					core.LabelMetricSetType.Key: core.MetricSetTypePodContainer,
    39  				},
    40  				MetricValues: map[string]core.MetricValue{
    41  					core.MetricCpuUsage.MetricDescriptor.Name: {
    42  						ValueType:  core.ValueInt64,
    43  						MetricType: core.MetricCumulative,
    44  						IntValue:   947130377781,
    45  					},
    46  					core.MetricNetworkTxErrors.MetricDescriptor.Name: {
    47  						ValueType:  core.ValueInt64,
    48  						MetricType: core.MetricCumulative,
    49  						IntValue:   0,
    50  					},
    51  				},
    52  			},
    53  		},
    54  	}
    55  
    56  	current := &core.DataBatch{
    57  		Timestamp: now,
    58  		MetricSets: map[string]*core.MetricSet{
    59  
    60  			key: {
    61  				CollectionStartTime: now.Add(-time.Hour),
    62  				ScrapeTime:          now,
    63  
    64  				Labels: map[string]string{
    65  					core.LabelMetricSetType.Key: core.MetricSetTypePodContainer,
    66  				},
    67  				MetricValues: map[string]core.MetricValue{
    68  					core.MetricCpuUsage.MetricDescriptor.Name: {
    69  						ValueType:  core.ValueInt64,
    70  						MetricType: core.MetricCumulative,
    71  						IntValue:   948071062732,
    72  					},
    73  					core.MetricNetworkTxErrors.MetricDescriptor.Name: {
    74  						ValueType:  core.ValueInt64,
    75  						MetricType: core.MetricCumulative,
    76  						IntValue:   120,
    77  					},
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	procesor := NewRateCalculator(core.RateMetricsMapping)
    84  	procesor.Process(prev)
    85  	procesor.Process(current)
    86  
    87  	ms := current.MetricSets[key]
    88  	cpuRate := ms.MetricValues[core.MetricCpuUsageRate.Name]
    89  	txeRate := ms.MetricValues[core.MetricNetworkTxErrorsRate.Name]
    90  
    91  	assert.InEpsilon(t, 13, cpuRate.IntValue, 2)
    92  	assert.InEpsilon(t, 2, txeRate.FloatValue, 0.1)
    93  }