-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_receiverware_throughput_test.go
84 lines (73 loc) · 1.72 KB
/
04_receiverware_throughput_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package go_channel__test
import (
"math/rand"
"sync/atomic"
"testing"
"time"
)
func throughputReceiverware(in <-chan string, throughput *uint64) <-chan string {
previousThroughput := uint64(0)
// a ticker to capture throughput each second
ticker := time.NewTicker(time.Second)
quit := make(chan struct{})
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
atomic.StoreUint64(throughput, previousThroughput)
atomic.StoreUint64(&previousThroughput, 0)
case <-quit:
return
}
}
}()
// we decorate the input channel
out := make(chan string, cap(in))
go func() {
defer close(quit)
defer close(out)
for data := range in {
atomic.AddUint64(&previousThroughput, 1)
out <- data
}
}()
return out
}
func Test_throughputReceiverware(t *testing.T) {
c := make(chan string, 10000)
t.Logf("channel c has %d/%d items\n", len(c), cap(c))
instantThroughput := uint64(0)
// print throughput every second
ticker := time.NewTicker(time.Second)
quit := make(chan struct{})
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.Logf("instant throughput %d", instantThroughput)
case <-quit:
return
}
}
}()
wrappedC := throughputReceiverware(c, &instantThroughput)
// this simulate your application usage of the channel data
var result []string
go func() {
for s := range wrappedC {
result = append(result, s)
}
}()
// publish data on the source channel
go func() {
for i := 0; i < 100000; i++ {
c <- ""
// add entropy in sleep time in order to have a different throughput over the time
time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond)
}
}()
time.Sleep(5 * time.Second)
t.Logf("finish with %d results", len(result))
}