...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package report
18
19 import (
20 "time"
21 )
22
23 type weightedReport struct {
24 baseReport Report
25
26 report *report
27 results chan Result
28 weightTotal float64
29 }
30
31
32
33 func NewWeightedReport(r Report, precision string) Report {
34 return &weightedReport{
35 baseReport: r,
36 report: newReport(precision),
37 results: make(chan Result, 16),
38 }
39 }
40
41 func (wr *weightedReport) Results() chan<- Result { return wr.results }
42
43 func (wr *weightedReport) Run() <-chan string {
44 donec := make(chan string, 2)
45 go func() {
46 defer close(donec)
47 basec, rc := make(chan string, 1), make(chan Stats, 1)
48 go func() { basec <- (<-wr.baseReport.Run()) }()
49 go func() { rc <- (<-wr.report.Stats()) }()
50 go wr.processResults()
51 wr.report.stats = wr.reweighStat(<-rc)
52 donec <- wr.report.String()
53 donec <- (<-basec)
54 }()
55 return donec
56 }
57
58 func (wr *weightedReport) Stats() <-chan Stats {
59 donec := make(chan Stats, 2)
60 go func() {
61 defer close(donec)
62 basec, rc := make(chan Stats, 1), make(chan Stats, 1)
63 go func() { basec <- (<-wr.baseReport.Stats()) }()
64 go func() { rc <- (<-wr.report.Stats()) }()
65 go wr.processResults()
66 donec <- wr.reweighStat(<-rc)
67 donec <- (<-basec)
68 }()
69 return donec
70 }
71
72 func (wr *weightedReport) processResults() {
73 defer close(wr.report.results)
74 defer close(wr.baseReport.Results())
75 for res := range wr.results {
76 wr.processResult(res)
77 wr.baseReport.Results() <- res
78 }
79 }
80
81 func (wr *weightedReport) processResult(res Result) {
82 if res.Err != nil {
83 wr.report.results <- res
84 return
85 }
86 if res.Weight == 0 {
87 res.Weight = 1.0
88 }
89 wr.weightTotal += res.Weight
90 res.End = res.Start.Add(time.Duration(float64(res.End.Sub(res.Start)) / res.Weight))
91 res.Weight = 1.0
92 wr.report.results <- res
93 }
94
95 func (wr *weightedReport) reweighStat(s Stats) Stats {
96 weightCoef := wr.weightTotal / float64(len(s.Lats))
97
98 s.RPS *= weightCoef
99 s.AvgTotal *= weightCoef * weightCoef
100 return s
101 }
102
View as plain text