From 742f92b56b244df903819a4255620026feb2f5e4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Dec 2017 12:53:22 +1030 Subject: [PATCH] testing file in progress --- bitrate/BitrateCalculator.go | 87 +++++++++++++++++++++---------- bitrate/BitrateCalculator_test.go | 69 +++++++++++++++++++++++- 2 files changed, 128 insertions(+), 28 deletions(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index fea3bc1f..2f6fc528 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -1,38 +1,71 @@ +/* +NAME + revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + +DESCRIPTION + See Readme.md + +AUTHOR + Alan Noble + +LICENSE + revid is Copyright (C) 2017 Alan Noble. + + 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 ( - "time" - "fmt" + "fmt" + "time" ) +// bitrateCalculator implements interface BitrateCalculator type BitrateCalculator struct { - Delay int // sec - now - prev - isFirstTime bool - elapsedTime time.Time + outputDelay int // sec + now time.Time + prev time.Time + startedBefore bool + elapsedTime time.Duration + lastDisplayTime time.Time } -// The bitrate calculator -func (bc *BitrateCalculator) Start() { - if bc.isFirstTime { - if Delay == nil { - bc.Delay = 5 * time.Second - } - bc.now = time.Now() - bc.prev = now - bc.isFirstTime = false - bc.elapsedTime = time.Duration(0) +// 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() -} - -func (bc *BitrateCalculator) Stop(noOfKB int) (bitrate int) { - deltaTime := now.Sub(prevTime) - bc.elapsedTime += deltaTime - if bc.elapsedTime > bc.Delay*time.Second { - fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9))) - bc.elapsedTime = time.Duration(0) - } - bc.prev = 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 } diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go index 617d30b5..506e423e 100644 --- a/bitrate/BitrateCalculator_test.go +++ b/bitrate/BitrateCalculator_test.go @@ -1,5 +1,72 @@ +/* +NAME + revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + +DESCRIPTION + See Readme.md + +AUTHOR + Alan Noble + +LICENSE + revid is Copyright (C) 2017 Alan Noble. + + 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" + "testing" + "time" + "fmt" ) + +// 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{} + for i := 0; i < 2; i++ { + bitrateCalc.Start(bitrate2) + time.Sleep(testTime*time.Millisecond) + currentBitrate := int64(bitrateCalc.Stop(amountOfData)) + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + if i == 0 && currentBitrate != 0 { + t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) + } + } +}