Skip to content

Commit

Permalink
Added a benchmark
Browse files Browse the repository at this point in the history
Signed-off-by: Alun Evans <alun@badgerous.net>
  • Loading branch information
alxn committed Nov 17, 2023
1 parent d051f7a commit ed31b9c
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1982,38 +1982,74 @@ 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)
}
return m
}

key := uint64(1)
val := []uint64{1, 2, 3, 4, 5, 6, 7, 8}
possibleCPUs, err := internal.PossibleCPUs()

Check failure on line 1999 in map_test.go

View workflow job for this annotation

GitHub Actions / Build and Lint

undefined: internal.PossibleCPUs (typecheck)

Check failure on line 1999 in map_test.go

View workflow job for this annotation

GitHub Actions / Build and Lint

undefined: internal.PossibleCPUs) (typecheck)
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)
}
}
})
Expand Down

0 comments on commit ed31b9c

Please sign in to comment.