Skip to content

Commit

Permalink
Pace clients to be more interesting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 16, 2023
1 parent ba2b93e commit f6d36b4
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions examples/metrics-and-dashboards/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"sync"
"time"

Expand All @@ -19,6 +19,8 @@ import (
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs"
)

const ClientCount = 32

func main() {
http.Handle("/metrics", promhttp.Handler())
go func() {
Expand All @@ -43,7 +45,7 @@ func main() {

// Make a bunch of clients that all ping the server at various times
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
for i := 0; i < ClientCount; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
Expand All @@ -54,28 +56,23 @@ func main() {
}, i)
}(i)
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
// Listen for ctrl c
ctrlC := make(chan os.Signal, 1)
signal.Notify(ctrlC, os.Interrupt)

select {
case <-done:
case <-ctrlC:
}

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)
}
Expand All @@ -85,13 +82,12 @@ func newClient(serverInfo peer.AddrInfo, pings int) {
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(time.Second)
time.Sleep(5 * time.Second)
}
}

0 comments on commit f6d36b4

Please sign in to comment.