github.com/cilium/cilium@v1.16.2/pkg/monitor/datapath_sock_trace_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package monitor 5 6 import ( 7 "bytes" 8 "encoding/binary" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/cilium/cilium/pkg/byteorder" 14 "github.com/cilium/cilium/pkg/types" 15 ) 16 17 func TestDecodeTraceSockNotify(t *testing.T) { 18 // This check on the struct length constant is there to ensure that this 19 // test is updated when the struct changes. 20 require.Equal(t, 38, TraceSockNotifyLen) 21 22 input := TraceSockNotify{ 23 Type: 0x00, 24 XlatePoint: 0x01, 25 DstIP: types.IPv6{ 26 0x02, 0x03, 27 0x04, 0x05, 28 0x06, 0x07, 29 0x08, 0x09, 30 0x0a, 0x0b, 31 0x0c, 0x0d, 32 0x0e, 0x10, 33 0x11, 0x12, 34 }, 35 DstPort: 0x13_14, 36 SockCookie: 0x15_16_17_18, 37 L4Proto: 0x19, 38 Flags: 0x1a, 39 } 40 41 buf := bytes.NewBuffer(nil) 42 err := binary.Write(buf, byteorder.Native, input) 43 require.Nil(t, err) 44 45 output := &TraceSockNotify{} 46 err = DecodeTraceSockNotify(buf.Bytes(), output) 47 require.Nil(t, err) 48 49 require.Equal(t, input.Type, output.Type) 50 require.Equal(t, input.XlatePoint, output.XlatePoint) 51 require.Equal(t, input.DstIP, output.DstIP) 52 require.Equal(t, input.DstPort, output.DstPort) 53 require.Equal(t, input.SockCookie, output.SockCookie) 54 require.Equal(t, input.L4Proto, output.L4Proto) 55 require.Equal(t, input.Flags, output.Flags) 56 } 57 58 func BenchmarkNewDecodeTraceSockNotify(b *testing.B) { 59 input := &TraceSockNotify{} 60 buf := bytes.NewBuffer(nil) 61 62 if err := binary.Write(buf, byteorder.Native, input); err != nil { 63 b.Fatal(err) 64 } 65 66 b.ReportAllocs() 67 b.ResetTimer() 68 69 for i := 0; i < b.N; i++ { 70 tsn := &TraceSockNotify{} 71 if err := DecodeTraceSockNotify(buf.Bytes(), tsn); err != nil { 72 b.Fatal(err) 73 } 74 } 75 } 76 77 func BenchmarkOldDecodeTraceSockNotify(b *testing.B) { 78 input := &TraceSockNotify{} 79 buf := bytes.NewBuffer(nil) 80 81 if err := binary.Write(buf, byteorder.Native, input); err != nil { 82 b.Fatal(err) 83 } 84 85 b.ReportAllocs() 86 b.ResetTimer() 87 88 for i := 0; i < b.N; i++ { 89 tsn := &TraceSockNotify{} 90 if err := binary.Read(bytes.NewBuffer(buf.Bytes()), byteorder.Native, tsn); err != nil { 91 b.Fatal(err) 92 } 93 } 94 }