github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/examples/map_benchmark/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/dylandreimerink/gobpfld" 9 "github.com/dylandreimerink/gobpfld/bpfsys" 10 "github.com/dylandreimerink/gobpfld/bpftypes" 11 ) 12 13 const mapSize = 10 14 15 func main() { 16 normalMap := &gobpfld.ArrayMap{ 17 AbstractMap: gobpfld.AbstractMap{ 18 Name: gobpfld.MustNewObjName("normal"), 19 Definition: gobpfld.BPFMapDef{ 20 Type: bpftypes.BPF_MAP_TYPE_ARRAY, 21 KeySize: 4, // SizeOf(uint32) 22 ValueSize: 8, // SizeOf(uint64) 23 MaxEntries: mapSize + 1, 24 }, 25 }, 26 } 27 28 mmapMap := &gobpfld.ArrayMap{ 29 AbstractMap: gobpfld.AbstractMap{ 30 Name: gobpfld.MustNewObjName("mmaped"), 31 Definition: gobpfld.BPFMapDef{ 32 Type: bpftypes.BPF_MAP_TYPE_ARRAY, 33 KeySize: 4, // SizeOf(uint32) 34 ValueSize: 8, // SizeOf(uint64) 35 MaxEntries: mapSize + 1, 36 37 // Make MMapable 38 Flags: bpftypes.BPFMapFlagsMMapable, 39 }, 40 }, 41 } 42 43 err := normalMap.Load() 44 if err != nil { 45 fmt.Fprintf(os.Stderr, "error while loading normal map: %s\n", err.Error()) 46 os.Exit(1) 47 } 48 49 err = mmapMap.Load() 50 if err != nil { 51 fmt.Fprintf(os.Stderr, "error while loading mmap map: %s\n", err.Error()) 52 os.Exit(1) 53 } 54 55 for i := uint32(1); i <= mapSize; i++ { 56 val := uint64(i * 10) 57 err = normalMap.Set(i, &val, bpfsys.BPFMapElemAny) 58 if err != nil { 59 fmt.Fprintf(os.Stderr, "error while setting to normal map: %s\n", err.Error()) 60 os.Exit(1) 61 } 62 63 err = mmapMap.Set(i, &val, bpfsys.BPFMapElemAny) 64 if err != nil { 65 fmt.Fprintf(os.Stderr, "error while setting to normal map: %s\n", err.Error()) 66 os.Exit(1) 67 } 68 } 69 70 fmt.Println("[Normal map]") 71 fmt.Println("------------------------") 72 73 start := time.Now() 74 for i := uint32(0); i < 1000000; i++ { 75 var val uint64 76 err = normalMap.Get(i&mapSize, &val) 77 if err != nil { 78 fmt.Fprintf(os.Stderr, "error while getting from normal map: %s\n", err.Error()) 79 os.Exit(1) 80 } 81 } 82 dur := time.Since(start) 83 fmt.Printf("1M took %v, %v per-op\n", dur, dur/1000000) 84 85 start = time.Now() 86 for i := uint32(0); i < 1000000; i++ { 87 var val uint64 88 err = mmapMap.Get(i&mapSize, &val) 89 if err != nil { 90 fmt.Fprintf(os.Stderr, "error while getting from normal map: %s\n", err.Error()) 91 os.Exit(1) 92 } 93 } 94 95 dur = time.Since(start) 96 fmt.Printf("1M took %v, %v per-op\n", dur, dur/1000000) 97 98 fmt.Println("\n[Normal map iterator foreach pointers]") 99 fmt.Println("------------------------") 100 101 var ( 102 key uint32 103 value uint64 104 ) 105 106 start = time.Now() 107 iter := normalMap.Iterator() 108 err = gobpfld.MapIterForEach(iter, &key, &value, func(_, _ interface{}) error { 109 fmt.Printf("key: %d, value: %d\n", key, value) 110 return nil 111 }) 112 if err != nil { 113 fmt.Fprintf(os.Stderr, "foreach ptr: %s\n", err.Error()) 114 os.Exit(1) 115 } 116 dur = time.Since(start) 117 fmt.Printf("1 iter took %v\n", dur) 118 119 fmt.Println("\n[MMapped map iterator foreach pointers]") 120 fmt.Println("------------------------") 121 122 start = time.Now() 123 iter = mmapMap.Iterator() 124 err = gobpfld.MapIterForEach(iter, &key, &value, func(_, _ interface{}) error { 125 fmt.Printf("key: %d, value: %d\n", key, value) 126 return nil 127 }) 128 if err != nil { 129 fmt.Fprintf(os.Stderr, "foreach ptr: %s\n", err.Error()) 130 os.Exit(1) 131 } 132 dur = time.Since(start) 133 fmt.Printf("1 iter took %v\n", dur) 134 }