-
Notifications
You must be signed in to change notification settings - Fork 202
/
main.go
125 lines (96 loc) · 3.25 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
package main
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/p2p/libp2p"
"github.com/ElrondNetwork/elrond-go/p2p/mock"
"github.com/libp2p/go-libp2p/p2p/net/mock"
)
func createMockNetworkArgs() libp2p.ArgsNetworkMessenger {
return libp2p.ArgsNetworkMessenger{
Context: context.Background(),
ListenAddress: libp2p.ListenLocalhostAddrWithIp4AndTcp,
P2pConfig: config.P2PConfig{
Node: config.NodeConfig{},
KadDhtPeerDiscovery: config.KadDhtPeerDiscoveryConfig{
Enabled: false,
},
Sharding: config.ShardingConfig{
Type: p2p.NilListSharder,
},
},
}
}
func main() {
net := mocknet.New(context.Background())
mes1, _ := libp2p.NewMockMessenger(createMockNetworkArgs(), net)
mes2, _ := libp2p.NewMockMessenger(createMockNetworkArgs(), net)
_ = net.LinkAll()
adr2 := mes2.Addresses()[0]
fmt.Printf("Connecting to %s...\n", adr2)
_ = mes1.ConnectToPeer(adr2)
_ = mes1.CreateTopic("test1", true)
_ = mes1.CreateTopic("test2", true)
_ = mes1.CreateTopic("test3", true)
_ = mes2.CreateTopic("test1", true)
_ = mes2.CreateTopic("test2", true)
_ = mes2.CreateTopic("test3", true)
bytesReceived1 := int64(0)
bytesReceived2 := int64(0)
bytesReceived3 := int64(0)
_ = mes1.RegisterMessageProcessor("test1",
&mock.MessageProcessorStub{
ProcessMessageCalled: func(message p2p.MessageP2P, _ p2p.PeerID) error {
atomic.AddInt64(&bytesReceived1, int64(len(message.Data())))
return nil
},
})
_ = mes1.RegisterMessageProcessor("test2", &mock.MessageProcessorStub{
ProcessMessageCalled: func(message p2p.MessageP2P, _ p2p.PeerID) error {
atomic.AddInt64(&bytesReceived2, int64(len(message.Data())))
return nil
},
})
_ = mes1.RegisterMessageProcessor("test3", &mock.MessageProcessorStub{
ProcessMessageCalled: func(message p2p.MessageP2P, _ p2p.PeerID) error {
atomic.AddInt64(&bytesReceived3, int64(len(message.Data())))
return nil
},
})
time.Sleep(time.Second)
timeStart := time.Now()
bytesSent := int64(0)
durTest := time.Second * 5
fmt.Printf("Testing for %s...\n", durTest.String())
for time.Now().UnixNano() < timeStart.Add(durTest).UnixNano() {
buffSize := 5000
buff := make([]byte, buffSize)
bytesSent += int64(buffSize)
mes2.Broadcast("test1", buff)
mes2.Broadcast("test2", buff)
//topic test3 receives more requests to send
mes2.Broadcast("test3", buff)
mes2.Broadcast("test3", buff)
}
fmt.Printf("Sent: %s -> %s\nReceived pipe 1 %s -> %s\nReceived pipe 2 %s -> %s\nReceived pipe 3 %s -> %s\n",
bytesPretty(float64(bytesSent)), bytesPerSecPretty(bytesSent, durTest),
bytesPretty(float64(bytesReceived1)), bytesPerSecPretty(bytesReceived1, durTest),
bytesPretty(float64(bytesReceived2)), bytesPerSecPretty(bytesReceived2, durTest),
bytesPretty(float64(bytesReceived3)), bytesPerSecPretty(bytesReceived3, durTest))
}
func bytesPretty(bytes float64) string {
if bytes < 1024 {
return fmt.Sprintf("%.0f bytes", bytes)
}
if bytes < 1048576 {
return fmt.Sprintf("%.2f kB", bytes/1024.0)
}
return fmt.Sprintf("%.2f MB", bytes/1048576.0)
}
func bytesPerSecPretty(bytes int64, dur time.Duration) string {
return bytesPretty(float64(bytes)/dur.Seconds()) + "/s"
}