k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/histogram_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/prometheus/common/model" 24 ) 25 26 func TestConvertSampleToBucket(t *testing.T) { 27 tests := map[string]struct { 28 samples []*model.Sample 29 want *HistogramVec 30 }{ 31 "convert single sample to bucket": { 32 samples: []*model.Sample{ 33 { 34 Metric: model.Metric{"name": "value", "le": "le-value"}, 35 Value: 1, 36 }, 37 }, 38 want: &HistogramVec{ 39 { 40 Labels: map[string]string{ 41 "name": "value", 42 }, 43 Buckets: map[string]int{ 44 "le-value": 1, 45 }, 46 }, 47 }, 48 }, 49 "convert multiple samples to bucket": { 50 samples: []*model.Sample{ 51 { 52 Metric: model.Metric{"name": "value", "le": "le-value"}, 53 Value: 1, 54 }, 55 { 56 Metric: model.Metric{"name": "value", "le": "le-value"}, 57 Value: 2, 58 }, 59 }, 60 want: &HistogramVec{ 61 { 62 Labels: map[string]string{ 63 "name": "value", 64 }, 65 Buckets: map[string]int{ 66 "le-value": 3, 67 }, 68 }, 69 }, 70 }, 71 } 72 73 var inputHistogramVec HistogramVec 74 for name, test := range tests { 75 inputHistogramVec = HistogramVec{ 76 { 77 Labels: map[string]string{ 78 "name": "value", 79 }, 80 Buckets: map[string]int{ 81 "le-value": 0, 82 }, 83 }, 84 } 85 for _, sample := range test.samples { 86 ConvertSampleToBucket(sample, &inputHistogramVec) 87 } 88 if !reflect.DeepEqual(&inputHistogramVec, test.want) { 89 t.Errorf("error %s: \n\tgot %#v \n\twanted %#v", name, &inputHistogramVec, test.want) 90 } 91 } 92 } 93 94 // TestHistogramQuantile makes sure sorted/unsorted list of samples 95 // gives the same percentiles 96 func TestHistogramQuantile(t *testing.T) { 97 tests := []struct { 98 histogram Histogram 99 q50 float64 100 q90 float64 101 q99 float64 102 }{ 103 // unsorted sequence 104 { 105 histogram: Histogram{ 106 Buckets: map[string]int{ 107 "4": 40, 108 "8": 60, 109 "1": 20, 110 "2": 30, 111 }, 112 }, 113 q50: 2.0, 114 q90: 6.8, 115 q99: 7.88, 116 }, 117 // sorted sequence 118 { 119 histogram: Histogram{ 120 Buckets: map[string]int{ 121 "1": 20, 122 "2": 30, 123 "4": 40, 124 "8": 60, 125 }, 126 }, 127 q50: 2.0, 128 q90: 6.8, 129 q99: 7.88, 130 }, 131 } 132 133 for _, test := range tests { 134 if q50, err := test.histogram.Quantile(0.5); err != nil { 135 t.Errorf("Unexpected error for q50: %v", err) 136 } else if q50 != test.q50 { 137 t.Errorf("Expected q50 to be %v, got %v instead", test.q50, q50) 138 } 139 140 if q90, err := test.histogram.Quantile(0.9); err != nil { 141 t.Errorf("Unexpected error for q90: %v", err) 142 } else if q90 != test.q90 { 143 t.Errorf("Expected q90 to be %v, got %v instead", test.q90, q90) 144 } 145 146 if q99, err := test.histogram.Quantile(0.99); err != nil { 147 t.Errorf("Unexpected error for q99: %v", err) 148 } else if q99 != test.q99 { 149 t.Errorf("Expected q99 to be %v, got %v instead", test.q99, q99) 150 } 151 } 152 }