Add benchmarking tests for packet and transport operations
This commit is contained in:
@@ -274,3 +274,58 @@ func TestPacketHashing(t *testing.T) {
|
||||
hash5 := p3.GetHash()
|
||||
_ = hash5 // Use hash5 to avoid unused variable error
|
||||
}
|
||||
|
||||
// BenchmarkPacketOperations benchmarks packet creation, packing, and hashing
|
||||
func BenchmarkPacketOperations(b *testing.B) {
|
||||
// Prepare test data (keep under MTU limit)
|
||||
data := randomBytes(256)
|
||||
transportID := randomBytes(16)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create packet
|
||||
packet := NewPacket(0x00, data, PacketTypeData, ContextNone, 0x00, HeaderType1, transportID, false, 0x00)
|
||||
|
||||
// Pack the packet
|
||||
if err := packet.Pack(); err != nil {
|
||||
b.Fatalf("Packet.Pack() failed: %v", err)
|
||||
}
|
||||
|
||||
// Get hash (triggers crypto operations)
|
||||
_ = packet.GetHash()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkPacketSerializeDeserialize benchmarks the full pack/unpack cycle
|
||||
func BenchmarkPacketSerializeDeserialize(b *testing.B) {
|
||||
// Prepare test data (keep under MTU limit)
|
||||
data := randomBytes(256)
|
||||
transportID := randomBytes(16)
|
||||
|
||||
// Create and pack original packet
|
||||
originalPacket := NewPacket(0x00, data, PacketTypeData, ContextNone, 0x00, HeaderType1, transportID, false, 0x00)
|
||||
if err := originalPacket.Pack(); err != nil {
|
||||
b.Fatalf("Original packet.Pack() failed: %v", err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create new packet from raw data
|
||||
packet := &Packet{Raw: make([]byte, len(originalPacket.Raw))}
|
||||
copy(packet.Raw, originalPacket.Raw)
|
||||
|
||||
// Unpack the packet
|
||||
if err := packet.Unpack(); err != nil {
|
||||
b.Fatalf("Packet.Unpack() failed: %v", err)
|
||||
}
|
||||
|
||||
// Re-pack
|
||||
if err := packet.Pack(); err != nil {
|
||||
b.Fatalf("Packet.Pack() failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
88
pkg/transport/transport_test.go
Normal file
88
pkg/transport/transport_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
)
|
||||
|
||||
func randomBytes(n int) []byte {
|
||||
b := make([]byte, n)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
panic("Failed to generate random bytes: " + err.Error())
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// BenchmarkTransportDestinationCreation benchmarks destination creation
|
||||
func BenchmarkTransportDestinationCreation(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create destination (this allocates and initializes destination objects)
|
||||
dest := transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
_ = dest // Use the destination to avoid optimization
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTransportPathLookup benchmarks path lookup operations
|
||||
func BenchmarkTransportPathLookup(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
// Pre-populate with some destinations
|
||||
destHash1 := randomBytes(16)
|
||||
destHash2 := randomBytes(16)
|
||||
destHash3 := randomBytes(16)
|
||||
|
||||
// Create some destinations
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Test path lookup operations (these involve map lookups and allocations)
|
||||
_ = transport.HasPath(destHash1)
|
||||
_ = transport.HasPath(destHash2)
|
||||
_ = transport.HasPath(destHash3)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTransportHopsCalculation benchmarks hops calculation
|
||||
func BenchmarkTransportHopsCalculation(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
// Create some destinations
|
||||
destHash := randomBytes(16)
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Test hops calculation (involves internal data structure access)
|
||||
_ = transport.HopsTo(destHash)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user