From 5502c0affe70fc70e1ca2949fbd8b554300bb660 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sat, 8 Apr 2017 13:57:33 +0200 Subject: [PATCH] Fix airtime calculation --- data/airtime.go | 36 +++++++++------------- data/airtime_test.go | 73 +++++++++++++++++++++----------------------- 2 files changed, 50 insertions(+), 59 deletions(-) diff --git a/data/airtime.go b/data/airtime.go index 6533963..884e791 100644 --- a/data/airtime.go +++ b/data/airtime.go @@ -1,8 +1,6 @@ package data -import ( - "math" -) +import "math" // Wireless struct type Wireless struct { @@ -29,7 +27,7 @@ type WirelessAirtime struct { Frequency uint32 `json:"frequency"` } -// FrequencyName to 11g or 11a +// FrequencyName returns 11g or 11a func (airtime WirelessAirtime) FrequencyName() string { if airtime.Frequency < 5000 { return "11g" @@ -37,32 +35,28 @@ func (airtime WirelessAirtime) FrequencyName() string { return "11a" } -// SetUtilization Calculates the utilization values in regard to the previous values +// SetUtilization calculates the utilization values in regard to the previous values func (current WirelessStatistics) SetUtilization(previous WirelessStatistics) { for _, c := range current { for _, p := range previous { - if c.Frequency == p.Frequency { - c.SetUtilization(p) + // Same frequency and time passed? + if c.Frequency == p.Frequency && p.ActiveTime < c.ActiveTime { + c.setUtilization(p) } } } } -// SetUtilization Calculates the utilization values in regard to the previous values -func (airtime *WirelessAirtime) SetUtilization(prev *WirelessAirtime) { - if airtime.ActiveTime <= prev.ActiveTime { - return - } - +// setUtilization updates the utilization values in regard to the previous values +func (airtime *WirelessAirtime) setUtilization(prev *WirelessAirtime) { + // Calculate deltas active := float64(airtime.ActiveTime) - float64(prev.ActiveTime) busy := float64(airtime.BusyTime) - float64(prev.BusyTime) - rx := float64(airtime.TxTime) - float64(prev.TxTime) - tx := float64(airtime.RxTime) - float64(prev.RxTime) + rx := float64(airtime.RxTime) - float64(prev.RxTime) + tx := float64(airtime.TxTime) - float64(prev.TxTime) - // Calculate utilizations - if active > 0 { - airtime.ChanUtil = float32(math.Min(100, 100*(busy+rx+tx)/active)) - airtime.RxUtil = float32(math.Min(100, 100*rx/active)) - airtime.TxUtil = float32(math.Min(100, 100*tx/active)) - } + // Update utilizations + airtime.ChanUtil = float32(math.Min(100, 100*busy/active)) + airtime.RxUtil = float32(math.Min(100, 100*rx/active)) + airtime.TxUtil = float32(math.Min(100, 100*tx/active)) } diff --git a/data/airtime_test.go b/data/airtime_test.go index e5ecfb7..63e51f6 100644 --- a/data/airtime_test.go +++ b/data/airtime_test.go @@ -19,62 +19,59 @@ func TestUtilization(t *testing.T) { t1 := &WirelessAirtime{ ActiveTime: 20, - BusyTime: 0, + BusyTime: 5, TxTime: 5, RxTime: 0, } t2 := &WirelessAirtime{ ActiveTime: 120, - BusyTime: 10, + BusyTime: 50, TxTime: 25, RxTime: 15, } - t3 := &WirelessAirtime{ - ActiveTime: 200, - BusyTime: 40, - TxTime: 35, - RxTime: 15, - } - t1.SetUtilization(t2) - assert.Zero(t1.ChanUtil) - assert.Zero(t1.TxUtil) - assert.Zero(t1.RxUtil) + t1.setUtilization(t1) + assert.NotZero(t1.ChanUtil) + assert.EqualValues(0, t2.ChanUtil) + assert.EqualValues(0, t2.RxUtil) + assert.EqualValues(0, t2.TxUtil) - t2.SetUtilization(t1) + t2.setUtilization(t1) assert.NotZero(t2.ChanUtil) assert.EqualValues(45, t2.ChanUtil) - assert.EqualValues(20, t2.RxUtil) - assert.EqualValues(15, t2.TxUtil) - - t3.SetUtilization(t2) - assert.EqualValues(50, t3.ChanUtil) - assert.EqualValues(12.5, t3.RxUtil) - assert.EqualValues(0, t3.TxUtil) + assert.EqualValues(15, t2.RxUtil) + assert.EqualValues(20, t2.TxUtil) } func TestWirelessStatistics(t *testing.T) { assert := assert.New(t) + // This is the current value stats := WirelessStatistics([]*WirelessAirtime{{ - Frequency: 2400, - ActiveTime: 20, - TxTime: 10, - }}) - - // Different Frequency, should not change anything - stats.SetUtilization([]*WirelessAirtime{{ - Frequency: 5000, - ActiveTime: 15, - TxTime: 1, - }}) - assert.EqualValues(0, stats[0].ChanUtil) - - // Same Frequency, should set the utilization - stats.SetUtilization([]*WirelessAirtime{{ - Frequency: 2400, + Frequency: 2400, ActiveTime: 10, - TxTime: 5, + BusyTime: 4, + RxTime: 3, }}) - assert.EqualValues(50, stats[0].ChanUtil) + + // previous value: Different Frequency, should not change anything + stats.SetUtilization([]*WirelessAirtime{{ + Frequency: 5000, + ActiveTime: 5, + RxTime: 1, + }}) + assert.Zero(0, stats[0].ChanUtil) + assert.Zero(0, stats[0].RxUtil) + assert.Zero(0, stats[0].TxUtil) + + // previous value: Same Frequency, should set the utilization + stats.SetUtilization([]*WirelessAirtime{{ + Frequency: 2400, + ActiveTime: 5, + RxTime: 2, + BusyTime: 1, + }}) + assert.EqualValues(60, stats[0].ChanUtil) + assert.EqualValues(20, stats[0].RxUtil) + assert.EqualValues(0, stats[0].TxUtil) }