Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit a4aa391

Browse files
committed
p2p/simulation: Add benchmark template for minimal peer protocol init
1 parent cda5556 commit a4aa391

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

p2p/simulations/network_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"strconv"
24+
"strings"
2325
"testing"
2426
"time"
2527

@@ -392,3 +394,79 @@ func triggerChecks(ctx context.Context, ids []enode.ID, trigger chan enode.ID, i
392394
}
393395
}
394396
}
397+
398+
// \todo: refactor to implement shapshots
399+
// and connect configuration methods once these are moved from
400+
// swarm/network/simulations/connect.go
401+
func BenchmarkMinimalService(b *testing.B) {
402+
b.Run("ring/32", benchmarkMinimalServiceTmp)
403+
}
404+
405+
func benchmarkMinimalServiceTmp(b *testing.B) {
406+
407+
// stop timer to discard setup time pollution
408+
b.StopTimer()
409+
args := strings.Split(b.Name(), "/")
410+
nodeCount, err := strconv.ParseInt(args[2], 10, 16)
411+
if err != nil {
412+
b.Fatal(err)
413+
}
414+
415+
for i := 0; i < b.N; i++ {
416+
// this is a minimal service, whose protocol will close a channel upon run of protocol
417+
// making it possible to bench the time it takes for the service to start and protocol actually to be run
418+
protoCMap := make(map[enode.ID]map[enode.ID]chan struct{})
419+
adapter := adapters.NewSimAdapter(adapters.Services{
420+
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
421+
svc := NewNoopService(true)
422+
protoCMap[ctx.Config.ID] = svc.C
423+
return svc, nil
424+
},
425+
})
426+
427+
// create network
428+
network := NewNetwork(adapter, &NetworkConfig{
429+
DefaultService: "noopwoop",
430+
})
431+
defer network.Shutdown()
432+
433+
// create and start nodes
434+
ids := make([]enode.ID, nodeCount)
435+
for i := 0; i < int(nodeCount); i++ {
436+
conf := adapters.RandomNodeConfig()
437+
node, err := network.NewNodeWithConfig(conf)
438+
if err != nil {
439+
b.Fatalf("error creating node: %s", err)
440+
}
441+
if err := network.Start(node.ID()); err != nil {
442+
b.Fatalf("error starting node: %s", err)
443+
}
444+
ids[i] = node.ID()
445+
}
446+
447+
// ready, set, go
448+
b.StartTimer()
449+
450+
// connect nodes in a ring
451+
for i, id := range ids {
452+
peerID := ids[(i+1)%len(ids)]
453+
if err := network.Connect(id, peerID); err != nil {
454+
b.Fatal(err)
455+
}
456+
}
457+
458+
// wait for all protocols to signal to close down
459+
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
460+
defer cancel()
461+
for nodid, peers := range protoCMap {
462+
for peerid, peerC := range peers {
463+
log.Debug("getting ", "node", nodid, "peer", peerid)
464+
select {
465+
case <-ctx.Done():
466+
b.Fatal(ctx.Err())
467+
case <-peerC:
468+
}
469+
}
470+
}
471+
}
472+
}

0 commit comments

Comments
 (0)