Skip to content

Commit

Permalink
add noalloc tests for identify, eventbus and swarm metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Feb 16, 2023
1 parent 3dcb4b5 commit 37694ca
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
34 changes: 34 additions & 0 deletions p2p/host/eventbus/basic_metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eventbus

import (
"math/rand"
"reflect"
"testing"

Expand Down Expand Up @@ -28,3 +29,36 @@ func BenchmarkSubscriberQueueLength(b *testing.B) {
mt.SubscriberQueueLength(names[i%len(names)], i)
}
}

var eventTypes = []reflect.Type{
reflect.TypeOf(new(event.EvtLocalAddressesUpdated)),
reflect.TypeOf(new(event.EvtNATDeviceTypeChanged)),
reflect.TypeOf(new(event.EvtLocalProtocolsUpdated)),
reflect.TypeOf(new(event.EvtPeerIdentificationCompleted)),
}

var names = []string{
"one",
"two",
"three",
"four",
"five",
}

func TestMetricsNoAllocNoCover(t *testing.T) {
mt := NewMetricsTracer()
tests := map[string]func(){
"EventEmitted": func() { mt.EventEmitted(eventTypes[rand.Intn(len(eventTypes))]) },
"AddSubscriber": func() { mt.AddSubscriber(eventTypes[rand.Intn(len(eventTypes))]) },
"RemoveSubscriber": func() { mt.RemoveSubscriber(eventTypes[rand.Intn(len(eventTypes))]) },
"SubscriberQueueLength": func() { mt.SubscriberQueueLength(names[rand.Intn(len(names))], rand.Intn(100)) },
"SubscriberQueueFull": func() { mt.SubscriberQueueFull(names[rand.Intn(len(names))], rand.Intn(2) == 1) },
"SubscriberEventQueued": func() { mt.SubscriberEventQueued(names[rand.Intn(len(names))]) },
}
for method, f := range tests {
allocs := testing.AllocsPerRun(1000, f)
if allocs > 0 {
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs)
}
}
}
7 changes: 6 additions & 1 deletion p2p/net/swarm/swarm_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,10 @@ func (m *metricsTracer) FailedDialing(addr ma.Multiaddr, err error) {
e = "connection refused"
}
}
dialError.WithLabelValues(transport, e).Inc()

tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)

*tags = append(*tags, transport, e)
dialError.WithLabelValues(*tags...).Inc()
}
60 changes: 60 additions & 0 deletions p2p/net/swarm/swarm_metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package swarm

import (
"context"
"crypto/rand"
"net"
"syscall"
"testing"
"time"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
ma "github.com/multiformats/go-multiaddr"

mrand "math/rand"

"github.com/stretchr/testify/require"
)
Expand All @@ -30,3 +37,56 @@ func BenchmarkMetricsConnOpen(b *testing.B) {
}
}
}

func randItem[T any](items []T) T {
return items[mrand.Intn(len(items))]
}

func TestMetricsNoAllocNoCover(t *testing.T) {
mt := NewMetricsTracer()

connections := []network.ConnectionState{
{StreamMultiplexer: "yamux", Security: "tls", Transport: "tcp"},
{StreamMultiplexer: "yamux", Security: "noise", Transport: "tcp"},
{StreamMultiplexer: "", Security: "", Transport: "quic"},
{StreamMultiplexer: "mplex", Security: "noise", Transport: "tcp"},
}

directions := []network.Direction{network.DirInbound, network.DirOutbound}

_, pub1, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
_, pub2, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
_, pub3, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
keys := []crypto.PubKey{pub1, pub2, pub3}

errors := []error{
context.Canceled,
context.DeadlineExceeded,
&net.OpError{Err: syscall.ETIMEDOUT},
}

addrs := []ma.Multiaddr{
ma.StringCast("/ip4/1.2.3.4/tcp/1"),
ma.StringCast("/ip4/1.2.3.4/tcp/2"),
ma.StringCast("/ip4/1.2.3.4/udp/2345"),
}

tests := map[string]func(){
"OpenedConnection": func() { mt.OpenedConnection(randItem(directions), randItem(keys), randItem(connections)) },
"ClosedConnection": func() {
mt.ClosedConnection(randItem(directions), time.Duration(mrand.Intn(100))*time.Second, randItem(connections))
},
"CompletedHandshake": func() { mt.CompletedHandshake(time.Duration(mrand.Intn(100))*time.Second, randItem(connections)) },
"FailedDialing": func() { mt.FailedDialing(randItem(addrs), randItem(errors)) },
}

for method, f := range tests {
allocs := testing.AllocsPerRun(1000, f)
if allocs > 0 {
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs)
}
}
}
30 changes: 30 additions & 0 deletions p2p/protocol/identify/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package identify

import (
"math/rand"
"testing"

"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/network"
)

func TestMetricsNoAllocNoCover(t *testing.T) {
events := []any{
event.EvtLocalAddressesUpdated{},
event.EvtLocalProtocolsUpdated{},
event.EvtNATDeviceTypeChanged{},
}
dirs := []network.Direction{network.DirInbound, network.DirOutbound, network.DirUnknown}
tr := NewMetricsTracer()
tests := map[string]func(){
"TriggeredPushes": func() { tr.TriggeredPushes(events[rand.Intn(len(events))]) },
"Identify": func() { tr.Identify(dirs[rand.Intn(len(dirs))]) },
"IdentifyPush": func() { tr.IdentifyPush(dirs[rand.Intn(len(dirs))]) },
}
for method, f := range tests {
allocs := testing.AllocsPerRun(1000, f)
if allocs > 0 {
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs)
}
}
}

0 comments on commit 37694ca

Please sign in to comment.