-
Notifications
You must be signed in to change notification settings - Fork 2
/
pub_test.go
64 lines (55 loc) · 1.51 KB
/
pub_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
package mockconn
import (
"encoding/binary"
"log"
"net"
"time"
)
func WritePacktes(writer net.Conn, nPackets int, sendCh chan []int64) {
var sendSeq []int64
seq := int64(1)
count := int64(0)
t1 := time.Now()
for i := 0; i < nPackets; i++ {
b := make([]byte, 1024)
binary.PutVarint(b, seq)
_, err := writer.Write(b)
if err != nil {
break
}
sendSeq = append(sendSeq, seq)
seq++
count++
}
dur := time.Since(t1)
throughput := float64(count) / dur.Seconds()
log.Printf("%v send %v packets in %v ms, throughput is: %.1f packets/s \n",
writer.LocalAddr(), count, dur.Milliseconds(), throughput)
sendCh <- sendSeq // return the sequences written
}
func ReadPackets(reader net.Conn, nPackets int, latency time.Duration, recvCh chan []int64) {
var recvSeq []int64
b := make([]byte, 1024)
count := int64(0)
t1 := time.Now()
for i := 0; i < nPackets; i++ {
_, err := reader.Read(b)
if err != nil {
break
}
seq, _ := binary.Varint(b[:8])
recvSeq = append(recvSeq, seq)
count++
}
dur := time.Since(t1)
dur2 := dur - latency
throughput := float64(count) / dur2.Seconds()
log.Printf("%v read %v packets in %v ms, deduct %v ms latency, throughput is: %.1f packets/s \n",
reader.LocalAddr(), count, dur.Milliseconds(), latency.Milliseconds(), throughput)
if uc, ok := reader.(*UniConn); ok {
uc.PrintMetrics()
} else if nc, ok := reader.(*NetConn); ok {
nc.PrintMetrics()
}
recvCh <- recvSeq // return the sequences read
}