From 8d6d92bac594e18cb0690b595e6399107bf9512a Mon Sep 17 00:00:00 2001 From: Michael Berlin Date: Wed, 24 Aug 2016 10:51:16 -0700 Subject: [PATCH] throttler: Fix the aggregated average historic value if multiple threads are used. (#1985) --- go/vt/throttler/aggregated_interval_history.go | 2 +- .../aggregated_interval_history_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 go/vt/throttler/aggregated_interval_history_test.go diff --git a/go/vt/throttler/aggregated_interval_history.go b/go/vt/throttler/aggregated_interval_history.go index 3dbd00217e..ccfb391fee 100644 --- a/go/vt/throttler/aggregated_interval_history.go +++ b/go/vt/throttler/aggregated_interval_history.go @@ -35,5 +35,5 @@ func (h *aggregatedIntervalHistory) average(from, to time.Time) float64 { for i := 0; i < h.threadCount; i++ { sum += h.historyPerThread[i].average(from, to) } - return sum / float64(h.threadCount) + return sum } diff --git a/go/vt/throttler/aggregated_interval_history_test.go b/go/vt/throttler/aggregated_interval_history_test.go new file mode 100644 index 0000000000..8b861cdf6a --- /dev/null +++ b/go/vt/throttler/aggregated_interval_history_test.go @@ -0,0 +1,16 @@ +package throttler + +import ( + "testing" + "time" +) + +func TestAggregatedIntervalHistory(t *testing.T) { + h := newAggregatedIntervalHistory(10, 1*time.Second, 2) + h.addPerThread(0, record{sinceZero(0 * time.Second), 1000}) + h.addPerThread(1, record{sinceZero(0 * time.Second), 2000}) + + if got, want := h.average(sinceZero(250*time.Millisecond), sinceZero(750*time.Millisecond)), 3000.0; got != want { + t.Errorf("average(0.25s, 0.75s) across both threads = %v, want = %v", got, want) + } +}