github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/histogram/histogram_test.go (about) 1 // Copyright 2023 The Inspektor Gadget 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 histogram 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestHistogram_NewIntervalsFromExp2Slots(t *testing.T) { 24 t.Parallel() 25 26 const unit = UnitMicroseconds 27 28 testTable := []struct { 29 description string 30 slots []uint32 31 expected *Histogram 32 }{ 33 { 34 description: "Nil slots", 35 slots: nil, 36 expected: &Histogram{ 37 Unit: unit, 38 Intervals: nil, 39 }, 40 }, 41 { 42 description: "Empty slots", 43 slots: []uint32{}, 44 expected: &Histogram{ 45 Unit: unit, 46 Intervals: nil, 47 }, 48 }, 49 { 50 description: "With 1 slot", 51 slots: []uint32{333}, 52 expected: &Histogram{ 53 Unit: unit, 54 Intervals: []Interval{ 55 {Count: 333, Start: 0, End: 1}, 56 }, 57 }, 58 }, 59 { 60 description: "With 2 slots", 61 slots: []uint32{777, 555}, 62 expected: &Histogram{ 63 Unit: unit, 64 Intervals: []Interval{ 65 {Count: 777, Start: 0, End: 1}, 66 {Count: 555, Start: 2, End: 3}, 67 }, 68 }, 69 }, 70 { 71 description: "With zero slots", 72 slots: []uint32{222, 0, 111}, 73 expected: &Histogram{ 74 Unit: unit, 75 Intervals: []Interval{ 76 {Count: 222, Start: 0, End: 1}, 77 {Count: 0, Start: 2, End: 3}, 78 {Count: 111, Start: 4, End: 7}, 79 }, 80 }, 81 }, 82 { 83 description: "Zero at first slot", 84 slots: []uint32{0, 888, 0, 666}, 85 expected: &Histogram{ 86 Unit: unit, 87 Intervals: []Interval{ 88 {Count: 0, Start: 0, End: 1}, 89 {Count: 888, Start: 2, End: 3}, 90 {Count: 0, Start: 4, End: 7}, 91 {Count: 666, Start: 8, End: 15}, 92 }, 93 }, 94 }, 95 { 96 description: "Multiple zeros at first slots", 97 slots: []uint32{0, 0, 0, 111}, 98 expected: &Histogram{ 99 Unit: unit, 100 Intervals: []Interval{ 101 {Count: 0, Start: 0, End: 1}, 102 {Count: 0, Start: 2, End: 3}, 103 {Count: 0, Start: 4, End: 7}, 104 {Count: 111, Start: 8, End: 15}, 105 }, 106 }, 107 }, 108 { 109 description: "Multiple zeros at last slots", 110 slots: []uint32{0, 888, 0, 111, 0, 0, 0}, 111 expected: &Histogram{ 112 Unit: unit, 113 Intervals: []Interval{ 114 {Count: 0, Start: 0, End: 1}, 115 {Count: 888, Start: 2, End: 3}, 116 {Count: 0, Start: 4, End: 7}, 117 {Count: 111, Start: 8, End: 15}, 118 }, 119 }, 120 }, 121 } 122 123 for _, test := range testTable { 124 test := test 125 t.Run(test.description, func(t *testing.T) { 126 t.Parallel() 127 128 h := &Histogram{ 129 Unit: unit, 130 Intervals: NewIntervalsFromExp2Slots(test.slots), 131 } 132 require.Equal(t, test.expected, h, "creating histogram from exp2 slots") 133 }) 134 } 135 } 136 137 func TestHistogram_String(t *testing.T) { 138 t.Parallel() 139 140 testTable := []struct { 141 description string 142 histogram *Histogram 143 expected string 144 }{ 145 { 146 description: "Empty histogram", 147 histogram: &Histogram{ 148 Unit: UnitMicroseconds, 149 Intervals: []Interval{}, 150 }, 151 expected: "", 152 }, 153 { 154 description: "With 1 slot value 1", 155 histogram: &Histogram{ 156 Unit: UnitMicroseconds, 157 Intervals: NewIntervalsFromExp2Slots([]uint32{1}), 158 }, 159 expected: "" + 160 " µs : count distribution\n" + 161 " 0 -> 1 : 1 |****************************************|\n", 162 }, 163 { 164 description: "With 1 slot value 55", 165 histogram: &Histogram{ 166 Unit: UnitMicroseconds, 167 Intervals: NewIntervalsFromExp2Slots([]uint32{55}), 168 }, 169 expected: "" + 170 " µs : count distribution\n" + 171 " 0 -> 1 : 55 |****************************************|\n", 172 }, 173 { 174 description: "scale", 175 histogram: &Histogram{ 176 Unit: UnitMicroseconds, 177 Intervals: NewIntervalsFromExp2Slots([]uint32{1, 2, 3}), 178 }, 179 expected: "" + 180 " µs : count distribution\n" + 181 " 0 -> 1 : 1 |************* |\n" + 182 " 2 -> 3 : 2 |************************** |\n" + 183 " 4 -> 7 : 3 |****************************************|\n", 184 }, 185 { 186 description: "scale with empty slots", 187 histogram: &Histogram{ 188 Unit: UnitMicroseconds, 189 Intervals: NewIntervalsFromExp2Slots([]uint32{1, 0, 3}), 190 }, 191 expected: "" + 192 " µs : count distribution\n" + 193 " 0 -> 1 : 1 |************* |\n" + 194 " 2 -> 3 : 0 | |\n" + 195 " 4 -> 7 : 3 |****************************************|\n", 196 }, 197 { 198 description: "scale with empty slots and same values 1", 199 histogram: &Histogram{ 200 Unit: UnitMicroseconds, 201 Intervals: NewIntervalsFromExp2Slots([]uint32{1, 0, 1}), 202 }, 203 expected: "" + 204 " µs : count distribution\n" + 205 " 0 -> 1 : 1 |****************************************|\n" + 206 " 2 -> 3 : 0 | |\n" + 207 " 4 -> 7 : 1 |****************************************|\n", 208 }, 209 { 210 description: "scale with empty slots and same values 100", 211 histogram: &Histogram{ 212 Unit: UnitMicroseconds, 213 Intervals: NewIntervalsFromExp2Slots([]uint32{100, 0, 100}), 214 }, 215 expected: "" + 216 " µs : count distribution\n" + 217 " 0 -> 1 : 100 |****************************************|\n" + 218 " 2 -> 3 : 0 | |\n" + 219 " 4 -> 7 : 100 |****************************************|\n", 220 }, 221 } 222 223 for _, test := range testTable { 224 test := test 225 t.Run(test.description, func(t *testing.T) { 226 t.Parallel() 227 228 actual := test.histogram.String() 229 require.Equal(t, test.expected, actual, "histogram string representation") 230 }) 231 } 232 }