istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/loadbalancersim/timeseries/data.go (about) 1 // Copyright Istio 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 timeseries 16 17 import ( 18 "math" 19 "sort" 20 ) 21 22 var ( 23 negativeInfinity = math.Inf(-1) 24 infinity = math.Inf(1) 25 nan = math.NaN() 26 ) 27 28 type Data []float64 29 30 func (d Data) Min() float64 { 31 return d.sorted().min() 32 } 33 34 func (d Data) Max() float64 { 35 return d.sorted().max() 36 } 37 38 func (d Data) Median() float64 { 39 return d.Quantile(0.5) 40 } 41 42 func (d Data) Mean() float64 { 43 total := float64(0) 44 for _, v := range d { 45 total += v 46 } 47 return total / float64(len(d)) 48 } 49 50 func (d Data) Quantile(phi float64) float64 { 51 return d.sorted().quantile(phi) 52 } 53 54 func (d Data) Quantiles(phis ...float64) []float64 { 55 return d.sorted().quantiles(phis...) 56 } 57 58 func (d Data) Copy() Data { 59 out := make(Data, 0, len(d)) 60 out = append(out, d...) 61 return out 62 } 63 64 func (d Data) sorted() sorted { 65 out := make(sorted, 0, len(d)) 66 out = append(out, d...) 67 68 sort.Float64s(out) 69 return out 70 } 71 72 type sorted []float64 73 74 func (s sorted) min() float64 { 75 if len(s) == 0 { 76 return negativeInfinity 77 } 78 79 return s[0] 80 } 81 82 func (s sorted) max() float64 { 83 if len(s) == 0 { 84 return infinity 85 } 86 87 return s[len(s)-1] 88 } 89 90 func (s sorted) quantile(phi float64) float64 { 91 if len(s) == 0 || math.IsNaN(phi) { 92 return nan 93 } 94 if phi <= 0 { 95 return s.min() 96 } 97 if phi >= 1 { 98 return s.max() 99 } 100 idx := uint(phi*float64(len(s)-1) + 0.5) 101 if idx >= uint(len(s)) { 102 idx = uint(len(s) - 1) 103 } 104 return s[idx] 105 } 106 107 func (s sorted) quantiles(phis ...float64) []float64 { 108 out := make([]float64, 0, len(phis)) 109 for _, phi := range phis { 110 out = append(out, s.quantile(phi)) 111 } 112 return out 113 }