Fix airtime calculation

This commit is contained in:
Julian Kornberger 2017-04-08 13:57:33 +02:00 committed by Julian K
parent b131fac626
commit 5502c0affe
2 changed files with 50 additions and 59 deletions

View File

@ -1,8 +1,6 @@
package data package data
import ( import "math"
"math"
)
// Wireless struct // Wireless struct
type Wireless struct { type Wireless struct {
@ -29,7 +27,7 @@ type WirelessAirtime struct {
Frequency uint32 `json:"frequency"` Frequency uint32 `json:"frequency"`
} }
// FrequencyName to 11g or 11a // FrequencyName returns 11g or 11a
func (airtime WirelessAirtime) FrequencyName() string { func (airtime WirelessAirtime) FrequencyName() string {
if airtime.Frequency < 5000 { if airtime.Frequency < 5000 {
return "11g" return "11g"
@ -37,32 +35,28 @@ func (airtime WirelessAirtime) FrequencyName() string {
return "11a" 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) { func (current WirelessStatistics) SetUtilization(previous WirelessStatistics) {
for _, c := range current { for _, c := range current {
for _, p := range previous { for _, p := range previous {
if c.Frequency == p.Frequency { // Same frequency and time passed?
c.SetUtilization(p) if c.Frequency == p.Frequency && p.ActiveTime < c.ActiveTime {
c.setUtilization(p)
} }
} }
} }
} }
// SetUtilization Calculates the utilization values in regard to the previous values // setUtilization updates the utilization values in regard to the previous values
func (airtime *WirelessAirtime) SetUtilization(prev *WirelessAirtime) { func (airtime *WirelessAirtime) setUtilization(prev *WirelessAirtime) {
if airtime.ActiveTime <= prev.ActiveTime { // Calculate deltas
return
}
active := float64(airtime.ActiveTime) - float64(prev.ActiveTime) active := float64(airtime.ActiveTime) - float64(prev.ActiveTime)
busy := float64(airtime.BusyTime) - float64(prev.BusyTime) busy := float64(airtime.BusyTime) - float64(prev.BusyTime)
rx := float64(airtime.TxTime) - float64(prev.TxTime) rx := float64(airtime.RxTime) - float64(prev.RxTime)
tx := float64(airtime.RxTime) - float64(prev.RxTime) tx := float64(airtime.TxTime) - float64(prev.TxTime)
// Calculate utilizations // Update utilizations
if active > 0 { airtime.ChanUtil = float32(math.Min(100, 100*busy/active))
airtime.ChanUtil = float32(math.Min(100, 100*(busy+rx+tx)/active)) airtime.RxUtil = float32(math.Min(100, 100*rx/active))
airtime.RxUtil = float32(math.Min(100, 100*rx/active)) airtime.TxUtil = float32(math.Min(100, 100*tx/active))
airtime.TxUtil = float32(math.Min(100, 100*tx/active))
}
} }

View File

@ -19,62 +19,59 @@ func TestUtilization(t *testing.T) {
t1 := &WirelessAirtime{ t1 := &WirelessAirtime{
ActiveTime: 20, ActiveTime: 20,
BusyTime: 0, BusyTime: 5,
TxTime: 5, TxTime: 5,
RxTime: 0, RxTime: 0,
} }
t2 := &WirelessAirtime{ t2 := &WirelessAirtime{
ActiveTime: 120, ActiveTime: 120,
BusyTime: 10, BusyTime: 50,
TxTime: 25, TxTime: 25,
RxTime: 15, RxTime: 15,
} }
t3 := &WirelessAirtime{
ActiveTime: 200,
BusyTime: 40,
TxTime: 35,
RxTime: 15,
}
t1.SetUtilization(t2) t1.setUtilization(t1)
assert.Zero(t1.ChanUtil) assert.NotZero(t1.ChanUtil)
assert.Zero(t1.TxUtil) assert.EqualValues(0, t2.ChanUtil)
assert.Zero(t1.RxUtil) assert.EqualValues(0, t2.RxUtil)
assert.EqualValues(0, t2.TxUtil)
t2.SetUtilization(t1) t2.setUtilization(t1)
assert.NotZero(t2.ChanUtil) assert.NotZero(t2.ChanUtil)
assert.EqualValues(45, t2.ChanUtil) assert.EqualValues(45, t2.ChanUtil)
assert.EqualValues(20, t2.RxUtil) assert.EqualValues(15, t2.RxUtil)
assert.EqualValues(15, t2.TxUtil) assert.EqualValues(20, t2.TxUtil)
t3.SetUtilization(t2)
assert.EqualValues(50, t3.ChanUtil)
assert.EqualValues(12.5, t3.RxUtil)
assert.EqualValues(0, t3.TxUtil)
} }
func TestWirelessStatistics(t *testing.T) { func TestWirelessStatistics(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// This is the current value
stats := WirelessStatistics([]*WirelessAirtime{{ stats := WirelessStatistics([]*WirelessAirtime{{
Frequency: 2400, 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,
ActiveTime: 10, 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)
} }