1// Copyright 2016 Google LLC 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 15package stat 16 17import ( 18 "bytes" 19 "encoding/csv" 20 "fmt" 21 "io" 22 "math" 23 "sort" 24 "strconv" 25 "text/tabwriter" 26 "time" 27) 28 29type byDuration []time.Duration 30 31func (data byDuration) Len() int { return len(data) } 32func (data byDuration) Swap(i, j int) { data[i], data[j] = data[j], data[i] } 33func (data byDuration) Less(i, j int) bool { return data[i] < data[j] } 34 35// quantile returns a value representing the kth of q quantiles. 36// May alter the order of data. 37func quantile(data []time.Duration, k, q int) (quantile time.Duration, ok bool) { 38 if len(data) < 1 { 39 return 0, false 40 } 41 if k > q { 42 return 0, false 43 } 44 if k < 0 || q < 1 { 45 return 0, false 46 } 47 48 sort.Sort(byDuration(data)) 49 50 if k == 0 { 51 return data[0], true 52 } 53 if k == q { 54 return data[len(data)-1], true 55 } 56 57 bucketSize := float64(len(data)-1) / float64(q) 58 i := float64(k) * bucketSize 59 60 lower := int(math.Trunc(i)) 61 var upper int 62 if i > float64(lower) && lower+1 < len(data) { 63 // If the quantile lies between two elements 64 upper = lower + 1 65 } else { 66 upper = lower 67 } 68 weightUpper := i - float64(lower) 69 weightLower := 1 - weightUpper 70 return time.Duration(weightLower*float64(data[lower]) + weightUpper*float64(data[upper])), true 71} 72 73type Aggregate struct { 74 Name string 75 Count, Errors int 76 Min, Median, Max time.Duration 77 P75, P90, P95, P99 time.Duration // percentiles 78} 79 80// NewAggregate constructs an aggregate from latencies. Returns nil if latencies does not contain aggregateable data. 81func NewAggregate(name string, latencies []time.Duration, errorCount int) *Aggregate { 82 agg := Aggregate{Name: name, Count: len(latencies), Errors: errorCount} 83 84 if len(latencies) == 0 { 85 return nil 86 } 87 var ok bool 88 if agg.Min, ok = quantile(latencies, 0, 2); !ok { 89 return nil 90 } 91 if agg.Median, ok = quantile(latencies, 1, 2); !ok { 92 return nil 93 } 94 if agg.Max, ok = quantile(latencies, 2, 2); !ok { 95 return nil 96 } 97 if agg.P75, ok = quantile(latencies, 75, 100); !ok { 98 return nil 99 } 100 if agg.P90, ok = quantile(latencies, 90, 100); !ok { 101 return nil 102 } 103 if agg.P95, ok = quantile(latencies, 95, 100); !ok { 104 return nil 105 } 106 if agg.P99, ok = quantile(latencies, 99, 100); !ok { 107 return nil 108 } 109 return &agg 110} 111 112func (agg *Aggregate) String() string { 113 if agg == nil { 114 return "no data" 115 } 116 var buf bytes.Buffer 117 tw := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0) // one-space padding 118 fmt.Fprintf(tw, "min:\t%v\nmedian:\t%v\nmax:\t%v\n95th percentile:\t%v\n99th percentile:\t%v\n", 119 agg.Min, agg.Median, agg.Max, agg.P95, agg.P99) 120 tw.Flush() 121 return buf.String() 122} 123 124// WriteCSV writes a csv file to the given Writer, 125// with a header row and one row per aggregate. 126func WriteCSV(aggs []*Aggregate, iow io.Writer) (err error) { 127 w := csv.NewWriter(iow) 128 defer func() { 129 w.Flush() 130 if err == nil { 131 err = w.Error() 132 } 133 }() 134 err = w.Write([]string{"name", "count", "errors", "min", "median", "max", "p75", "p90", "p95", "p99"}) 135 if err != nil { 136 return err 137 } 138 for _, agg := range aggs { 139 err = w.Write([]string{ 140 agg.Name, strconv.Itoa(agg.Count), strconv.Itoa(agg.Errors), 141 agg.Min.String(), agg.Median.String(), agg.Max.String(), 142 agg.P75.String(), agg.P90.String(), agg.P95.String(), agg.P99.String(), 143 }) 144 if err != nil { 145 return err 146 } 147 } 148 return nil 149} 150