Files

124 lines
3.4 KiB
Go

// Command allmende-conns holds many connections to one node, to measure what
// that costs it (memory above all; a --public node accepts 1000 inbound).
//
// Every client is a small libp2p host that connects over TCP and subscribes to
// the listing topic of a region, like a real peer would. It does nothing else.
// Watch the node's memory from outside (/proc/<pid>/status: VmRSS, VmHWM).
//
// allmende-conns -node /ip4/127.0.0.1/tcp/4001/p2p/12D3Koo... -n 1000 -region u1x -hold 2m
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"sync"
"sync/atomic"
"time"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
)
func main() {
nodeAddr := flag.String("node", "", "multiaddr of the node, with its /p2p/ peer ID")
n := flag.Int("n", 1000, "connections to open")
region := flag.String("region", "u1x", "region whose listing topic every client subscribes to")
hold := flag.Duration("hold", time.Minute, "how long to keep them open once connected")
parallel := flag.Int("parallel", 25, "connections made at the same time")
relay := flag.Bool("relay", false, "measure the node as a relay instead: -n clients take a reservation on it, then pairs talk through it")
flag.Parse()
if *nodeAddr == "" {
fmt.Fprintln(os.Stderr, "usage: allmende-conns -node <multiaddr> [-n 1000] [-region u1x] [-hold 1m]")
os.Exit(2)
}
maddr, err := ma.NewMultiaddr(*nodeAddr)
if err != nil {
fmt.Fprintln(os.Stderr, "bad -node:", err)
os.Exit(2)
}
target, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
fmt.Fprintln(os.Stderr, "-node needs a /p2p/<peer id> part:", err)
os.Exit(2)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
if *relay {
runRelay(ctx, target, *n, *parallel, *hold)
return
}
var (
hosts []host.Host
mu sync.Mutex
ok atomic.Int64
failed atomic.Int64
wg sync.WaitGroup
sem = make(chan struct{}, *parallel)
)
start := time.Now()
for i := 0; i < *n && ctx.Err() == nil; i++ {
sem <- struct{}{}
wg.Add(1)
go func() {
defer wg.Done()
defer func() { <-sem }()
h, err := libp2p.New(libp2p.NoListenAddrs, libp2p.DisableRelay(),
libp2p.ResourceManager(&network.NullResourceManager{}))
if err != nil {
failed.Add(1)
return
}
cctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := h.Connect(cctx, *target); err != nil {
failed.Add(1)
h.Close()
return
}
ps, err := pubsub.NewGossipSub(ctx, h)
if err == nil {
var t *pubsub.Topic
if t, err = ps.Join("/allmende/1/listings/" + *region); err == nil {
_, err = t.Subscribe()
}
}
if err != nil {
failed.Add(1)
h.Close()
return
}
mu.Lock()
hosts = append(hosts, h)
mu.Unlock()
ok.Add(1)
}()
}
wg.Wait()
fmt.Printf("%d connected, %d failed, in %s\n", ok.Load(), failed.Load(), time.Since(start).Round(time.Millisecond))
select {
case <-ctx.Done():
case <-time.After(*hold):
}
still := 0
for _, h := range hosts {
if h.Network().Connectedness(target.ID) == network.Connected {
still++
}
}
fmt.Printf("%d of %d still connected after %s\n", still, len(hosts), (*hold).Round(time.Second))
for _, h := range hosts {
h.Close()
}
}