-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.go
93 lines (78 loc) · 2.02 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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"sync"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)
const ClientCount = 32
func main() {
http.Handle("/metrics", promhttp.Handler())
go func() {
http.Handle("/debug/metrics/prometheus", promhttp.Handler())
log.Fatal(http.ListenAndServe(":5001", nil))
}()
rcmgr.MustRegisterWith(prometheus.DefaultRegisterer)
str, err := rcmgr.NewStatsTraceReporter()
if err != nil {
log.Fatal(err)
}
rmgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()), rcmgr.WithTraceReporter(str))
if err != nil {
log.Fatal(err)
}
server, err := libp2p.New(libp2p.ResourceManager(rmgr))
if err != nil {
log.Fatal(err)
}
// Make a bunch of clients that all ping the server at various times
wg := sync.WaitGroup{}
for i := 0; i < ClientCount; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
time.Sleep(time.Duration(i%100) * 100 * time.Millisecond)
newClient(peer.AddrInfo{
ID: server.ID(),
Addrs: server.Addrs(),
}, i)
}(i)
}
wg.Wait()
}
func newClient(serverInfo peer.AddrInfo, pings int) {
// Sleep some random amount of time to spread out the clients so the graphs look more interesting
time.Sleep(time.Duration(rand.Intn(100)) * time.Second)
fmt.Println("Started client", pings)
client, err := libp2p.New(
// We just want metrics from the server
libp2p.DisableMetrics(),
libp2p.NoListenAddrs,
)
defer func() {
_ = client.Close()
}()
if err != nil {
log.Fatal(err)
}
client.Connect(context.Background(), serverInfo)
p := ping.Ping(context.Background(), client, serverInfo.ID)
pingSoFar := 0
for pingSoFar < pings {
res := <-p
pingSoFar++
if res.Error != nil {
log.Fatal(res.Error)
}
time.Sleep(5 * time.Second)
}
}