-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
169 lines (138 loc) · 3.67 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This example is a simple benchmark measuring the throughput of broadcast
// messages.
// Run with default options: go run main.go
// Show usage: go run main.go -h
package main
import (
"flag"
pbmsg "github.com/nknorg/nnet/protobuf/message"
"os"
"os/signal"
"sync"
"time"
"github.com/nknorg/nnet"
"github.com/nknorg/nnet/log"
"github.com/nknorg/nnet/node"
"github.com/nknorg/nnet/util"
)
func create(transport string, port uint16, id []byte) (*nnet.NNet, error) {
conf := &nnet.Config{
Port: port,
Transport: transport,
NumFingerSuccessors: 1,
}
nn, err := nnet.NewNNet(id, conf)
if err != nil {
return nil, err
}
return nn, nil
}
func main() {
transportPtr := flag.String("t", "tcp", "transport type, tcp or kcp")
numNodesPtr := flag.Int("n", 2, "number of nodes")
broadcastTypePtr := flag.String("b", "tree", "broadcast type, push or tree")
msgSizePtr := flag.Int("m", 1024, "message size in bytes")
flag.Parse()
if *numNodesPtr < 2 {
log.Error("Number of nodes must be greater than 1")
return
}
var broadcastType pbmsg.RoutingType
switch *broadcastTypePtr {
case "push":
broadcastType = pbmsg.RoutingType_BROADCAST_PUSH
case "tree":
broadcastType = pbmsg.RoutingType_BROADCAST_TREE
default:
log.Error("Unknown broadcast type")
return
}
if *msgSizePtr < 1 {
log.Error("Msg size must be greater than 0")
return
}
const createPort uint16 = 23333
var nn *nnet.NNet
var id []byte
var err error
var msgCount int
var msgCountLock sync.RWMutex
nnets := make([]*nnet.NNet, 0)
for i := 0; i < *numNodesPtr; i++ {
id, err = util.RandBytes(32)
if err != nil {
log.Error(err)
return
}
nn, err = create(*transportPtr, createPort+uint16(i), id)
if err != nil {
log.Error(err)
return
}
nn.MustApplyMiddleware(node.BytesReceived{func(msg, msgID, srcID []byte, remoteNode *node.RemoteNode) ([]byte, bool) {
msgCountLock.Lock()
msgCount++
msgCountLock.Unlock()
return msg, true
}, 0})
nnets = append(nnets, nn)
}
for i := 0; i < len(nnets); i++ {
time.Sleep(112358 * time.Microsecond)
err = nnets[i].Start(i == 0)
if err != nil {
log.Error(err)
return
}
if i > 0 {
err = nnets[i].Join(nnets[0].GetLocalNode().Addr)
if err != nil {
log.Error(err)
return
}
}
}
time.Sleep(time.Duration(*numNodesPtr/5) * time.Second)
for i := 3; i > 0; i-- {
log.Infof("Sending broadcast message in %d seconds", i)
time.Sleep(time.Second)
}
log.Info("Starting...")
msg, _ := util.RandBytes(*msgSizePtr)
go func() {
for {
nnets[0].SendBytesBroadcastAsync(msg, broadcastType)
}
}()
time.Sleep(time.Second)
msgCountLock.RLock()
msgCountHistory := []int{msgCount}
msgCountLock.RUnlock()
go func() {
for {
time.Sleep(time.Second)
msgCountLock.RLock()
msgCountHistory = append(msgCountHistory, msgCount)
msgCountLock.RUnlock()
msgPerNode := (msgCountHistory[len(msgCountHistory)-1] - msgCountHistory[len(msgCountHistory)-2]) / (len(nnets) - 1)
log.Infof("Each node receives %d msg/s or %f MB/s", msgPerNode, float32(msgPerNode*len(msg))/1024/1024)
}
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
<-signalChan
log.Info("\nReceived an interrupt, stopping...\n")
var wg sync.WaitGroup
for i := 0; i < len(nnets); i++ {
wg.Add(1)
go func(nn *nnet.NNet) {
nn.Stop(nil)
wg.Done()
}(nnets[len(nnets)-1-i])
}
wg.Wait()
if len(msgCountHistory) > 2 {
msgPerNode := (msgCountHistory[len(msgCountHistory)-1] - msgCountHistory[0]) / (len(msgCountHistory) - 1) / (len(nnets) - 1)
log.Infof("Each node receives %d msg/s or %f MB/s on average", msgPerNode, float32(msgPerNode*len(msg))/1024/1024)
}
}