|
| 1 | +package metrics |
| 2 | + |
| 3 | +// Copyright (C) 2021-2025 Intel Corporation |
| 4 | +// SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestExcludeFinalSample(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + inputRows []row |
| 16 | + expectedCount int |
| 17 | + expectedMaxTS float64 |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "exclude single final timestamp", |
| 21 | + inputRows: []row{ |
| 22 | + {timestamp: 5.0, metrics: map[string]float64{"metric1": 100.0}}, |
| 23 | + {timestamp: 10.0, metrics: map[string]float64{"metric1": 200.0}}, |
| 24 | + {timestamp: 15.0, metrics: map[string]float64{"metric1": 150.0}}, |
| 25 | + {timestamp: 20.0, metrics: map[string]float64{"metric1": 50.0}}, // this should be excluded |
| 26 | + }, |
| 27 | + expectedCount: 3, |
| 28 | + expectedMaxTS: 15.0, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "exclude multiple rows with same final timestamp", |
| 32 | + inputRows: []row{ |
| 33 | + {timestamp: 5.0, socket: "0", metrics: map[string]float64{"metric1": 100.0}}, |
| 34 | + {timestamp: 10.0, socket: "0", metrics: map[string]float64{"metric1": 200.0}}, |
| 35 | + {timestamp: 15.0, socket: "0", metrics: map[string]float64{"metric1": 150.0}}, |
| 36 | + {timestamp: 15.0, socket: "1", metrics: map[string]float64{"metric1": 160.0}}, // same timestamp, different socket |
| 37 | + }, |
| 38 | + expectedCount: 2, |
| 39 | + expectedMaxTS: 10.0, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "single sample - should not exclude", |
| 43 | + inputRows: []row{ |
| 44 | + {timestamp: 5.0, metrics: map[string]float64{"metric1": 100.0}}, |
| 45 | + }, |
| 46 | + expectedCount: 1, |
| 47 | + expectedMaxTS: 5.0, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "two samples - exclude last one", |
| 51 | + inputRows: []row{ |
| 52 | + {timestamp: 5.0, metrics: map[string]float64{"metric1": 100.0}}, |
| 53 | + {timestamp: 10.0, metrics: map[string]float64{"metric1": 50.0}}, |
| 54 | + }, |
| 55 | + expectedCount: 1, |
| 56 | + expectedMaxTS: 5.0, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + // Create a MetricCollection with a single MetricGroup |
| 63 | + mc := MetricCollection{ |
| 64 | + MetricGroup{ |
| 65 | + names: []string{"metric1"}, |
| 66 | + rows: tt.inputRows, |
| 67 | + groupByField: "", |
| 68 | + groupByValue: "", |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + // Call excludeFinalSample |
| 73 | + mc.excludeFinalSample() |
| 74 | + |
| 75 | + // Verify the number of remaining rows |
| 76 | + assert.Equal(t, tt.expectedCount, len(mc[0].rows), "unexpected number of rows after exclusion") |
| 77 | + |
| 78 | + // Verify that no row has a timestamp greater than expectedMaxTS |
| 79 | + if len(mc[0].rows) > 0 { |
| 80 | + for _, row := range mc[0].rows { |
| 81 | + assert.LessOrEqual(t, row.timestamp, tt.expectedMaxTS, "found row with timestamp greater than expected maximum") |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestExcludeFinalSampleMultipleGroups(t *testing.T) { |
| 89 | + // Test with multiple metric groups (e.g., multiple sockets) |
| 90 | + mc := MetricCollection{ |
| 91 | + MetricGroup{ |
| 92 | + names: []string{"metric1"}, |
| 93 | + groupByField: "SKT", |
| 94 | + groupByValue: "0", |
| 95 | + rows: []row{ |
| 96 | + {timestamp: 5.0, socket: "0", metrics: map[string]float64{"metric1": 100.0}}, |
| 97 | + {timestamp: 10.0, socket: "0", metrics: map[string]float64{"metric1": 200.0}}, |
| 98 | + {timestamp: 15.0, socket: "0", metrics: map[string]float64{"metric1": 50.0}}, // should be excluded |
| 99 | + }, |
| 100 | + }, |
| 101 | + MetricGroup{ |
| 102 | + names: []string{"metric1"}, |
| 103 | + groupByField: "SKT", |
| 104 | + groupByValue: "1", |
| 105 | + rows: []row{ |
| 106 | + {timestamp: 5.0, socket: "1", metrics: map[string]float64{"metric1": 110.0}}, |
| 107 | + {timestamp: 10.0, socket: "1", metrics: map[string]float64{"metric1": 210.0}}, |
| 108 | + {timestamp: 15.0, socket: "1", metrics: map[string]float64{"metric1": 60.0}}, // should be excluded |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + mc.excludeFinalSample() |
| 114 | + |
| 115 | + // Both groups should have 2 rows remaining |
| 116 | + assert.Equal(t, 2, len(mc[0].rows), "socket 0 should have 2 rows") |
| 117 | + assert.Equal(t, 2, len(mc[1].rows), "socket 1 should have 2 rows") |
| 118 | + |
| 119 | + // Verify max timestamps |
| 120 | + assert.Equal(t, 10.0, mc[0].rows[1].timestamp, "socket 0 max timestamp should be 10.0") |
| 121 | + assert.Equal(t, 10.0, mc[1].rows[1].timestamp, "socket 1 max timestamp should be 10.0") |
| 122 | +} |
| 123 | + |
| 124 | +func TestExcludeFinalSampleEmptyCollection(t *testing.T) { |
| 125 | + // Test with empty MetricCollection |
| 126 | + mc := MetricCollection{} |
| 127 | + mc.excludeFinalSample() // should not panic |
| 128 | + assert.Equal(t, 0, len(mc), "empty collection should remain empty") |
| 129 | +} |
| 130 | + |
| 131 | +func TestExcludeFinalSampleEmptyRows(t *testing.T) { |
| 132 | + // Test with MetricGroup that has no rows |
| 133 | + mc := MetricCollection{ |
| 134 | + MetricGroup{ |
| 135 | + names: []string{"metric1"}, |
| 136 | + groupByField: "", |
| 137 | + groupByValue: "", |
| 138 | + rows: []row{}, |
| 139 | + }, |
| 140 | + } |
| 141 | + mc.excludeFinalSample() // should not panic |
| 142 | + assert.Equal(t, 0, len(mc[0].rows), "empty rows should remain empty") |
| 143 | +} |
0 commit comments