github.com/noironetworks/cilium-net@v1.6.12/pkg/endpoint/bpf_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package endpoint
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"io/ioutil"
    23  	"testing"
    24  
    25  	"github.com/cilium/cilium/pkg/datapath/linux"
    26  
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  func (s *EndpointSuite) TestWriteInformationalComments(c *C) {
    31  	e := NewEndpointWithState(s, 100, StateCreating)
    32  
    33  	var f bytes.Buffer
    34  	err := e.writeInformationalComments(&f)
    35  	c.Assert(err, IsNil)
    36  }
    37  
    38  type writeFunc func(io.Writer) error
    39  
    40  func BenchmarkWriteHeaderfile(b *testing.B) {
    41  	e := NewEndpointWithState(&suite, 100, StateCreating)
    42  	dp := linux.NewDatapath(linux.DatapathConfiguration{}, nil)
    43  
    44  	targetComments := func(w io.Writer) error {
    45  		return e.writeInformationalComments(w)
    46  	}
    47  	targetConfig := func(w io.Writer) error {
    48  		return dp.WriteEndpointConfig(w, e)
    49  	}
    50  
    51  	var buf bytes.Buffer
    52  	file, err := ioutil.TempFile("", "cilium_ep_bench_")
    53  	if err != nil {
    54  		b.Fatal(err)
    55  	}
    56  	defer file.Close()
    57  
    58  	benchmarks := []struct {
    59  		name   string
    60  		output io.Writer
    61  		write  writeFunc
    62  	}{
    63  		{"in-memory-info", &buf, targetComments},
    64  		{"in-memory-cfg", &buf, targetConfig},
    65  		{"to-disk-info", file, targetComments},
    66  		{"to-disk-cfg", file, targetConfig},
    67  	}
    68  
    69  	for _, bm := range benchmarks {
    70  		b.Run(bm.name, func(b *testing.B) {
    71  			for i := 0; i < b.N; i++ {
    72  				if err := bm.write(bm.output); err != nil {
    73  					b.Fatal(err)
    74  				}
    75  			}
    76  		})
    77  	}
    78  }