From ed31b9c37200e1d772033462383aafe75afe94d8 Mon Sep 17 00:00:00 2001 From: Alun Evans Date: Fri, 3 Nov 2023 14:23:54 -0700 Subject: [PATCH] Added a benchmark Signed-off-by: Alun Evans --- map_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/map_test.go b/map_test.go index eeeac95e8..a549cc325 100644 --- a/map_test.go +++ b/map_test.go @@ -1982,12 +1982,13 @@ func BenchmarkMarshaling(b *testing.B) { } func BenchmarkPerCPUMarshalling(b *testing.B) { + batch := 1024 newMap := func(valueSize uint32) *Map { m, err := NewMap(&MapSpec{ Type: PerCPUHash, KeySize: 8, ValueSize: valueSize, - MaxEntries: 1, + MaxEntries: uint32(batch), }) if err != nil { b.Fatal(err) @@ -1995,25 +1996,60 @@ func BenchmarkPerCPUMarshalling(b *testing.B) { return m } - key := uint64(1) - val := []uint64{1, 2, 3, 4, 5, 6, 7, 8} + possibleCPUs, err := internal.PossibleCPUs() + if err != nil { + b.Fatal(err) + } + + val := make([]uint64, possibleCPUs) + for i := range val { + val[i] = uint64(i + 1) + } m := newMap(8) - if err := m.Put(key, val[0:]); err != nil { - b.Fatal(err) + for i := 0; i < batch; i++ { + key := uint64(i + 1) + if err := m.Put(key, val); err != nil { + b.Fatal(err) + } } b.Cleanup(func() { m.Close() }) - b.Run("reflection", func(b *testing.B) { + b.Run("Lookup", func(b *testing.B) { b.ReportAllocs() b.ResetTimer() var value []uint64 for i := 0; i < b.N; i++ { - err := m.Lookup(unsafe.Pointer(&key), &value) - if err != nil { - b.Fatal("Can't get key:", err) + for j := 0; j < batch; j++ { + key := uint64(j + 1) + err := m.Lookup(unsafe.Pointer(&key), &value) + if err != nil { + b.Fatal("Can't get key:", err) + } + } + } + }) + b.Run("BatchLookup", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var ( + keys = make([]uint64, batch) + values = make([]uint64, batch*possibleCPUs) + ) + + for i := 0; i < b.N; i++ { + var key uint64 + count, err := m.BatchLookup(nil, unsafe.Pointer(&key), keys, values, &BatchOptions{}) + if count <= 0 { + if err != nil { + b.Fatal("Can't get batch:", err) + } + } + if count != batch { + b.Fatalf("Can't get batch: %d", count) } } })