forked from jmptrader/ascender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statser.go
51 lines (43 loc) · 819 Bytes
/
statser.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
49
50
51
// 2014, 1015 Jamie Alquiza
package main
import (
"log"
"time"
)
type Statser struct {
value chan int64
}
func NewStatser() *Statser {
s := &Statser{make(chan int64, 1)}
s.init()
return s
}
func (s *Statser) init() {
s.value <- 0
}
func (s *Statser) IncrSent(v int64) {
i := <-s.value
s.value <- i + v
}
func (s *Statser) FetchSent() int64 {
i := <-s.value
s.value <- i
return i
}
// Outputs periodic info summary.
func statsTracker(s *Statser) {
tick := time.Tick(5 * time.Second)
var currCnt, lastCnt int64
for {
<-tick
lastCnt = currCnt
currCnt = s.FetchSent()
deltaCnt := currCnt - lastCnt
if deltaCnt > 0 {
log.Printf("Last 5s: sent %d messages | Avg: %.2f messages/sec. | Send queue length: %d\n",
deltaCnt,
float64(deltaCnt)/5,
len(messageIncomingQueue))
}
}
}