github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/iterator_benchmark_test.go (about)

     1  //go:build bpftests
     2  // +build bpftests
     3  
     4  package gobpfld
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/dylandreimerink/gobpfld/bpfsys"
    10  	"github.com/dylandreimerink/gobpfld/bpftypes"
    11  )
    12  
    13  const mapSize = 100000
    14  
    15  var testMap *HashMap
    16  
    17  func getTestMap() *HashMap {
    18  	if testMap != nil {
    19  		return testMap
    20  	}
    21  
    22  	testMap = &HashMap{
    23  		AbstractMap: AbstractMap{
    24  			Name: MustNewObjName("xdp_stats_map"),
    25  			Definition: BPFMapDef{
    26  				Type:       bpftypes.BPF_MAP_TYPE_ARRAY,
    27  				KeySize:    4, // SizeOf(uint32)
    28  				ValueSize:  8, // SizeOf(uint64)
    29  				MaxEntries: mapSize,
    30  			},
    31  		},
    32  	}
    33  
    34  	err := testMap.Load()
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  
    39  	for i := uint32(0); i < mapSize; i++ {
    40  		val := uint64(i * 10)
    41  		err = testMap.Set(&i, &val, bpfsys.BPFMapElemAny)
    42  		if err != nil {
    43  			panic(err)
    44  		}
    45  	}
    46  
    47  	return testMap
    48  }
    49  
    50  func benchmarkBatchMapIterator(bufSize int, b *testing.B) {
    51  	tMap := getTestMap()
    52  
    53  	b.ResetTimer()
    54  
    55  	// run the Fib function b.N times
    56  	for n := 0; n < b.N; n++ {
    57  		iter := batchLookupIterator{
    58  			BPFMap:  tMap,
    59  			BufSize: bufSize,
    60  		}
    61  
    62  		var (
    63  			key   uint32
    64  			value uint64
    65  		)
    66  		err := iter.Init(&key, &value)
    67  		if err != nil {
    68  			b.Error(err)
    69  		}
    70  
    71  		var updated bool
    72  		for updated, err = iter.Next(); updated && err == nil; updated, err = iter.Next() {
    73  		}
    74  		if err != nil {
    75  			b.Error(err)
    76  		}
    77  	}
    78  }
    79  
    80  func BenchmarkBatchMapIterator16(b *testing.B) {
    81  	benchmarkBatchMapIterator(16, b)
    82  }
    83  
    84  func BenchmarkBatchMapIterator64(b *testing.B) {
    85  	benchmarkBatchMapIterator(64, b)
    86  }
    87  
    88  func BenchmarkBatchMapIterator256(b *testing.B) {
    89  	benchmarkBatchMapIterator(256, b)
    90  }
    91  
    92  func BenchmarkBatchMapIterator1024(b *testing.B) {
    93  	benchmarkBatchMapIterator(1024, b)
    94  }
    95  
    96  func BenchmarkBatchMapIterator4096(b *testing.B) {
    97  	benchmarkBatchMapIterator(4096, b)
    98  }
    99  
   100  func BenchmarkBatchMapIterator16384(b *testing.B) {
   101  	benchmarkBatchMapIterator(16384, b)
   102  }