Merged in BitrateCalculatorPackage (pull request #7)

BitrateCalculatorPackage

Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
saxon.milton@gmail.com 2017-12-06 11:03:59 +00:00 committed by Alan Noble
commit 4a865c0364
2 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,72 @@
/*
NAME
BitrateCalculator.go - is a simple struct with methods to allow for easy
calculation of bitrate.
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
BitrateCalculator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package bitrate
import (
"fmt"
"time"
)
// BitrateCalculator provides fields and methods to allow calculation of bitrate
type BitrateCalculator struct {
outputDelay int // sec
now time.Time
prev time.Time
startedBefore bool
elapsedTime time.Duration
lastDisplayTime time.Time
}
// Place this at the start of the code segment that you would like to time
func (bc *BitrateCalculator) Start(outputDelay int) {
if outputDelay >= 0 {
bc.outputDelay = outputDelay
} else {
bc.outputDelay = 0
}
bc.prev = time.Now()
if !bc.startedBefore {
bc.startedBefore = true
bc.elapsedTime = time.Duration(0)
bc.lastDisplayTime = time.Now()
}
}
// Place this at the end of the code segment that you would like to time
func (bc *BitrateCalculator) Stop(noOfKB float64) (bitrate int64) {
bc.now = time.Now()
deltaTime := bc.now.Sub(bc.prev)
if bc.now.Sub(bc.lastDisplayTime) > time.Duration(bc.outputDelay)*time.Second {
bitrate = int64(noOfKB / float64(deltaTime/1e9))
fmt.Printf("Bitrate: %d kbps\n", bitrate)
bc.elapsedTime = time.Duration(0)
bc.lastDisplayTime = bc.now
}
return
}

View File

@ -0,0 +1,77 @@
/*
NAME
BitrateCalculator_test.go - is a file that may be used to test the
BitrateCalculator.go file using the golang testing utilities
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
BitrateCalculator_test.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package bitrate
import (
"testing"
"time"
)
// Some consts used over duration of testing
const (
bitrateDelay1 = 0 // s
bitrateDelay2 = 5 // s
amountOfData = 100000.0
testTime = 2500.0 // ms
)
// This will be the BitrateCalculator object we use over testing
var bitrateCalc BitrateCalculator
// Simple test to check that the calculator can calc bitrate over a given
// duration of time
func Test1(t *testing.T) {
bitrateCalc = BitrateCalculator{}
bitrateCalc.Start(bitrateDelay1)
time.Sleep(testTime * time.Millisecond)
currentBitrate := int64(bitrateCalc.Stop(amountOfData))
actualBitrate := int64(amountOfData / ((testTime * time.Millisecond) / 1e9))
if currentBitrate != actualBitrate {
t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate)
}
}
// Now let's check that the output delay feature works
func Test2(t *testing.T) {
bitrateCalc = BitrateCalculator{}
var currentBitrate int64
for i := 0; i < 2; i++ {
bitrateCalc.Start(bitrateDelay2)
time.Sleep(testTime * time.Millisecond)
currentBitrate = int64(bitrateCalc.Stop(amountOfData))
if i == 0 && currentBitrate != 0 {
t.Errorf("The bitrate calc did not delay outputting!")
}
time.Sleep(6000 * time.Millisecond)
}
actualBitrate := int64(amountOfData / ((testTime * time.Millisecond) / 1e9))
if currentBitrate != actualBitrate {
t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate)
}
}