github.com/cilium/cilium@v1.16.2/pkg/endpoint/bpf_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package endpoint
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/cilium/cilium/pkg/datapath/linux"
    15  	"github.com/cilium/cilium/pkg/datapath/linux/config"
    16  	datapath "github.com/cilium/cilium/pkg/datapath/types"
    17  	"github.com/cilium/cilium/pkg/testutils"
    18  	testidentity "github.com/cilium/cilium/pkg/testutils/identity"
    19  	testipcache "github.com/cilium/cilium/pkg/testutils/ipcache"
    20  )
    21  
    22  func TestWriteInformationalComments(t *testing.T) {
    23  	s := setupEndpointSuite(t)
    24  
    25  	e := NewTestEndpointWithState(t, s, s, testipcache.NewMockIPCache(), &FakeEndpointProxy{}, testidentity.NewMockIdentityAllocator(nil), 100, StateWaitingForIdentity)
    26  
    27  	var f bytes.Buffer
    28  	err := e.writeInformationalComments(&f)
    29  	require.Nil(t, err)
    30  }
    31  
    32  type writeFunc func(io.Writer) error
    33  
    34  func BenchmarkWriteHeaderfile(b *testing.B) {
    35  	testutils.IntegrationTest(b)
    36  
    37  	s := setupEndpointSuite(b)
    38  
    39  	e := NewTestEndpointWithState(b, s, s, testipcache.NewMockIPCache(), &FakeEndpointProxy{}, testidentity.NewMockIdentityAllocator(nil), 100, StateWaitingForIdentity)
    40  	dp := linux.NewDatapath(linux.DatapathParams{
    41  		RuleManager:    nil,
    42  		NodeAddressing: nil,
    43  		NodeMap:        nil,
    44  		ConfigWriter:   &config.HeaderfileWriter{},
    45  	})
    46  	cfg := datapath.LocalNodeConfiguration{}
    47  
    48  	targetComments := func(w io.Writer) error {
    49  		return e.writeInformationalComments(w)
    50  	}
    51  	targetConfig := func(w io.Writer) error {
    52  		return dp.WriteEndpointConfig(w, &cfg, e)
    53  	}
    54  
    55  	var buf bytes.Buffer
    56  	file, err := os.CreateTemp("", "cilium_ep_bench_")
    57  	if err != nil {
    58  		b.Fatal(err)
    59  	}
    60  	defer file.Close()
    61  
    62  	benchmarks := []struct {
    63  		name   string
    64  		output io.Writer
    65  		write  writeFunc
    66  	}{
    67  		{"in-memory-info", &buf, targetComments},
    68  		{"in-memory-cfg", &buf, targetConfig},
    69  		{"to-disk-info", file, targetComments},
    70  		{"to-disk-cfg", file, targetConfig},
    71  	}
    72  
    73  	for _, bm := range benchmarks {
    74  		b.Run(bm.name, func(b *testing.B) {
    75  			for i := 0; i < b.N; i++ {
    76  				if err := bm.write(bm.output); err != nil {
    77  					b.Fatal(err)
    78  				}
    79  			}
    80  		})
    81  	}
    82  }