From b9af48c7fc431b678aacd39f205f3be3a2d21bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Bach=C3=A9?= Date: Sun, 7 Apr 2019 20:23:41 -0700 Subject: [PATCH] Fixed stats copy --- internal/session/session.go | 9 +++++---- pkg/session/bench/benchmark.go | 10 ++++++---- pkg/session/sender/sender.go | 17 +++++++++-------- pkg/stats/bytes_test.go | 2 +- pkg/stats/ctrl_test.go | 2 +- pkg/stats/data_test.go | 4 ++-- pkg/stats/stats.go | 9 ++++++++- 7 files changed, 32 insertions(+), 21 deletions(-) diff --git a/internal/session/session.go b/internal/session/session.go index 033e295..d762739 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -16,7 +16,7 @@ type CompletionHandler func() // Session contains common elements to perform send/receive type Session struct { Done chan struct{} - NetworkStats stats.Stats + NetworkStats *stats.Stats sdpInput io.Reader sdpOutput io.Writer peerConnection *webrtc.PeerConnection @@ -32,9 +32,10 @@ func New(sdpInput io.Reader, sdpOutput io.Writer) Session { sdpOutput = os.Stdout } return Session{ - sdpInput: sdpInput, - sdpOutput: sdpOutput, - Done: make(chan struct{}), + sdpInput: sdpInput, + sdpOutput: sdpOutput, + Done: make(chan struct{}), + NetworkStats: stats.New(), } } diff --git a/pkg/session/bench/benchmark.go b/pkg/session/bench/benchmark.go index 6b9fc8d..5c4854a 100644 --- a/pkg/session/bench/benchmark.go +++ b/pkg/session/bench/benchmark.go @@ -27,9 +27,9 @@ type Session struct { testDurationError time.Duration startPhase2 chan struct{} - uploadNetworkStats stats.Stats + uploadNetworkStats *stats.Stats downloadDone chan bool - downloadNetworkStats stats.Stats + downloadNetworkStats *stats.Stats } // New creates a new sender session @@ -42,8 +42,10 @@ func new(s internalSess.Session, isMaster bool) *Session { testDuration: testDurationDefault, testDurationError: testDurationErrorDefault, - startPhase2: make(chan struct{}), - downloadDone: make(chan bool), + startPhase2: make(chan struct{}), + downloadDone: make(chan bool), + uploadNetworkStats: stats.New(), + downloadNetworkStats: stats.New(), } } diff --git a/pkg/session/sender/sender.go b/pkg/session/sender/sender.go index 8683661..6dec882 100644 --- a/pkg/session/sender/sender.go +++ b/pkg/session/sender/sender.go @@ -36,19 +36,20 @@ type Session struct { doneCheck bool // Stats/infos - readingStats stats.Stats + readingStats *stats.Stats } // New creates a new sender session func new(s internalSess.Session, f io.Reader) *Session { return &Session{ - sess: s, - stream: f, - initialized: false, - dataBuff: make([]byte, senderBuffSize), - stopSending: make(chan struct{}, 1), - output: make(chan outputMsg, senderBuffSize*10), - doneCheck: false, + sess: s, + stream: f, + initialized: false, + dataBuff: make([]byte, senderBuffSize), + stopSending: make(chan struct{}, 1), + output: make(chan outputMsg, senderBuffSize*10), + doneCheck: false, + readingStats: stats.New(), } } diff --git a/pkg/stats/bytes_test.go b/pkg/stats/bytes_test.go index 32dae08..085238f 100644 --- a/pkg/stats/bytes_test.go +++ b/pkg/stats/bytes_test.go @@ -31,7 +31,7 @@ func Test_Bytes(t *testing.T) { }, } - s := Stats{} + s := New() for _, cur := range tests { assert.Equal(cur.before, s.Bytes()) s.AddBytes(cur.add) diff --git a/pkg/stats/ctrl_test.go b/pkg/stats/ctrl_test.go index 6b930c9..0fbb6c4 100644 --- a/pkg/stats/ctrl_test.go +++ b/pkg/stats/ctrl_test.go @@ -9,7 +9,7 @@ import ( func Test_ControlFlow(t *testing.T) { assert := assert.New(t) - s := Stats{} + s := New() // Everything should be 0 at the beginning assert.Equal(true, s.timeStart.IsZero()) diff --git a/pkg/stats/data_test.go b/pkg/stats/data_test.go index dd87c95..551dec9 100644 --- a/pkg/stats/data_test.go +++ b/pkg/stats/data_test.go @@ -10,7 +10,7 @@ import ( func Test_Bandwidth(t *testing.T) { assert := assert.New(t) - s := Stats{} + s := New() now := time.Now() tests := []struct { @@ -66,7 +66,7 @@ func Test_Bandwidth(t *testing.T) { func Test_Duration(t *testing.T) { assert := assert.New(t) - s := Stats{} + s := New() // Should be 0 assert.Equal(time.Duration(0), s.Duration()) diff --git a/pkg/stats/stats.go b/pkg/stats/stats.go index 4095612..e6fba76 100644 --- a/pkg/stats/stats.go +++ b/pkg/stats/stats.go @@ -8,7 +8,7 @@ import ( // Stats provide a way to track statistics infos type Stats struct { - lock sync.RWMutex + lock *sync.RWMutex nbBytes uint64 timeStart time.Time timeStop time.Time @@ -17,6 +17,13 @@ type Stats struct { timePaused time.Duration } +// New creates a new Stats +func New() *Stats { + return &Stats{ + lock: &sync.RWMutex{}, + } +} + func (s *Stats) String() string { s.lock.RLock() defer s.lock.RUnlock()