Normalize normalizes the source profile by multiplying each value in profile by the ratio of the sum of the base profile's values of that sample type to the sum of the source profile's value of that sample type.
(pb *Profile)
| 92 | // ratio of the sum of the base profile's values of that sample type to the sum of the |
| 93 | // source profile's value of that sample type. |
| 94 | func (p *Profile) Normalize(pb *Profile) error { |
| 95 | |
| 96 | if err := p.compatible(pb); err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | baseVals := make([]int64, len(p.SampleType)) |
| 101 | for _, s := range pb.Sample { |
| 102 | for i, v := range s.Value { |
| 103 | baseVals[i] += v |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | srcVals := make([]int64, len(p.SampleType)) |
| 108 | for _, s := range p.Sample { |
| 109 | for i, v := range s.Value { |
| 110 | srcVals[i] += v |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | normScale := make([]float64, len(baseVals)) |
| 115 | for i := range baseVals { |
| 116 | if srcVals[i] == 0 { |
| 117 | normScale[i] = 0.0 |
| 118 | } else { |
| 119 | normScale[i] = float64(baseVals[i]) / float64(srcVals[i]) |
| 120 | } |
| 121 | } |
| 122 | p.ScaleN(normScale) |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func isZeroSample(s *Sample) bool { |
| 127 | for _, v := range s.Value { |