forked from taggledevel2/ratchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_stat.go
48 lines (41 loc) · 1.04 KB
/
execution_stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ratchet
import (
"time"
"github.com/tsaserv/ratchet/data"
)
type executionStat struct {
dataSentCounter int
dataReceivedCounter int
executionsCounter int
totalExecutionTime float64
avgExecutionTime float64
totalBytesReceived int
avgBytesReceived int
totalBytesSent int
avgBytesSent int
}
func (s *executionStat) recordExecution(foo func()) {
s.executionsCounter++
st := time.Now()
foo()
s.totalExecutionTime += time.Now().Sub(st).Seconds()
}
func (s *executionStat) recordDataSent(d data.JSON) {
s.dataSentCounter++
s.totalBytesSent += len(d)
}
func (s *executionStat) recordDataReceived(d data.JSON) {
s.dataReceivedCounter++
s.totalBytesReceived += len(d)
}
func (s *executionStat) calculate() {
if s.executionsCounter > 0 {
s.avgExecutionTime = (s.totalExecutionTime / float64(s.executionsCounter))
}
if s.dataReceivedCounter > 0 {
s.avgBytesReceived = (s.totalBytesReceived / s.dataReceivedCounter)
}
if s.dataSentCounter > 0 {
s.avgBytesSent = (s.totalBytesSent / s.dataSentCounter)
}
}