github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/logging/logging_test.go (about) 1 package p2plogging_test 2 3 import ( 4 "testing" 5 6 "github.com/libp2p/go-libp2p/core/peer" 7 "github.com/stretchr/testify/require" 8 9 p2plogging "github.com/onflow/flow-go/network/p2p/logging" 10 "github.com/onflow/flow-go/utils/unittest" 11 ) 12 13 // TestPeerIdLogging checks the end-to-end functionality of the PeerId logger helper. 14 // It ensures that the PeerId logger helper returns the same string as the peer.ID.String() method. 15 func TestPeerIdLogging(t *testing.T) { 16 pid := unittest.PeerIdFixture(t) 17 pidStr := p2plogging.PeerId(pid) 18 require.Equal(t, pid.String(), pidStr) 19 } 20 21 // BenchmarkPeerIdString benchmarks the peer.ID.String() method. 22 func BenchmarkPeerIdString(b *testing.B) { 23 unittest.SkipBenchmarkUnless(b, unittest.BENCHMARK_EXPERIMENT, "skips peer id string benchmarking, set environment variable to enable") 24 25 count := 100 26 pids := make([]peer.ID, 0, count) 27 for i := 0; i < count; i++ { 28 pids = append(pids, unittest.PeerIdFixture(b)) 29 } 30 31 b.ResetTimer() 32 for i := 0; i < b.N; i++ { 33 _ = pids[i%count].String() 34 } 35 } 36 37 // BenchmarkPeerIdLogging benchmarks the PeerId logger helper, which is expected to be faster than the peer.ID.String() method, 38 // as it caches the base58 encoded peer ID strings. 39 func BenchmarkPeerIdLogging(b *testing.B) { 40 unittest.SkipBenchmarkUnless(b, unittest.BENCHMARK_EXPERIMENT, "skips peer id logging benchmarking, set environment variable to enable") 41 42 count := 100 43 pids := make([]peer.ID, 0, count) 44 for i := 0; i < count; i++ { 45 pids = append(pids, unittest.PeerIdFixture(b)) 46 } 47 48 b.ResetTimer() 49 for i := 0; i < b.N; i++ { 50 _ = p2plogging.PeerId(pids[i%count]) 51 } 52 }