|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/rancher/fleet/benchmarks/cmd/parser" |
| 8 | + |
| 9 | + "gonum.org/v1/gonum/stat" |
| 10 | +) |
| 11 | + |
| 12 | +type Dataset map[string]map[string]Measurements |
| 13 | + |
| 14 | +// Measurements contains all measurements for an experiment in the population |
| 15 | +// and some statistics. |
| 16 | +type Measurements struct { |
| 17 | + Mean float64 |
| 18 | + StdDev float64 |
| 19 | + ZScore float64 |
| 20 | + Values []float64 |
| 21 | +} |
| 22 | + |
| 23 | +type scoresByXP map[string]scores |
| 24 | + |
| 25 | +type scores struct { |
| 26 | + ZScores []float64 |
| 27 | + Weights []float64 |
| 28 | + MeanZScore float64 |
| 29 | +} |
| 30 | + |
| 31 | +func (s scoresByXP) AvgZScores() float64 { |
| 32 | + zscores := []float64{} |
| 33 | + for _, xp := range s { |
| 34 | + zscores = append(zscores, xp.ZScores...) |
| 35 | + } |
| 36 | + |
| 37 | + return stat.Mean(zscores, nil) |
| 38 | +} |
| 39 | + |
| 40 | +func skip(name string) bool { |
| 41 | + switch name { |
| 42 | + case "GCDuration", "Mem", "MemDuring", "ResourceCount": |
| 43 | + return true |
| 44 | + |
| 45 | + } |
| 46 | + return strings.HasPrefix(name, "RESTClient") |
| 47 | +} |
| 48 | + |
| 49 | +// transformDataset takes a sample and transforms it into a dataset. |
| 50 | +// The output is organized by experiment, for example: |
| 51 | +// |
| 52 | +// { "50-gitrepo": { |
| 53 | +// "CPU": { "mean": 0.5, "stddev": 0.1, values: [0.4, 0.5, 0.6] }, |
| 54 | +// "GC": { "mean": 0.5, "stddev": 0.1, values: [0.4, 0.5, 0.6] }, |
| 55 | +// }, |
| 56 | +// "50-bundle": { |
| 57 | +// "CPU": { "mean": 0.5, "stddev": 0.1, values: [0.4, 0.5, 0.6] }, |
| 58 | +// "GC": { "mean": 0.5, "stddev": 0.1, values: [0.4, 0.5, 0.6] }, |
| 59 | +// }, |
| 60 | +// } |
| 61 | +func transformDataset(ds Dataset, sample parser.Sample) { |
| 62 | + for name, experiment := range sample.Experiments { |
| 63 | + for measurement, value := range experiment.Measurements { |
| 64 | + if _, ok := ds[name]; !ok { |
| 65 | + ds[name] = map[string]Measurements{} |
| 66 | + } |
| 67 | + if _, ok := ds[name][measurement]; !ok { |
| 68 | + ds[name][measurement] = Measurements{ |
| 69 | + Values: []float64{}, |
| 70 | + } |
| 71 | + } |
| 72 | + tmp := ds[name][measurement] |
| 73 | + tmp.Values = append(tmp.Values, value.Value) |
| 74 | + ds[name][measurement] = tmp |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// calculate calculates the mean, stddev of the measurement. It calculates the zscore of the sample. |
| 80 | +// This mutates dsPop and scores. |
| 81 | +func calculate(sample *parser.Sample, dsPop Dataset, scores scoresByXP) { |
| 82 | + // foreach experiment in population, calculate mean and stddev |
| 83 | + for experiment, xp := range dsPop { |
| 84 | + for measurement, sg := range xp { |
| 85 | + mean, stddev := stat.MeanStdDev(sg.Values, nil) |
| 86 | + if math.IsNaN(stddev) || stddev == 0 { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + if _, ok := sample.Experiments[experiment]; !ok { |
| 91 | + //fmt.Printf("missing experiment %s\n", name) |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + if _, ok := sample.Experiments[experiment].Measurements[measurement]; !ok { |
| 96 | + //fmt.Printf("missing measurement %s for experiments %s\n", measurement, name) |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if skip(measurement) { |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + // calculate zscore |
| 105 | + m := sample.Experiments[experiment].Measurements[measurement] |
| 106 | + zscore := stat.StdScore(m.Value, mean, stddev) |
| 107 | + //fmt.Printf("zscore %s - %s %v %v %v\n", experiment, measurement, m, mean, zscore) |
| 108 | + |
| 109 | + // store in dsPop |
| 110 | + sg.Mean = mean |
| 111 | + sg.StdDev = stddev |
| 112 | + sg.ZScore = zscore |
| 113 | + dsPop[experiment][measurement] = sg |
| 114 | + |
| 115 | + // store to summarize by experiment |
| 116 | + xp := scores[experiment] |
| 117 | + xp.ZScores = append(xp.ZScores, zscore) |
| 118 | + xp.Weights = append(xp.Weights, weight(measurement)) |
| 119 | + scores[experiment] = xp |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Summarize experiments |
| 124 | + for name, xp := range scores { |
| 125 | + avg := stat.Mean(xp.ZScores, xp.Weights) |
| 126 | + xp.MeanZScore = avg |
| 127 | + scores[name] = xp |
| 128 | + //fmt.Printf("%s %v %v %v\n", name, avg, xp.ZScores, xp.Weights) |
| 129 | + } |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | +// Some measurements have a higher volatility than others, or are duplicated. |
| 134 | +// Only TotalDuration is used, as it is shown in :he result table. |
| 135 | +// |
| 136 | +// "CPU": 14.029999999999973, |
| 137 | +// "GCDuration": 1.9185229570000004, |
| 138 | +// "Mem": 4, |
| 139 | +// "MemDuring": 4, |
| 140 | +// "NetworkRX": 68288672, |
| 141 | +// "NetworkTX": 30662826, |
| 142 | +// "ReconcileErrors": 0, |
| 143 | +// "ReconcileRequeue": 65, |
| 144 | +// "ReconcileRequeueAfter": 462, |
| 145 | +// "ReconcileSuccess": 2329, |
| 146 | +// "ReconcileTime": 8153.420151420956, |
| 147 | +// "ResourceCount": 300, |
| 148 | +// "WorkqueueAdds": 2844, |
| 149 | +// "WorkqueueQueueDuration": 3911.157310051014, |
| 150 | +// "WorkqueueRetries": 527, |
| 151 | +// "WorkqueueWorkDuration": 8169.425508522996 |
| 152 | +func weight(name string) float64 { |
| 153 | + if name == "TotalDuration" { |
| 154 | + return 1.0 |
| 155 | + } |
| 156 | + |
| 157 | + return 0.0 |
| 158 | +} |
0 commit comments