github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/asm/metadata_test.go (about)

     1  package asm
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  
     7  	"github.com/go-quicktest/qt"
     8  )
     9  
    10  func TestMetadata(t *testing.T) {
    11  	var m Metadata
    12  
    13  	// Metadata should be the size of a pointer.
    14  	qt.Assert(t, qt.Equals(unsafe.Sizeof(m), unsafe.Sizeof(uintptr(0))))
    15  
    16  	// A lookup in a nil meta should return nil.
    17  	qt.Assert(t, qt.IsNil(m.Get(bool(false))))
    18  
    19  	// We can look up anything we inserted.
    20  	m.Set(bool(false), int(0))
    21  	m.Set(int(1), int(1))
    22  	qt.Assert(t, qt.Equals(m.Get(bool(false)), 0))
    23  	qt.Assert(t, qt.Equals(m.Get(1), 1))
    24  
    25  	// We have copy on write semantics
    26  	old := m
    27  	m.Set(bool(false), int(1))
    28  	qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
    29  	qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
    30  	qt.Assert(t, qt.Equals(old.Get(bool(false)), 0))
    31  	qt.Assert(t, qt.Equals(old.Get(int(1)), 1))
    32  
    33  	// Newtypes are handled distinctly.
    34  	type b bool
    35  	m.Set(b(false), int(42))
    36  	qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
    37  	qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
    38  	qt.Assert(t, qt.Equals(m.Get(b(false)), 42))
    39  
    40  	// Setting nil removes a key.
    41  	m.Set(bool(false), nil)
    42  	qt.Assert(t, qt.IsNil(m.Get(bool(false))))
    43  	qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
    44  	qt.Assert(t, qt.Equals(m.Get(b(false)), 42))
    45  }
    46  
    47  func BenchmarkMetadata(b *testing.B) {
    48  	// Assume that three bits of metadata on a single instruction is
    49  	// our worst case.
    50  	const worstCaseItems = 3
    51  
    52  	type t struct{}
    53  
    54  	b.Run("add first", func(b *testing.B) {
    55  		b.ReportAllocs()
    56  
    57  		for i := 0; i < b.N; i++ {
    58  			var v Metadata
    59  			v.Set(t{}, 0)
    60  		}
    61  	})
    62  
    63  	b.Run("add last", func(b *testing.B) {
    64  		var m Metadata
    65  		for i := 0; i < worstCaseItems-1; i++ {
    66  			m.Set(i, i)
    67  		}
    68  
    69  		b.ReportAllocs()
    70  		b.ResetTimer()
    71  
    72  		for i := 0; i < b.N; i++ {
    73  			v := m
    74  			v.Set(t{}, 0)
    75  		}
    76  	})
    77  
    78  	b.Run("add existing", func(b *testing.B) {
    79  		var m Metadata
    80  		for i := 0; i < worstCaseItems-1; i++ {
    81  			m.Set(i, i)
    82  		}
    83  		m.Set(t{}, 0)
    84  
    85  		b.ReportAllocs()
    86  		b.ResetTimer()
    87  
    88  		for i := 0; i < b.N; i++ {
    89  			v := m
    90  			v.Set(t{}, 0)
    91  		}
    92  	})
    93  
    94  	b.Run("get miss", func(b *testing.B) {
    95  		var m Metadata
    96  		for i := 0; i < worstCaseItems; i++ {
    97  			m.Set(i, i)
    98  		}
    99  
   100  		b.ReportAllocs()
   101  		b.ResetTimer()
   102  
   103  		for i := 0; i < b.N; i++ {
   104  			if m.Get(t{}) != nil {
   105  				b.Fatal("got result from miss")
   106  			}
   107  		}
   108  	})
   109  }